在我们使用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」的代码移除即可!