用實驗快速掌握Apache
用實驗快速掌握Apache
前言:Apache是大多數linux版本的標準web 服務器,掌握他的意義就非常大。
本文以實驗的方式,讓讀者快速掌握Apache的基本配置。
[實驗目的]
1.Apache的基本安裝和配置。
2.Apache中應用CGI。
3.基本代理配置。
[實驗要求]
1.兩臺互通的RH8.0機。
2.這兩臺機已配置過DNS。
[實驗步驟]
1.準備工作。
由于web 服務器與DNS關系非常緊密。作好這步準備工作,請參考本人的另一篇文章
“用實驗快速掌握DNS配置”。這篇文章發表在linuxsir的網絡版塊。
DNS 能夠實現的主要功能就是能能解析:station1.exmaple1.com,
server1.example1.com 兩個域名的IP,如果你想在/etc/hosts中實現,我想也是不是不可
以。這里不提倡這么做。
本文中作法比較教條,這里指明機器1為:server1.example1.com,機器2為:
station1.example1.com .讀者也可以自己變通。
2.在機器1上安裝和配置簡單的Apache。
1) 檢查下列包是否安裝,
httpd
httpd-manual
缺什么,裝什么。
2) 開啟httpd服務
# service httpd start
如果開啟失敗,查看/var/log/httpd/下的相關日志,切記要在/etc/hosts中添加類似
192.168.0.254 example1.com server1 的一行。
3) 檢查/etc/httpd/conf/httpd.conf中,有下列一行活動
DocumentRoot /var/www/html
4) 用一個瀏覽器打開:
如果是正常情況,你會看到Apache的介紹頁面。
5) 創建新的目錄和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) 重新啟動httpd
# service httpd restart
如果啟動失敗,看/var/log/httpd/下的相應日志,會告訴你具體哪里錯誤。
9) 在瀏覽器里能夠看到你自己寫的網頁了嗎?
10) 在機器2上能看到http://server1.example1.com 嗎?
3.機器1,在Apache中應用CGI
1) 編輯/etc/httpd/conf/httpd.conf,在<VirtualHost>塊中添加下列一行:
ScriptAlias /cgi-bin/ /var/www/virtual/server1.example1.com/cgi-bin/
2) 創建目錄,創建文件。
# 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的執行權限:
# chmod 555 test.sh
4) 重啟httpd服務:
# service httpd restart
若有錯,看日志。
5) 瀏覽:http://server1.example1.com/cgi-bin/test.sh
6) 機器2能瀏覽http://server1.example1.com/cgi-bin/test.sh嗎?
4.基本代理配置:
1) 機器1,檢查squid 包裝了沒有,沒有的話,則進行安裝。
2) 機器1,啟動squid 服務。
# service squid start
有問題? 看/var/log/messages.
3) 機器1,瀏覽器中設置代理端口為3128,舉例:在mozilla: Eite | Preferences...|
Advanced | proxies 中,設定手動,端口為:3128,其他不動。
機器2,瀏覽器中類似設置為:手動,http代理:192.168.0.254 端口:3128。
4) 此時,機器1,能瀏覽server1.exmaple1.com.,若平時能上internet,此時也正常上
internet。
5) 機器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
有錯?看日志。
8) 此時,機器2能夠瀏覽server1.example1.com 或 internet網頁.
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
有錯? 看日志。
11) 機器2,仍能瀏覽 http://www.sina.com.cn,為什么?
12) 編輯/etc/squid/squid.conf ,把http_access deny otherdeny放到
http_access allow example1前面,重啟squid,機器2還能看到
[實驗總結]
Apache配置是否成功,與DNS有很大關系,所以,要求讀者先花時間作好DNS工作。本文以最通俗的方式,教你簡單的配置Apache,如果想深入掌握它,還得研究Apache2.0的文檔。最近看到http://kajaa.bbs.us/ApacheManual/zh-cn/是kajaa老兄翻譯成了Apache的中文文檔,對大家一定有好處。
posted on 2005-09-26 08:39 MingIsMe 閱讀(169) 評論(0) 編輯 收藏 所屬分類: 09 Linux