用實(shí)驗(yàn)快速掌握Apache
前言:Apache是大多數(shù)linux版本的標(biāo)準(zhǔn)web 服務(wù)器,掌握他的意義就非常大。
本文以實(shí)驗(yàn)的方式,讓讀者快速掌握Apache的基本配置。
[實(shí)驗(yàn)?zāi)康?/SPAN>]
1.Apache的基本安裝和配置。
2.Apache中應(yīng)用CGI。
3.基本代理配置。
[實(shí)驗(yàn)要求]
1.兩臺(tái)互通的RH8.0機(jī)。
2.這兩臺(tái)機(jī)已配置過DNS。
[實(shí)驗(yàn)步驟]
1.準(zhǔn)備工作。
由于web 服務(wù)器與DNS關(guān)系非常緊密。作好這步準(zhǔn)備工作,請(qǐng)參考本人的另一篇文章
“用實(shí)驗(yàn)快速掌握DNS配置”。這篇文章發(fā)表在linuxsir的網(wǎng)絡(luò)版塊。
DNS 能夠?qū)崿F(xiàn)的主要功能就是能能解析:station1.exmaple1.com,
server1.example1.com 兩個(gè)域名的IP,如果你想在/etc/hosts中實(shí)現(xiàn),我想也是不是不可
以。這里不提倡這么做。
本文中作法比較教條,這里指明機(jī)器1為:server1.example1.com,機(jī)器2為:
station1.example1.com .讀者也可以自己變通。
2.在機(jī)器1上安裝和配置簡單的Apache。
1) 檢查下列包是否安裝,
httpd
httpd-manual
缺什么,裝什么。
2) 開啟httpd服務(wù)
# service httpd start
如果開啟失敗,查看/var/log/httpd/下的相關(guān)日志,切記要在/etc/hosts中添加類似
192.168.0.254 example1.com server1 的一行。
3) 檢查/etc/httpd/conf/httpd.conf中,有下列一行活動(dòng)
DocumentRoot /var/www/html
4) 用一個(gè)瀏覽器打開:
http://server1.example1.com
如果是正常情況,你會(huì)看到Apache的介紹頁面。
5) 創(chuàng)建新的目錄和html文件。
# mkdir -p /var/www/virtual/server1.example1.com/html
# vi /var/www/virtual/server1.example1.com/html/index.html
<b> Server1.example1.com </b>
6) 編輯/etc/httpd/conf/httpd.conf,末尾追加下列文本。
NameVirtualHost 192.168.0.254
<VirtualHost 192.168.0.254>
ServerName server1.example1.com
ServerAdmin root@server1.example1.com
DocumentRoot /var/www/virtual/server1.example1.com/html
ErrorLog logs/server1.example1.com-error_log
CustomLog logs/server1.example1.com-access_log combined
<Directory>
Options Indexes Includes
</Directory>
</VirtualHost>
7) 確保DNS能夠解析你的VirtualHost
# host server1.example1.com
8) 重新啟動(dòng)httpd
# service httpd restart
如果啟動(dòng)失敗,看/var/log/httpd/下的相應(yīng)日志,會(huì)告訴你具體哪里錯(cuò)誤。
9) 在瀏覽器里能夠看到你自己寫的網(wǎng)頁了嗎?
http://server1.example1.com
10) 在機(jī)器2上能看到http://server1.example1.com 嗎?
3.機(jī)器1,在Apache中應(yīng)用CGI
1) 編輯/etc/httpd/conf/httpd.conf,在<VirtualHost>塊中添加下列一行:
ScriptAlias /cgi-bin/ /var/www/virtual/server1.example1.com/cgi-bin/
2) 創(chuàng)建目錄,創(chuàng)建文件。
# mkdir /var/www/virtual/server1.example1.com/cgi-bin
#vi /var/www/virtual/server1.example1.com/cgi-bin/test.sh
#!/bin/bash
echo Content-Type: text/html
echo
echo “<pre>”
echo My username is :
whoami
echo
echo My id is :
id
echo
echo Here is /etc/passwd :
cat /etc/passwd
echo “</pre>”
3) 改變test.sh的執(zhí)行權(quán)限:
# chmod 555 test.sh
4) 重啟httpd服務(wù):
# service httpd restart
若有錯(cuò),看日志。
5) 瀏覽:http://server1.example1.com/cgi-bin/test.sh
6) 機(jī)器2能瀏覽http://server1.example1.com/cgi-bin/test.sh嗎?
4.基本代理配置:
1) 機(jī)器1,檢查squid 包裝了沒有,沒有的話,則進(jìn)行安裝。
2) 機(jī)器1,啟動(dòng)squid 服務(wù)。
# service squid start
有問題? 看/var/log/messages.
3) 機(jī)器1,瀏覽器中設(shè)置代理端口為3128,舉例:在mozilla: Eite | Preferences...|
Advanced | proxies 中,設(shè)定手動(dòng),端口為:3128,其他不動(dòng)。
機(jī)器2,瀏覽器中類似設(shè)置為:手動(dòng),http代理:192.168.0.254 端口:3128。
4) 此時(shí),機(jī)器1,能瀏覽server1.exmaple1.com.,若平時(shí)能上internet,此時(shí)也正常上
internet。
5) 機(jī)器2,不能瀏覽server1.example1.com 或internet。
查看/var/log/httpd/*的日志文件,原因是什么?
6) 編輯/etc/squid/squid.conf,在acl CONNECT method CONNECT行下,添加下列一行:
acl examample1 src 192.168.0.0/24
找到INSERT YOUR OWN RULE(S) HERE 增加下列一行:
http_access allow example1
7) 重啟squid
# service squid restart
有錯(cuò)?看日志。
8) 此時(shí),機(jī)器2能夠?yàn)g覽server1.example1.com 或 internet網(wǎng)頁.
9) 編輯/etc/squid/squid.conf ,在acl examample1 src 192.168.0.0/24行下添加
acl otherdeny dstdomain .sina.com.cn
在http_access allow example1下添加:
http_access deny otherdeny
10) 重啟squid.
# service squid restart
有錯(cuò)? 看日志。
11) 機(jī)器2,仍能瀏覽 http://www.sina.com.cn,為什么?
12) 編輯/etc/squid/squid.conf ,把http_access deny otherdeny放到
http_access allow example1前面,重啟squid,機(jī)器2還能看到
http://www.sina.com.cn嗎?
[實(shí)驗(yàn)總結(jié)]
Apache配置是否成功,與DNS有很大關(guān)系,所以,要求讀者先花時(shí)間作好DNS工作。本文以最通俗的方式,教你簡單的配置Apache,如果想深入掌握它,還得研究Apache2.0的文檔。最近看到http://kajaa.bbs.us/ApacheManual/zh-cn/是kajaa老兄翻譯成了Apache的中文文檔,對(duì)大家一定有好處。