Apache 安裝設置

  • 安裝Apache2
sudo apt-get install apache2 
  • 編輯/etc/apache2/apache2.conf(新安裝的系統可能在/etc/apache2/conf.d/charset)文件,在改動之前,請先將該配置文件做個備份。以便在出錯的時候可以恢復。將
AddDefaultCharset ISO-8859-1  改為  AddDefaultCharset GB2312

當然如果你網站的編碼用的是UTF-8的話,也可以改為

AddDefaultCharset UTF-8

這樣的話,我們就不會每次打開網頁都是亂碼了!

增加安全模塊

?????? 增加安全模塊,以保障Apache服務的正常運行,現在我們安裝mod-security。(可選)

首先安裝libapache2-mod-security包

sudo apt-get install libapache2-mod-security

該模塊默認是沒激活的,我們可以在/etc/apache2/mods-available目錄下看到有mod-security.load文件,但在/etc/apache2/mods-enabled目錄卻沒有它的軟鏈接。我們現在激活它:

$sudo ln -s /etc/apache2/mods-available/mod-security.load /etc/apache2/mods-enabled/mod-security.load

$sudo cp /usr/share/doc/libapache2-mod-security/examples/httpd2.conf.example-full /etc/apache2/mods-available/mod-security.conf

$sudo cp /etc/apache2/mods-available/mod-security.conf /etc/apache2/mods-available/mod-security.conf.orig

$sudo vi /etc/apache2/mods-available/mod-security.conf

==== mod-security.conf 文件內容開始====

# 檢測內容長度以避免堆溢出攻擊

SecFilterForceByteRange 32 254 =>SecFilterForceByteRange 32 126

# debug設置

SecFilterDebugLevel 9 =>SecFilterDebugLevel 0

# 設置缺省的動作

SecFilterDefaultAction "deny,log,status:499" =>SecFilterDefaultAction "deny,log,status:404"

# 把設置傳遞給子目錄

SecFilterInheritance Off

# Redirect user on filter match

# 當匹配sh的時候,重新定向到一個特殊的警告頁面,該頁面是自行編寫的,寫些警告的話讓攻擊者知難而退,該段先不要生效,等到相關配置配好之后再失效不遲。記住在配好之后要使之生效。

#SecFilter sh redirect:http://localhost/hack/warning.htm

# Prevent OS specific keywords

#過濾一些敏感的東西,我們使用*是為了攻擊者使用/etc/./passwd來繞開檢測

