在我們使用Apache這一伺服器軟體的過程中,難免會遇到一些緩慢的PHP頁面,但是Apache可沒有Nginx自帶的「slowlog」功能,所以今天就來教大家如何在Apache使用這個類似「slowlog」的「mod_log_slow」來定位響應慢的請求
教學
1、首先我們先從GitHub下載mod_log_slow並進入該模組的目錄:
提示:推薦大家最好先 cd
至「~/」或「/tmp/」中
1 2 3 4 5 6 7 8 |
[root@arefly ~] git clone git://github.com/yokawasa/mod_log_slow.git Initialized empty Git repository in /root/mod_log_slow/.git/ remote: Reusing existing pack: 113, done. remote: Total 113 (delta 0), reused 0 (delta 0) Receiving objects: 100% (113/113), 30.23 KiB, done. Resolving deltas: 100% (58/58), done. [root@arefly ~] cd mod_log_slow [root@arefly mod_log_slow] |
2、然後我們需要用 vim Makefile
並按下 i 開啟編輯,來在「Makefile」這一文件中的 ap_basedir
處設定你的Apache的安裝目錄(例如我的Apache就是安裝在「/usr/local/apache/」中的)
1 2 3 4 5 6 7 8 9 10 |
## ## Makefile -- Build procedure for sample log_slow Apache module ## Autogenerated via ``apxs -n log_slow -g''. ## ap_basedir=【你的Apache安裝目錄】 builddir=. top_srcdir=$(ap_basedir) top_builddir=$(ap_basedir) include $(ap_basedir)/build/special.mk |
3、編輯完成後,我們按下 esc 並輸入 :wq
來儲存並退出該文件
4、現在我們就要來開始編譯(make
)並安裝(make install
)該模組:
1 2 3 4 5 6 7 8 |
[root@arefly mod_log_slow] make /usr/local/apache/build/libtool --silent --mode=compile gcc -std=gnu99 -g -O2 -pthread -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/local/apache/include -I. -I/root/lnmp/src/httpd-2.4.9/srclib/apr/include -I/root/lnmp/src/httpd-2.4.9/srclib/apr-util/include -I/root/lnmp/src/httpd-2.4.9/srclib/apr-util/xml/expat/lib -I/usr/local/include -prefer-pic -c mod_log_slow.c && touch mod_log_slow.slo /usr/local/apache/build/libtool --silent --mode=link gcc -std=gnu99 -g -O2 -pthread -o mod_log_slow.la -rpath /usr/local/apache/modules -module -avoid-version mod_log_slow.lo [root@arefly mod_log_slow] make install make[1]: Entering directory `/root/mod_log_slow' /usr/local/apache/build/libtool --silent --mode=install install mod_log_slow.la /usr/local/apache/modules/ make[1]: Leaving directory `/root/mod_log_slow' /usr/local/apache/build/libtool --silent --mode=install install mod_log_slow.la /usr/local/apache/modules/ |
5、編譯及安裝完成後,我們就需要 vim
編輯你的Apache安裝目錄下的「conf/httpd.conf」文件(例如我的伺服器的該文件就位於「/usr/local/apache/conf/httpd.conf」):
1 2 3 4 5 |
[root@arefly ~] cd /usr/local/apache/conf [root@arefly conf] ls extra httpd.conf.bak magic original httpd.conf httpd.conf.slow_log mime.types vhost [root@arefly conf] vim httpd.conf |
6、然後我們還是先輸入 /LoadModule
來查詢「LoadModule
」,然後我們就可以看到類似下面的介面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# # Dynamic Shared Object (DSO) Support # # To be able to use the functionality of a module which was built as a DSO you # have to place corresponding `LoadModule' lines at this location so the # directives contained in it are actually available _before_ they are used. # Statically compiled modules (those listed by `httpd -l') do not need # to be loaded here. # # Example: # LoadModule foo_module modules/mod_foo.so # LoadModule authn_file_module modules/mod_authn_file.so #LoadModule authn_dbm_module modules/mod_authn_dbm.so #LoadModule authn_anon_module modules/mod_authn_anon.so #LoadModule authn_dbd_module modules/mod_authn_dbd.so #LoadModule authn_socache_module modules/mod_authn_socache.so LoadModule authn_core_module modules/mod_authn_core.so LoadModule authz_host_module modules/mod_authz_host.so |
7、接著按下 i 來開啟編輯模式,並將 LoadModule log_slow_module modules/mod_log_slow.so
添加至第一個 LoadModule
上方(即高亮部分):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# # Dynamic Shared Object (DSO) Support # # To be able to use the functionality of a module which was built as a DSO you # have to place corresponding `LoadModule' lines at this location so the # directives contained in it are actually available _before_ they are used. # Statically compiled modules (those listed by `httpd -l') do not need # to be loaded here. # # Example: # LoadModule foo_module modules/mod_foo.so # LoadModule log_slow_module modules/mod_log_slow.so LoadModule authn_file_module modules/mod_authn_file.so #LoadModule authn_dbm_module modules/mod_authn_dbm.so #LoadModule authn_anon_module modules/mod_authn_anon.so #LoadModule authn_dbd_module modules/mod_authn_dbd.so #LoadModule authn_socache_module modules/mod_authn_socache.so LoadModule authn_core_module modules/mod_authn_core.so LoadModule authz_host_module modules/mod_authz_host.so |
8、新增完成後,按下 esc 並輸入 :wq
來儲存並退出該文件
9、我們現在已經成功將該模組加入至Apache中,我們還需要在你的虛擬主機(vhost)中設定一下
10、首先進入你的Apache安裝目錄下的「conf/vhost/」目錄,然後 vim
編輯你的虛擬主機文件(例如我的就是「/usr/local/apache/conf/vhost/www.arefly.com.conf」)
1 2 3 4 5 6 7 |
[root@arefly ~] cd /usr/local/apache/conf/vhost [root@arefly vhost] ls 0.conf file.arefly.com.conf www.arefly.com.conf chat.arefly.com.conf lab.arefly.com.conf demo.arefly.com.conf tools.arefly.com.conf music.arefly.com.conf www.spthk.com.conf download.arefly.com.conf [root@arefly vhost] vim www.arefly.com.conf |
11、接著在 <VirtualHost *:[端口]>
中加入下列代碼:
注意:你可以將 /usr/local/apache/logs/slow_log
改成你想存放慢日誌的位置!
1 2 3 4 5 6 |
## (VirtualHost) mod_log_slow configuration LogSlowEnabled On LogSlowLongRequestTime 100 LogSlowFileName /usr/local/apache/logs/slow_log LogSlowTimeFormat "[%Y-%m-%d %H:%M:%S]" LogSlowBufferedLogs Off |
12、現在我們的文件就應該看起來像這樣:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<VirtualHost *:8080> ServerAdmin info@arefly.com DocumentRoot "/mnt/web/wwwroot/arefly" ServerName www.arefly.com ServerAlias arefly.com ErrorLog "/mnt/web/wwwlogs/www.arefly.com_error_apache.log" CustomLog "/mnt/web/wwwlogs/www.arefly.com_apache.log" common ## (VirtualHost) mod_log_slow configuration LogSlowEnabled On LogSlowLongRequestTime 100 LogSlowFileName /usr/local/apache/logs/slow_log LogSlowTimeFormat "[%Y-%m-%d %H:%M:%S]" LogSlowBufferedLogs Off <Directory "/mnt/web/wwwroot/arefly"> SetOutputFilter DEFLATE Options FollowSymLinks Require all granted AllowOverride All Order allow,deny Allow from all DirectoryIndex index.html index.php </Directory> </VirtualHost> |
13、最後我們來重啟一下Apache,讓這些功能生效:
1 2 |
[root@arefly ~] service httpd restart [root@arefly ~] |
一段時間過後...
14、現在我們就可以看看我們的slow_log日誌文件(默認路徑為「/usr/local/apache/logs/slow_log」)記錄下來了什麼,然後慢慢優化自己的伺服器吧!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@arefly conf] cat /usr/local/apache/logs/slow_log 39e1:53c4dc7f:0 [2014-07-15 15:47:01] elapsed: 9.99 cpu: 0.00(usr)/0.00(sys) pid: 14817 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /gravatar HTTP/1.0 39e0:53c4dc81:0 [2014-07-15 15:47:11] elapsed: 2.33 cpu: 0.27(usr)/0.00(sys) pid: 14816 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /gravatar HTTP/1.0 39e5:53c4dc84:0 [2014-07-15 15:47:13] elapsed: 3.27 cpu: 0.97(usr)/0.00(sys) pid: 14821 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /gravatar/ HTTP/1.0 39e2:53c4dc89:0 [2014-07-15 15:47:03] elapsed: 17.69 cpu: 0.00(usr)/0.00(sys) pid: 14818 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET / HTTP/1.0 39e6:53c4dc89:0 [2014-07-15 15:47:17] elapsed: 3.85 cpu: 0.00(usr)/0.00(sys) pid: 14822 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: POST /wp-content/themes/weisaysimple/comments-ajax.php HTTP/1.0 39e0:53c4dc8a:1 [2014-07-15 15:47:21] elapsed: 1.19 cpu: 0.60(usr)/0.02(sys) pid: 14816 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: POST /wp-admin/admin-ajax.php HTTP/1.0 39e5:53c4dc8b:1 [2014-07-15 15:47:21] elapsed: 2.07 cpu: 0.25(usr)/0.01(sys) pid: 14821 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /gravatar HTTP/1.0 3a1c:53c4dc8f:0 [2014-07-15 15:47:23] elapsed: 3.24 cpu: 0.96(usr)/0.00(sys) pid: 14876 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /gravatar/ HTTP/1.0 39e3:53c4dc93:0 [2014-07-15 15:47:29] elapsed: 2.14 cpu: 0.26(usr)/0.01(sys) pid: 14819 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /gravatar HTTP/1.0 39e0:53c4dc94:2 [2014-07-15 15:47:30] elapsed: 1.92 cpu: 0.40(usr)/0.01(sys) pid: 14816 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: POST /wp-admin/admin-ajax.php HTTP/1.0 39e5:53c4dc95:2 [2014-07-15 15:47:31] elapsed: 2.02 cpu: 0.26(usr)/0.00(sys) pid: 14821 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /gravatar HTTP/1.0 3a16:53c4dc97:0 [2014-07-15 15:47:32] elapsed: 3.57 cpu: 0.48(usr)/0.03(sys) pid: 14870 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /force-browsers-load-latest-css-js/ HTTP/1.0 3a1c:53c4dc99:1 [2014-07-15 15:47:33] elapsed: 3.54 cpu: 0.91(usr)/0.02(sys) pid: 14876 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /gravatar/ HTTP/1.0 39e7:53c4dc99:0 [2014-07-15 15:47:36] elapsed: 1.28 cpu: 0.41(usr)/0.02(sys) pid: 14823 ip: 127.0.0.1 host: www.arefly.com:80 reqinfo: GET /wp-admin/admin-ajax.php?postviews_id=6841&action=postviews&_=1405410454093 HTTP/1.0 |
15、想要停止使用此功能也很簡單,將前面加入「httpd.conf」及「vhost/虛擬機名稱.conf」的代碼移除即可!