天天看點

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

│   └── index.html

    └── 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

<code>&lt;VirtualHost *:80&gt;</code>

<code>    </code><code>ServerAdmin [email protected]</code>

<code>    </code><code>DocumentRoot </code><code>"/var/html/www"</code>

<code>    </code><code>ServerName www.zhang.com</code>

<code>    </code><code>ServerAlias zhang.com</code>

<code>    </code><code>ErrorLog </code><code>"logs/www-error_log"</code>

<code>    </code><code>CustomLog </code><code>"logs/www-access_log"</code> <code>common</code>

<code>&lt;</code><code>/VirtualHost</code><code>&gt;</code>

<code>    </code><code>DocumentRoot </code><code>"/var/html/blog"</code>

<code>    </code><code>ServerName blog.zhang.com</code>

<code>    </code><code>ServerAlias blog.com</code>

<code>    </code><code>ErrorLog </code><code>"logs/blog-error_log"</code>

<code>    </code><code>CustomLog </code><code>"logs/blog-access_log"</code> <code>common</code>

<code>    </code><code>DocumentRoot </code><code>"/var/html/bbs"</code>

<code>    </code><code>ServerName bbs.zhang.com</code>

<code>    </code><code>ServerAlias bbs.com</code>

<code>    </code><code>ErrorLog </code><code>"logs/bbs-error_log"</code>

<code>    </code><code>CustomLog </code><code>"logs/bbs-access_log"</code> <code>common</code>

#修改主配置檔案加載虛拟站點,去掉注釋#号

vim /usr/local/httpd/conf/httpd.conf

<code>Include conf</code><code>/extra/httpd-mpm</code><code>.conf</code>

<code>Include conf</code><code>/extra/httpd-vhosts</code><code>.conf</code>

#增加虛拟目錄通路權限

<code>&lt;Directory </code><code>"/var/html"</code><code>&gt;</code>

<code>    </code><code>Options FollowSymLinks</code>

<code>    </code><code>AllowOverride None</code>

<code>    </code><code>Require all granted</code>

<code>&lt;</code><code>/Directory</code><code>&gt;</code>

#檢查配置檔案,重新加載配置檔案

/etc/init.d/httpd -t

Syntax OK

/etc/init.d/httpd graceful

#測試通路站點,注意需要在客戶機上配置hosts,配置域名解析

2.基于端口的虛拟主機

#在主配置檔案中增加端口監聽

<code>Listen 80</code>

<code>Listen 8000</code>

<code>Listen 9000</code>

#修改虛拟主機端口号

<code>&lt;VirtualHost *:8000&gt;</code>

<code>&lt;VirtualHost *:9000&gt;</code>

3.基于ip的虛拟主機

<code>&lt;VirtualHost 10.0.1:80&gt;</code>

<code>&lt;VirtualHost 10.0.2:8000&gt;</code>

<code>&lt;VirtualHost 10.0.3:9000&gt;</code>

二、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

#修改虛拟主機日志記錄按天使用輪詢

<code>    </code><code>CustomLog </code><code>"|/usr/local/sbin/cronolog /usr/local/http/logs/access_bbs_%Y%m%d.log"</code> <code>combined</code>

<code>#增加上面一行,對BBS進行通路日志輪詢,以天為機關</code>

2.使用apache自帶工具

<code>    </code><code>ErrorLog </code><code>"|/usr/local/httpd/bin/rotatelogs /usr/local/http/logs/access_bbs_%Y%m%d-%H:%M.log 5M"</code>   <code>#以大小為機關輪詢日志</code>

<code>    </code><code>CustomLog </code><code>"|/usr/local/httpd/bin/rotatelogs /usr/local/http/logs/access_bbs_%Y%m%d-%H:%M.log 86400"</code> <code>combined</code>

<code>#對BBS進行通路日志輪詢,86400為妙數,以天為機關</code>

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 &amp;&gt;/dev/null" &gt;&gt;/var/spool/cron/root 

三、分析日志

#檢視通路日志中通路次數最多的ip取最多的10個

<code>awk</code> <code>'{print $1}'</code> <code>access_bbs_20170423.log |</code><code>sort</code><code>|</code><code>uniq</code> <code>-c|</code><code>sort</code> <code>-rn -k1|</code><code>head</code> <code>-10</code>

<code>#同上</code>

<code>awk</code> <code>'{++S[$1]} END {for (key in S) print S[key],key}'</code> <code>access_bbs_20170423.log |</code><code>sort</code> <code>-rn -k1|</code><code>head</code> <code>-10</code>

四、修改隐藏版本資訊

1、在編譯安裝之前更改

vim /root/tools/httpd-2.4.25/include/ap_release.h

<code>#define AP_SERVER_BASEVENDOR "Apache Software Foundation"</code>

<code>#define AP_SERVER_BASEPROJECT "Apache HTTP Server" </code>

<code>#define AP_SERVER_BASEPRODUCT "Apache"</code>

<code>#define AP_SERVER_MAJORVERSION_NUMBER 2  #主版本</code>

<code>#define AP_SERVER_MINORVERSION_NUMBER 4  #次版本号</code>

<code>#define AP_SERVER_PATCHLEVEL_NUMBER   25   </code>

<code>#define AP_SERVER_DEVBUILD_BOOLEAN    0</code>

vim /root/tools/httpd-2.4.25/os/unix/os.h

<code>#define PLATFORM "Unix"  #運作系統</code>

2、在安裝完後修改配置檔案,隐藏版本資訊

curl -I 10.0.0.4   #檢視版本資訊

<code>HTTP</code><code>/1</code><code>.1 200 OK</code>

<code>Date: Sun, 23 Apr 2017 15:35:39 GMT</code>

<code>Server: Apache</code><code>/2</code><code>.4.25 (Unix) PHP</code><code>/5</code><code>.6.30</code>

<code>Last-Modified: Thu, 20 Apr 2017 17:33:10 GMT</code>

<code>ETag: </code><code>"15-54d9c88ebe6ed"</code>

<code>Accept-Ranges: bytes</code>

<code>Content-Length: 21</code>

<code>Content-Type: text</code><code>/html</code>

#去掉版本号

vim /usr/local/httpd/conf/extra/httpd-default.conf

<code>ServerTokens Prod</code>

<code>ServerSignature Off</code>

vim /usr/local/httpd/conf/httpd.conf #去掉前面的注釋#号

<code>Include conf</code><code>/extra/httpd-default</code><code>.conf</code>

#重新加載配置

../bin/apachectl graceful

#重新檢視版本資訊

curl -I 10.0.0.4

<code>Date: Sun, 23 Apr 2017 15:39:10 GMT</code>

<code>Server: Apache    </code><code>#此次已不再顯示版本資訊</code>

版權聲明:原創作品,如需轉載,請注明出處。否則将追究法律責任

本文轉自 80後小菜鳥 51CTO部落格,原文連結:http://blog.51cto.com/zhangxinqi/1918702

繼續閱讀