天天看點

linux下搭建HTTP網站伺服器和網站日志分析工具AWStats的使用

伺服器IP位址:192.168.4.5

伺服器主機名:srv5.tarena.com

1.在伺服器端安裝httpd軟體包

[root@ser5 /]# yum -y install httpd

[root@ser5 /]# service httpd start

[root@ser5 /]# chkconfig httpd on

2.在客戶機端驗證

在浏覽器中輸入192.168.4.5

如果顯示歡迎頁面表示伺服器搭建成功

3.部署網頁文檔 

首先将歡迎頁面配置移除

[root@ser5 桌面]# cd /etc/httpd/conf.d/

[root@ser5 conf.d]# mv welcome.conf welcome.conf.bak

建立測試首頁

[root@svr5 ~]# cd /var/www/html/

[root@svr5 html]# vim index.html

Hello World!

浏覽器驗證

4.網站目錄遷移和預設首頁設定

[root@ser5 html]# mkdir /var/ftp

[root@ser5 html]# echo "wo shi ftp" > /var/ftp/index.html

[root@ser5 html]# vim /etc/httpd/conf/httpd.conf

修改配置如下

DocumentRoot "/var/ftp"

将原來<Directory "/var/www/html">修改為<Directory "/var/ftp">

DirectoryIndex index.php index.html index.html.var

重新加載配置檔案

[root@ser5 html]# service httpd reload

5.禁止使用符号連結和自動索引

[root@ser5 ftp]# ln -s /var/www/html/index.html haha.html

浏覽器輸入驗證http://192.168.4.5/haha.html

通過驗證知道預設可以使用符号連結

下面禁用符号連結

[root@ser5 ftp]# vim /etc/httpd/conf/httpd.conf

<Directory "目錄路徑">

     Options  -FollowSymlinks

     .. ..

</Directory>

[root@svr5 ~]# service httpd restart

預設情況下首頁不存在時通路網站會出現該目錄的索引

[root@ser5 ftp]# rm -rf index.html 

浏覽器驗證出現索引

下面禁用自動索引

[root@svr5 ~]# vim /etc/httpd/conf/httpd.conf

     Options  -Indexes

浏覽器驗證出現錯誤頁面

6.使用目錄别名

[root@ser5 ftp]# echo "index" > /usr/src/index.html

[root@ser5 ftp]# vim /etc/httpd/conf/httpd.conf 

在檔案末尾添加一行alias /tools "/usr/src/"

[root@ser5 ftp]# service httpd reload

浏覽器輸入http://192.168.4.5/tools/能夠通路伺服器/usr/src/下的首頁

7.客戶機通路控制

允許從任何位址通路

<Directory  /var/www/html>

    Order  allow,deny

    Allow  from  all

僅允許從個别網段、IP位址通路

    Allow  from  192.168.4.0/24  192.168.7.200

8.為指定目錄啟用使用者授權

在網站根目錄下建一個子目錄test

能通路 http://伺服器位址/test/

給studir添加使用者授權,隻允許使用者stu01通路(密碼123456)

建立首頁和測試目錄

[root@ser5 ftp]# echo "Hello World" > index.html

[root@ser5 ftp]# mkdir test

建立兩個對test有權限的使用者(不必是系統使用者)

[root@ser5 ftp]# htpasswd -c /etc/httpd/authpwd stu01

[root@ser5 ftp]# htpasswd  /etc/httpd/authpwd stu02

<Directory "/var/ftp/test">

    AuthName "Tarena Library."

    AuthType basic

    AuthUserFile /etc/httpd/authpwd

    #Require valid-user  //允許對test有權限的使用者通路

    Require user stu01   //隻允許stu01通路test目錄  (和上面Require兩個不能同時使    用)

浏覽器輸入http://192.168.4.5可以通路

輸入http://192.168.4.5/test/需要輸入賬戶和密碼驗證

9.建構AWStats日志分析系統

下載下傳awstats工具

[root@ser5 ~]# tar zxf awstats-7.1.tar.gz

[root@ser5 ~]# mv awstats-7.1 /usr/local/awstats

[root@ser5 ~]# cd /usr/local/awstats/tools/

[root@ser5 tools]# ./awstats_configure.pl 

> /etc/httpd/conf/httpd.conf (填寫httpd的配置檔案路徑)

file (required if first install) [y/N] ? y

> srv5.tarena.com(填寫做要日志分析的網站域名)

> 

Press ENTER to continue... 

> http://localhost(伺服器名)/awstats/awstats.pl?config=srv5.tarena.com(網站域名)

//檢視日志分析的網址

Press ENTER to finish...

修改awstats配置檔案,指定要做日志分析的日志路徑

[root@ser5 tools]# vim /etc/awstats/awstats.srv5.tarena.com.conf

LogFile="/var/log/httpd/access_log" (指定日志路徑)

建立日志分析出的資料指定目錄

[root@ser5 tools]# mkdir  /var/lib/awstats

執行日志分析,并設定cron計劃任務

[root@svr5 tools]# ./awstats_updateall.pl  now

.. ..

[root@svr5 tools]# crontab  -e

*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now

檢視分析結果

http://192.168.4.5/awstats/awstats.pl?config=srv5.tarena.com

為友善檢視分析結果,可以将此超連結放到網站根目錄的一個網頁中

[root@svr5 ~]# vim /網站根目錄/aw-svr5.html

<html>  <head>

<meta http-equiv=refresh content="0;

url=http://伺服器位址/awstats/awstats.pl?config=網站域名">

</head> 

<body> </body>  </html>

通路http://伺服器位址/aw-svr5.html

10.建立基于域名的虛拟主機

在沒有配置DNS伺服器的情況下通過修改/etc/hosts檔案來實作客戶機對伺服器的域名解析

[ser5 /]# vim /etc/hosts (在伺服器和用戶端同時做這個步驟)

192.168.4.5 www.baidu.com www.google.com

建立兩個虛拟主機的主目錄和首頁

[root@ser5 /]# mkdir /var/www/baidu

[root@ser5 /]# echo "baidu.com" > /var/www/baidu/index.html

[root@ser5 /]# mkdir /var/www/google

[root@ser5 /]# echo "google.com" > /var/www/google/index.html

建立虛拟主機的配置檔案

[root@ser5 /]# vim /etc/httpd/conf.d/Vhost.conf

NameVirtualHost 192.168.4.5

<VirtualHost 192.168.4.5>

        ServerName www.baidu.com

        DocumentRoot "/var/www/baidu"

</VirtualHost>

        ServerName www.google.com

        DocumentRoot "/var/www/google"

[root@ser5 /]# service httpd reload

在客戶機上分别輸入www.baidu.com和www.google.com

注:如果輸入伺服器IP位址,打開的是設定的第一個虛拟主機,因為當設定虛拟主機時,原來在httpd.conf中設定的網站将不能生效,如果想将原來在httpd.conf生效需要在虛拟主機配置檔案修改如下

        ServerName 192.168.4.5

        DocumentRoot "/var/ftp"

然後輸入http://伺服器IP 進行驗證

本文轉自 無心低語 51CTO部落格,原文連結:http://blog.51cto.com/fengzhankui/1540200,如需轉載請自行聯系原作者

繼續閱讀