HTTP服務的安裝及相關配置
實驗目的:對HTTP服務進行安裝配置,以達到訪問目的及訪問權限控制
實驗準備:安裝有apache軟件的服務端(192.168.1.1)
客戶機1(192.168.1.2)客戶機2(192.168.1.3)
保證客戶機與服務端的連通
實驗一:服務端的基本配置與HTTP服務的安裝,初始瀏覽
1. 服務端的基本配置
Ip配置:
[root@localhost~]# ifconfig eth0 | head -2 |tail -1
Inetaddr:192.168.1. Bcast:192.168.1.255Mask:255.255.255.0
主機名配置:
[root@localhost~]# tail -1 /etc/sysconfig/network
HOSTNAME=web01.tarena.com
修改hosts文件,增加域名指向:
[root@localhost ~]# cat /etc/hosts | tail -2
192.168.1.1 web01.tarena.com web01
192.168.1.1 www.tarena.comweb01
2. HTTP服務的安裝
服務端的安裝
通過yum庫安裝http服務
[root@localhost ~]# yum -y install httpd
啓動服務
[root@localhostconf]# servicehttpd restart
[root@localhostconf]#chkconfighttpd on
通過瀏覽器訪問本地地址看查看默認頁
實驗二:基本HTTP服務器的配置
Web服務器域名:www.tarena.com
默認首頁包括:index.html、index.php
開啓保持連接
確認默認httpd是否支持php
1.備份主配置文件,以防出錯
[root@localhost~]# cp/etc/httpd/conf/httpd.confhttpd.conf.bak
2.修改主配置文件,實現實驗要求
[root@localhost~]# vim/etc/httpd/conf/httpd.conf
74 KeepAliveON --開啓保持連接默認爲OFF
265 ServerNamewww.tarena.com:80 --本站點名稱
#ServerNamewww.example.com:80 --默認名稱爲註釋
391 DirectoryIndex index.html index.php–-更改默認首頁包括html,php
DirectoryIndexindex.html index.html.var --默認索引頁/首頁文件
保存退出後重啓服務
[root@localhostconf]# servicehttpd restart
[root@localhostconf]#chkconfighttpd on
3.在網站根目錄(/var/www/html)下創建.html文件並在客戶端訪問測試
[root@localhost~]# cd /var/www/html
[root@localhosthtml]# touch index.html
[root@localhosthtml]# vim index.html
[root@localhosthtml]# cat index.html
<html>
<body>haha</body> 訪問測試頁內容爲haha
</html>
服務端測試結果如下圖
在網站根目錄下編寫php頁面並測試
[root@localhost html]#touchtest.php
[root@localhost html]#cattest.php
<?php
phpinfo()
?>
進入客戶機登錄http://www.tarena.com/test.php進行測試(默認php服務未安裝)
實驗三:
配置只允許客戶機192.168.1.2訪問
允許所有用戶訪問www.tarena.com/authdir/index.html
1.進入主配置文件修改,以達到控制訪問目的
306 <Directory"/var/www/html">指定目錄
332 Order allow,deny 訪問權限
333 # Allow from all 允許所有訪問,默認
334 Allow from 192.168.1.2 允許訪問地址(生效)
335</Directory>
保存退出後重啓服務,進入客戶機1,2分別測試
客戶機1(192.168.1.2)可以訪問www.tarena.com
客戶機2(192.168.1.3)沒有訪問權限
2.進入主配置文件增加訪問控制條目,以達到允許所有用戶訪問www.tarena.com/authdir/index.html的目的
在服務端網站根目錄下新建站點authdir並創建index.html測試頁面
[root@localhost html]# mkdir/var/www/html/authdir
[root@localhosthtml ]#touch/var/www/html/authdir/index.html
[root@localhost html]#catauthdir/index.html
天空之城
進入主配置文件增加訪問控制條目
337 <Directory/var/www/html/authdir> 指定目錄
338 Order allow,deny
339 Allow from all
340 </Directory>
保存退出重啓後進入客戶端進入www.tarena.com/authdir測試
測試可得客戶機1,2都可以登錄到www.tarena.com/authdir看到測試內容
試驗四:HTTP的用戶授權
客戶端訪問http://www.tarena.com/authdir需要輸入用戶名密碼驗證
1.進入主配置文件在authdir下設置訪問控制權限
337 <Directory /var/www/html/authdir> 指定目錄
338 Order allow,deny
339 Allow from all
340 AuthName "PleaseInput Password!!"
頁面提示
341 AuthType Basic 認證類型,一般爲基本
342 AuthUserFile"/etc/httpd/.vuser"
登錄賬戶密碼所在位置
343 Require valid-user指定授權用戶
344 </Directory>
保存後退出並重啓服務
2.新建允許登錄賬戶密碼並指定存放位置/etc/httpd/.vuser
[root@localhost html]# htpasswd-c /etc/httpd/.vuser admin(允許登錄用戶)
New password:
Re-type new password:
Adding password for user admin
[root@localhost html]# cat/etc/httpd/.vuser
admin:UX8W9njqYtbUc
3.進入客戶機測試
測試證明訪問控制生效,需要輸入賬號用戶名纔可登錄
實驗五:HTTP目錄別名
要求:客戶端訪問http://www.tarena.com/sina時可以訪問/var/www/html/sina.com/bbs下的網頁
1.創建/var/www/html/sina.com/bbs目錄並在其內創建測試頁面
[root@localhost html]# mkdir -p/var/www/html/sina.com/bbs
[root@localhost html]# cat/var/www/html/sina.com/bbs/index.html
<html>
<head><title>Thisis a test Page!!!</title>
<body>
<h1>This is bbs.sina.comtest Page!!!</h1>
</body>
</head>
</html>
2.在主配置文件中增加/var/www/html/sina.com/bbs的別名/sina
1000 Alias /sina "/var/www/html/sina.com/bbs"
3.從客戶端登錄www.tarena.com/sina測試
實驗六:
查看默認HTTP使用進程管理方式
更改默認進程管理方式爲worker模式
1.查看默認HTTP使用的進程管理方式(管理方式分爲prefork,worker)
Prefork:
<IfModuleprefork.c>
StartServers 8 啓動服務時創建的進程數
MinSpareServers 5 最少空閒進程數
MaxSpareServers 20 最多空閒進程數
ServerLimit 256 最大客戶端上限
MaxClients 256 最多進程數,小於等於上值
MaxRequestsPerChild 4000 每個進程最多處理的請求
</IfModule>
Worker:
<IfModuleworker.c>
StartServers 2 啓動服務時創建的進程數
MaxClients 150 最多線程數,應小於ServerLimit
MinSpareThreads 25 最少空閒線程數
MaxSpareThreads 75 最多空閒線程數
ThreadsPerChild 25 每個進程可提供的線程數
MaxRequestsPerChild 0 每個進程最多處理的請求數
</IfModule>
2.更改默認進程管理方式爲worker模式
[root@localhost ~]# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
[root@localhost ~]# cd/usr/sbin/
[root@localhostsbin]#mvhttpdhttpd.prefork
[root@localhostsbin]#mvhttpd.workerhttpd
[root@localhostsbin]#servicehttpd restart
[root@localhostsbin]# httpd -l
Compiled in modules:
core.c
worker.c
http_core.c
mod_so.c
試驗七:
部署Awstats統計Http訪問日誌
配置Awstats(建立在HTTP基礎上的訪問日誌統計軟件)
1.將準備好的壓縮包解壓到/usr/local,並進入解壓到的目錄
[root@localhost~]# tar -zxvf awstats-7.1.tar.gz -C /usr/local/
[root@localhost~]# cd /usr/local/
2.將解壓完的目錄改名爲awstats,進入其目錄下的tools並啓動awstats_configure.pl進行配置
[root@localhost local]# mv awstats-7.1/ awstats
因爲在配置過程中將會在httpd主配置文件中生成<Directory"/usr/local/awstats/wwwroot">權限列表,如不改名將與目錄不符,不能生效
[root@localhost local]# cdawstats/tools/
[root@localhost tools]# ./awstats_configure.pl
Config file path ('none' to skip web server setup):
>/etc/httpd/conf/httpd.conf輸入apache的主配置文件
-----> Need to create a new configfile ?
Do you want me to build a new AWStatsconfig/profile
file (required if first install) [y/N] ? y 生成awstats的配置文件
Your web site, virtual server or profile name:
> www.tarena.com 輸入web服務器名字
Default: /etc/awstats
Directory path to store config file(s) (Enter for default): 回車
>
/usr/local/awstats/tools/awstats_updateall.pl now 啓動文件目錄
Press ENTER to continue... 回車
Press ENTER to finish... 回車
3.修改主配置文件
[root@localhost tools]# vim /etc/awstats/awstats.www.tarena.com.conf
LogFile="/var/log/httpd/access_log" 指定提取成功訪問的日誌位置
4.創建awstats日誌存放位置
[root@localhost tools]# mkdir/var/lib/awstats
啓動服務
[root@localhost ~]# cd /usr/local/awstats/tools/
[root@localhost tools]# ./awstats_updateall.pl now
[root@localhost tools]# servicehttpd restart
Running '"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -update-config=www.tarena.com -configdir="/etc/awstats"' to update configwww.tarena.com
5.進入客戶端登錄http://www.tarena.com/awstats/awstats.pl?config=www.tarena.com進行測試
6.如要定時刷新可配置計劃任務
[root@localhost tools]# crontab –e
[root@localhost tools]# crontab -l
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now
[root@localhost tools]# servicecrond restart
[root@localhost tools]# chkconfigcrond on
7.可通過html代碼實現網頁跳轉功能,獲得簡潔域名就能訪問
[root@localhost tools]# cat /var/www/html/awstats.html
<html>
<head><meta http-equiv=refresh content="0; url=http://www.tarena.com/awstats/awstats.pl?config=www.tarena.com">
</head>
<body>
</body>
</html>