1. The role of log segmentation
We know that after the service installation is complete, there will be a logs file under the relevant service, which contains access logs and error logs. Access logs can help us record visitor information. Error logs are generally used in service construction, service configuration, and startup to quickly find the cause of errors. They are also assistants for operation and maintenance personnel. However, the default logs are all in one file. As the service time goes by, the information in the logs will become very lengthy and difficult to find. For example, when I want to find the logs of a certain day in the previous week, I need to go one by one. Flipping through, greatly increasing the difficulty and time cost of maintenance services. So log segmentation is very necessary
This time we introduce two methods of splitting logs about apche, which can help us automatically split logs and classify logs
Two, rotatelogs segmentation
Use Apache's own rotatelogs segmentation tool to automatically split Apache's logs according to the date of each day
1) Modify the main configuration file of the apache service
vim /usr/local/httpd/conf/httpd.conf
#分割错误日志
ErrorLog "| /usr/local/bin/rotatelogs -l /var/log/httpd/benet.com-error_%F.log 86400"
#分割访问日志
CustomLog "| /usr/local/bin/rotatelogs -l /var/log/httpd/benet.com-access_%F.log 86400" combined
2) Create a split log save directory
mkdir /var/log/httpd
systemctl restart httpd
3) Access test to observe whether the log segmentation function is realized
[root@www logs]#ls /var/log/httpd/
3. Software introduction segmentation - AWStats analysis system
AWStats is an open source log analysis system developed in Perl language, which is used to complete automated log statistics and analysis
1) Unzip the software package
cd /opt
tar zxvf awstats-7.6.tar.gz
mv /opt/awstats-7.6 /usr/local/awstats
cd /usr/local/awstats/tools
./awstats_configure.pl
2) Create a configuration file for the site to be counted
3) Modify the automatically generated awstats access rights and load the CGI module
Note: Apache 2.4 and above need to load the CGI module
--143行开始取消以下的注释--
--跳至末行修改--
<Directory "/usr/local/awstats/wwwroot">
Options None
AllowOverride None
#Order allow,deny #注释掉
#Allow from all #注释掉
Require all granted #添加
</Directory>
4) Modify the site statistics configuration file
#awstats目录默认不存在,需要手动创建
mkdir /var/lib/awstats
vim /etc/awstats/awstats.www.yang.com.conf
5) Perform log analysis and set cron scheduled tasks
systemctl restart httpd
cd /usr/local/awstats/tools/
./awstats_updateall.pl now #更新数据(根据站点配置文件指定的日志文件路径获取日志数据)
#注意,更新数据前最好先把access.log文件清空后重新访问站点,再获取日志数据。
crontab -e
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now
systemctl start crond
6) Visit the AWStats analysis system site
[root@www tools]#systemctl stop firewalld
[root@www tools]#systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@www tools]#setenforce 0
[root@www tools]#echo "192.168.73.105 www.yang.com" >> /etc/hosts
[root@www tools]#crontab -e
crontab: installing new crontab
您在 /var/spool/mail/root 中有邮件
在主机中进行访问测试:http://www.yang.com/awstats/awstats.pl?config=www.yang.com
7) Optimize web page address
vim /usr/local/httpd/htdocs/aws.html
<html>
<head>
<meta http-equiv=refresh content="0;url=http://www.yang.com/awstats/awstats.pl?config=www.yang.com">
</head>
<body></body>
</html>
Visit the test again:
在虚拟机火狐中:
只需要访问地址:http://www.yang.com/aws.html