SecFilter /etc/passwd =>SecFilter /etc/*passwd

SecFilter /bin/*sh

# Very crude filters to prevent SQL injection attacks

# 防止SQL插入(SQL Injection)攻擊

????? SecFilter "delete[[[space|space]]]+from" SecFilter "insert[[[space|space]]]+into" SecFilter "select.+from" SecFilter "select[[[space|space]]]+from" SecFilter "union[[[space|space]]]+from"

==== mod-security.conf 文件內容結束====

sudo ln -s /etc/apache2/mods-available/mod-security.conf /etc/apache2/mods-enabled/mod-security.conf

重啟Apache2服務即可。

sudo /etc/init.d/apache2 restart

備注:第三步可能會引起部分網站不能正常運行,可以參照著去掉某些限制,由于是安全模塊,所以參照的是防火墻的做法,關掉一切不安全的,再根據需要打開必要的。

測試環境
  • 操作系統:Ubuntu 9.0.4
  • 測試機地址:10.39.6.59
  • 測試機域名:*.firehare.com

基本配置

我們都知道,如果我們想在單臺機器上設置多個域名或主機名時,我們就要用到基于名稱的虛擬主機了。那么要如何進行設置呢?這就是本指南想解決的問題 了。在 Ubuntu 的 /etc/apache2/ 目錄下有個 Apache2 的主配置文件 apache2.conf。在該文件中我們可以看到有這么一行內容:

Include /etc/apache2/sites-enabled/[^.#]*

這行的意思表明該文件包含了 /etc/apache2/sites-enabled/ 目錄中文件名不含 "." 或 "#" 這兩個字符的所有文件。而當我們列出該目錄的文件時,發現只有一個 000-default 的軟鏈接文件,實際連接的是 /etc/apache2/sites-available 目錄中的 default 文件,不難看出該文件的文件名中并不包含 "." 或 "#"。所以這個文件當然是要被配置文件 apache2.conf 所包含的了。打開該文件,發現它其實是一個虛擬主機的配置文件,不過由于該文件中的虛擬主機為 *,所以它實際上是一個通用配置文件。如果我們要建立虛擬主機的話,那么就要把該文件改成如下所示:

NameVirtualHost 10.39.6.59
<VirtualHost 10.39.6.59>
ServerName www.firehare.com
ServerAdmin ubuntu.firehare@gmail.com

DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined
ServerSignature On

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0?::1/128
</Directory>

</VirtualHost>

下面我們來分析一下上面這段設置中與虛擬主機有關的設置語句:

  • `NameVirtualHost 10.39.6.59`:表示我們要做的是一個基于名稱的虛擬主機,且其 IP 地址為 10.39.6.59
  • `<VirtualHost 10.39.6.59> 和 </VirtualHost>`:表示在其中的是一個虛擬主機的配置
  • `ServerName www.firehare.com`:設置虛擬主機的域名
  • `ServerAdmin ubuntu.firehare@gmail.com`:設置該虛擬主機網管員的郵件
  • `DocumentRoot /var/www/`:設置該虛擬主機的主目錄路徑
  • `ErrorLog /var/log/apache2/error.log`:設置該虛擬主機的出錯信息
  • `CustomLog /var/log/apache2/access.log combined`:設置該虛擬主機的訪問信息

這樣我們就配置了一個虛擬主機 www.firehare.com。但由于這是缺省配置,所以在 Apache2 重啟之后,無論你輸入 DNS 服務器中指向這個主機的任何域名,都會被導向 www.firehare.com 這個缺省配置所指向的 /var/www 這個目錄的。除非該域名被其他虛擬主機配置所用,比如我們還配置了 edunuke.firehare.com 指向本機,且配置了相應的虛擬主機,這樣的話,輸入域名 edunuke.firehare.com 就會被對應該域名的目錄中。

進一步說明

為了說明清楚 我們再添加一個虛擬主機站點 edunuke.firehare.com,首先到 /etc/apache2/sites-available/ 目錄中建立一個文件 edunuke。當然這個文件名中是沒有 "." 或 "#" 這兩個字符的了。然后編輯該文件:

<VirtualHost 10.39.6.59>
ServerName edunuke.firehare.com
ServerAdmin ubuntu.firehare@firehare.com
DocumentRoot "/var/www/edunuke/"
ErrorLog "/var/log/apache2/edunuke_errors.log"
CustomLog "/var/log/apache2/edunuke_accesses.log" common
</VirtualHost>

設置的具體含義同上面的相似,這是我就不再多說了。然后再運行命令:

sudo a2ensite edunuke

這樣的話,虛擬主機站點 edunuke.firehare.com 就已經安裝好了。這時你也可以在 /etc/apache2/sites-enabled/ 目錄中發現多了一個到 /etc/apache2/sites-available/edunuke 的軟鏈接。接下來就是將 Apache2 重啟來使虛擬主機站點運行起來:

sudo /etc/init.d/apache2 restart  這里可以使用reload 重新加載

這樣你在瀏覽器上輸入 edunuke.firehare.com 的話,就會被指向 /var/www/edunuke 目錄了,而輸入其他指向本機的域名則都會指到缺省配置中的 /var/www 目錄中。熟悉 Apache2 的朋友會問為什么這樣麻煩,放在一個文件中不也是可以嗎?為什么要用兩個文件呢?其實很簡單,因為如果我要對 edunuke 站點進行維護時,我只要運行命令:

sudo a2dissite edunuke
sudo /etc/init.d/apache2 restart

即可,這樣既可以維護 edunuke 這個站點,同時還不影響其他站點的正常運行。

高級配置

上面談了一下簡單的虛擬主機配置方法。這個基本上能滿足我們大部分的需要。但如果要是安裝 Zope+Plone 的話,上面的這點設置是遠遠不夠的,由于 Zope+Plone 結構所采用的端口并非是80端口,所以我們還得做端口重定向。為了能夠做這個,我們得激活 Rewrite 和 Proxy 兩個模塊。激活模塊很簡單,同站點配置目錄一樣,在 Apache2 中也有兩個模塊配置目錄:mods-available 和 mods-enabled。在 mods-available 目錄中的是所有可用的模塊,而在 mods-enabled 目錄中的則是已被安裝到 Apache2 中的模塊。由于在 mods-available 目錄中已經有了 Rewrite 和 Proxy 模塊的配置引導文件,所以只需要簡單地將其安裝到 Apache2 中即可。使用命令:

sudo a2enmod rewrite
sudo a2enmod proxy

然后,添加虛擬主機站點 plone.firehare.com,同 edunuke 站點創建相似在/etc/apache2/sites-available/ 目錄中建立一個文件 plone。顯然這個文件名中是沒有 "." 或 "#" 這兩個字符的了。然后編輯該文件:

<VirtualHost 10.39.6.59>
ServerName plone.firehare.com
ServerAdmin ubuntu.firehare@firehare.com
ErrorLog "/var/log/apache2/plone_errors.log"
CustomLog "/var/log/apache2/plone_accesses.log" common

RewriteEngine on
RewriteRule ^/(.*) http://127.0.0.1:8081/VirtualHostBase/http/plone.firehare.com:80/plone/VirtualHostRoot/$1 [L,P]

<Proxy *>
Order Deny,Allow
Deny from all
Allow from all
</Proxy>

</VirtualHost>

這樣就安裝好了 plone.firehare.com 虛擬主機站點,可以在瀏覽器中地址欄中輸入 http://plone.firehare.com 就可以重定向到 Zope+Plone 站點去了。