apache虛擬機器主機、日誌輪詢、日誌統計、去版本最佳化

來源:互聯網
上載者:User

標籤: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 [email protected]    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 [email protected]    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 [email protected]    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.confInclude 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 80Listen 8000Listen 9000

#修改虛擬機器主機連接埠號碼

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

<VirtualHost *:80>    ServerAdmin [email protected]    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 [email protected]    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 [email protected]    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 [email protected]    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 [email protected]    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 [email protected]    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 [email protected]    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 [email protected]    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 OKDate: Sun, 23 Apr 2017 15:35:39 GMTServer: Apache/2.4.25 (Unix) PHP/5.6.30Last-Modified: Thu, 20 Apr 2017 17:33:10 GMTETag: "15-54d9c88ebe6ed"Accept-Ranges: bytesContent-Length: 21Content-Type: text/html

#去掉版本號碼

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

ServerTokens ProdServerSignature 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 OKDate: Sun, 23 Apr 2017 15:39:10 GMTServer: Apache    #此次已不再顯示版本資訊Last-Modified: Thu, 20 Apr 2017 17:33:10 GMTETag: "15-54d9c88ebe6ed"Accept-Ranges: bytesContent-Length: 21Content-Type: text/html

本文出自 “80後小菜鳥” 部落格,請務必保留此出處http://zhangxinqi.blog.51cto.com/9668428/1918702

apache虛擬機器主機、日誌輪詢、日誌統計、去版本最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.