首页 > 代码库 > apache虚拟主机、日志轮询、日志统计、去版本优化
apache虚拟主机、日志轮询、日志统计、去版本优化
一、虚拟主机
1.基于域名的虚拟主机
www.zhang.com /var/html/www
blog.zhang.com /var/html/blog
bbs.zhang.com /var/html/bbs
#创建虚拟机目录
mkdir /var/html/{www,blog,bbs} -p
tree /var/html/
/var/html/
├── bbs
├── blog
└── www
#创建默认文件
touch /var/html/{www,blog,bbs}/index.html
tree /var/html/
/var/html/
├── bbs
│ └── index.html
├── blog
│ └── index.html
└── www
└── index.html
#写入默认文件内容
for name in www blog bbs;do echo "http://$name.zhang.com" >/var/html/$name/index.html;done
for name in www blog bbs;do cat /var/html/$name/index.html;done
http://www.zhang.com
http://blog.zhang.com
http://bbs.zhang.com
#配置虚拟站点
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80> ServerAdmin root@123.com DocumentRoot "/var/html/www" ServerName www.zhang.com ServerAlias zhang.com ErrorLog "logs/www-error_log" CustomLog "logs/www-access_log" common </VirtualHost>
<VirtualHost *:80> ServerAdmin root@123.com DocumentRoot "/var/html/blog" ServerName blog.zhang.com ServerAlias blog.com ErrorLog "logs/blog-error_log" CustomLog "logs/blog-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin root@123.com DocumentRoot "/var/html/bbs" ServerName bbs.zhang.com ServerAlias bbs.com ErrorLog "logs/bbs-error_log" CustomLog "logs/bbs-access_log" common </VirtualHost>
#修改主配置文件加载虚拟站点,去掉注释#号
vim /usr/local/httpd/conf/httpd.conf
Include conf/extra/httpd-mpm.conf Include conf/extra/httpd-vhosts.conf
#增加虚拟目录访问权限
vim /usr/local/httpd/conf/httpd.conf
<Directory "/var/html"> Options FollowSymLinks AllowOverride None Require all granted </Directory>
#检查配置文件,重新加载配置文件
/etc/init.d/httpd -t
Syntax OK
/etc/init.d/httpd graceful
#测试访问站点,注意需要在客户机上配置hosts,配置域名解析
http://www.zhang.com
http://blog.zhang.com
http://bbs.zhang.com
2.基于端口的虚拟主机
#在主配置文件中增加端口监听
vim /usr/local/httpd/conf/httpd.conf
Listen 80 Listen 8000 Listen 9000
#修改虚拟主机端口号
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80> ServerAdmin root@123.com DocumentRoot "/var/html/www" ServerName www.zhang.com ServerAlias zhang.com ErrorLog "logs/www-error_log" CustomLog "logs/www-access_log" common </VirtualHost> <VirtualHost *:8000> ServerAdmin root@123.com DocumentRoot "/var/html/blog" ServerName blog.zhang.com ServerAlias blog.com ErrorLog "logs/blog-error_log" CustomLog "logs/blog-access_log" common </VirtualHost> <VirtualHost *:9000> ServerAdmin root@123.com DocumentRoot "/var/html/bbs" ServerName bbs.zhang.com ServerAlias bbs.com ErrorLog "logs/bbs-error_log" CustomLog "logs/bbs-access_log" common </VirtualHost>
#检查配置文件,重新加载配置文件
/etc/init.d/httpd -t
Syntax OK
/etc/init.d/httpd graceful
3.基于ip的虚拟主机
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 10.0.1:80> ServerAdmin root@123.com DocumentRoot "/var/html/www" ServerName www.zhang.com ServerAlias zhang.com ErrorLog "logs/www-error_log" CustomLog "logs/www-access_log" common </VirtualHost> <VirtualHost 10.0.2:8000> ServerAdmin root@123.com DocumentRoot "/var/html/blog" ServerName blog.zhang.com ServerAlias blog.com ErrorLog "logs/blog-error_log" CustomLog "logs/blog-access_log" common </VirtualHost> <VirtualHost 10.0.3:9000> ServerAdmin root@123.com DocumentRoot "/var/html/bbs" ServerName bbs.zhang.com ServerAlias bbs.com ErrorLog "logs/bbs-error_log" CustomLog "logs/bbs-access_log" common </VirtualHost>
二、apache日志轮询
1.使用cronolog软件实现日志轮询
tar zxvf cronolog-1.6.2.tar.gz
cd cronolog-1.6.2
./configure
make
make install
#查看cronolog软件安装路径
ll /usr/local/sbin/cronolog
-rwxr-xr-x 1 root root 40438 4月 23 22:49 /usr/local/sbin/cronolog
#修改虚拟主机日志记录按天使用轮询
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:9000> ServerAdmin root@123.com DocumentRoot "/var/html/bbs" ServerName bbs.zhang.com ServerAlias bbs.com ErrorLog "logs/bbs-error_log" CustomLog "|/usr/local/sbin/cronolog /usr/local/http/logs/access_bbs_%Y%m%d.log" combined #增加上面一行,对BBS进行访问日志轮询,以天为单位 </VirtualHost>
2.使用apache自带工具
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:9000> ServerAdmin root@123.com DocumentRoot "/var/html/bbs" ServerName bbs.zhang.com ServerAlias bbs.com ErrorLog "|/usr/local/httpd/bin/rotatelogs /usr/local/http/logs/access_bbs_%Y%m%d-%H:%M.log 5M" #以大小为单位轮询日志 CustomLog "|/usr/local/httpd/bin/rotatelogs /usr/local/http/logs/access_bbs_%Y%m%d-%H:%M.log 86400" combined #对BBS进行访问日志轮询,86400为妙数,以天为单位 </VirtualHost>
3、使用shell脚本加定时任务轮询日志文件
cat /usr/local/httpd/logpolling.sh
cd /usr/local/httpd/logs
mv www-access_log www-access_$(date +%F)_log
/usr/local/httpd/bin/apachectl graceful
echo "0 0 * * * /usr/local/httpd/logpolling.sh &>/dev/null" >>/var/spool/cron/root
三、分析日志
#查看访问日志中访问次数最多的ip取最多的10个
awk ‘{print $1}‘ access_bbs_20170423.log |sort|uniq -c|sort -rn -k1|head -10 #同上 awk ‘{++S[$1]} END {for (key in S) print S[key],key}‘ access_bbs_20170423.log |sort -rn -k1|head -10
四、修改隐藏版本信息
1、在编译安装之前更改
vim /root/tools/httpd-2.4.25/include/ap_release.h
#define AP_SERVER_BASEVENDOR "Apache Software Foundation" #define AP_SERVER_BASEPROJECT "Apache HTTP Server" #define AP_SERVER_BASEPRODUCT "Apache" #define AP_SERVER_MAJORVERSION_NUMBER 2 #主版本 #define AP_SERVER_MINORVERSION_NUMBER 4 #次版本号 #define AP_SERVER_PATCHLEVEL_NUMBER 25 #define AP_SERVER_DEVBUILD_BOOLEAN 0
vim /root/tools/httpd-2.4.25/os/unix/os.h
#define PLATFORM "Unix" #运行系统
2、在安装完后修改配置文件,隐藏版本信息
curl -I 10.0.0.4 #查看版本信息
HTTP/1.1 200 OK Date: Sun, 23 Apr 2017 15:35:39 GMT Server: Apache/2.4.25 (Unix) PHP/5.6.30 Last-Modified: Thu, 20 Apr 2017 17:33:10 GMT ETag: "15-54d9c88ebe6ed" Accept-Ranges: bytes Content-Length: 21 Content-Type: text/html
#去掉版本号
vim /usr/local/httpd/conf/extra/httpd-default.conf
ServerTokens Prod ServerSignature Off
vim /usr/local/httpd/conf/httpd.conf #去掉前面的注释#号
Include conf/extra/httpd-default.conf
#重新加载配置
../bin/apachectl graceful
#重新查看版本信息
curl -I 10.0.0.4
HTTP/1.1 200 OK Date: Sun, 23 Apr 2017 15:39:10 GMT Server: Apache #此次已不再显示版本信息 Last-Modified: Thu, 20 Apr 2017 17:33:10 GMT ETag: "15-54d9c88ebe6ed" Accept-Ranges: bytes Content-Length: 21 Content-Type: text/html
本文出自 “80后小菜鸟” 博客,请务必保留此出处http://zhangxinqi.blog.51cto.com/9668428/1918702
apache虚拟主机、日志轮询、日志统计、去版本优化