Apache 是大多數 Linux 發行版的標準 Web 伺服器,掌握它的意義不言可喻。本文用實驗的方式,帶你快速跑完 Apache 的基本配置。
目標:Apache 基本安裝與設定、在 Apache 中應用 CGI、基本代理設定。環境:兩台互通的 RH8.0 機器,且已設定好 DNS。
Web 伺服器與 DNS 關係密切,先把 DNS 搞定。本文需要能解析 station1.example1.com 與 server1.example1.com 兩個名稱。雖然寫 /etc/hosts 也能work,但這裡不鼓勵——用 DNS 才是正規做法。約定:機器 1 為 server1.example1.com,機器 2 為 station1.example1.com。
service httpd start;若失敗,查 /var/log/httpd/ 下的日誌,並確認 /etc/hosts 有類似 192.168.0.254 example1.com server1 的一行。DocumentRoot /var/www/html。https://server1.example1.com,正常會看到 Apache 的介紹頁。mkdir -p /var/www/virtual/server1.example1.com/html vi /var/www/virtual/server1.example1.com/html/index.html
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 Options Indexes Includes </VirtualHost>
host server1.example1.com。service httpd restart,失敗就看日誌,它會告訴你哪一行有問題。ScriptAlias /cgi-bin/ /var/www/virtual/server1.example1.com/cgi-bin/。#!/bin/bash echo Content-Type: text/html echo echo "<pre>" echo "My username is : $(whoami)" echo "My id is : $(id)" echo "Here is /etc/passwd :" cat /etc/passwd echo "</pre>"
chmod 555 test.sh。https://server1.example1.com/cgi-bin/test.sh,再從機器 2 驗證一次。service squid start,有問題看 /var/log/messages。acl CONNECT method CONNECT 下方加上:acl examample1 src 192.168.0.0/24並在 INSERT YOUR OWN RULE(S) HERE 處加入:
http_access allow examample1
acl otherdeny dstdomain .sina.com.cn http_access deny otherdeny重啟後測試,你會發現機器 2 仍然看得到該站——因為規則是自上而下比對,deny 寫在 allow 之後就永遠輪不到它。
http_access deny otherdeny 移到 allow 之前再重啟,封鎖才真正生效。Apache 的設定成敗與 DNS 高度相關,先把 DNS 做扎實,後面會順很多。本文用最通俗的方式帶你跑完基本配置;若要深入,还是得回到 Apache 2.0 的官方文件。