chenjiali 发表于 2018-11-19 07:31:46

LAMP搭建9:Apache日志切割

  网站的访问日志增长很快,如果保存在一个文件中,不进行切割,那么很快就会撑爆服务器的磁盘,我们需要对其进行切割,并且定期删除无用的日志,以保证磁盘的合理使用。
  1.编辑虚拟主机配置文件
  # vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
  2.去掉访问日志配置行前面的注释符#
  
  DocumentRoot "/tmp/123"
  ServerName invalid.com
  
  
  DocumentRoot "/data/www"
  ServerName www.test.com
  ServerAlias www.mydiscuz.com
  ServerAlias www.others.com
  ErrorLog "logs/dummy-host.example.com-error_log"
      CustomLog "logs/dummy-host.example.com-access_log" combined
  
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www.mydiscuz.com$
  RewriteCond %{HTTP_HOST} ^www.others.com$
  RewriteRule ^/(.*)$ http://www.test.com/$1
  
  
  AllowOverride AuthConfig
  AuthName "username"
  AuthType Basic
  AuthUserFile /data/.htpasswd
  require valid-user
  
  
  其中combined是日志格式名字,主配置文件里定义了三种日志格式combined、common、combinedio。我们这里使用combined格式。
  3.检查无误后重新加载配置文件:
  # apachectl -t
  Syntax OK
  # apachectl graceful
  4.在浏览器中多访问几次,就可以生成访问日志和错误日志(没有错误时为空)
  # cd /usr/local/apache2/logs/
  # ls
  access_log                         error_log
  dummy-host.example.com-access_loghttpd.pid
  dummy-host.example.com-error_log
  查看主配置文件中日志格式定义:
  # vim /usr/local/apache2/conf/httpd.conf
  ……
  
  #
  # The following directives define some format nicknames for use with
  # a CustomLog directive (see below).
  #
  LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  LogFormat "%h %l %u %t \"%r\" %>s %b" common
  
  # You need to enable mod_logio.c to use %I and %O
  LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
  
  ……
  格式说明:
  LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  %h来源IP
  %l不存在时用'—'表示
  %u用户名,不存在时用'—'表示
  %t时间
  %r动作,GET或POST
  5.编辑虚拟主机配置文件,定义日志切割的方式:
  
  DocumentRoot "/tmp/123"
  ServerName invalid.com
  
  
  DocumentRoot "/data/www"
  ServerName www.test.com
  ServerAlias www.mydiscuz.com
  ServerAlias www.others.com
  ErrorLog "logs/test.com-error_log"
      CustomLog "| /usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400" combined
  
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www.mydiscuz.com$
  RewriteCond %{HTTP_HOST} ^www.others.com$
  RewriteRule ^/(.*)$ http://www.test.com/$1
  
  
  AllowOverride AuthConfig
  AuthName "username"
  AuthType Basic
  AuthUserFile /data/.htpasswd
  require valid-user
  
  
  说明:
  使用Apache自带的rotatelogs工具,-l 指定存放的绝对路径,%Y%m%d(YYYYMMDD),86400秒是一天,表示按一天为单位切割
  6.检查无误后重新加载配置文件:
  # apachectl -t
  Syntax OK
  # apachectl graceful
  7.访问我们的网站就可产生指定切割格式的日志文件:
  # ls
  access_log                         httpd.pid
  dummy-host.example.com-access_logtest.com-access_20170111_log
  dummy-host.example.com-error_log   test.com-error_log
  error_log
  




页: [1]
查看完整版本: LAMP搭建9:Apache日志切割