為 Apache 安裝 SSL 加密

簡介

SSL 的用途是甚麼?我需要做很多有關資料保安的工作,發現要從網絡上讀取敏感的資料是很容易的事 (例如電郵供應商 GMX, Web.de 等的密碼)。因此我開始接觸 SSL (有了 SSL 加密,要輕易讀取資料就不可能了),並撰寫了這小型的 HowTo,應該可以幫助您安裝一台 SSL 網頁伺服器。

 

1.  所需套件

 

  • apache 1.3.19 (apache_1.3.19.tar.gz)
  • apache 1.3.19 SSL-encryption (apache_1.3.19+ssl_1.44.tar.gz)
  • patch 2.5.4
  • openssl
所有套件都可以在 www.apache.org, www.openssl.orgwww.tuxfinder.com 找到。

 

2. 準備為原始碼安裝修補程式

首先要解壓 apache 的原始碼。

tar zxvf apache_1.3.19.tar.gz

然後複製 apache_1.3.19+ssl_1.44.tar.gz 套件往已建立的 apache 目錄 (apache_1.3.19/)

透過以下指令可以加密:

tar zxvf apache_1.3.19+ssl_1.44.tar.gz

假如 path 2.5.4 或以上及 openssl 還未安裝,現在就要做了,因為這些套件是 apache 支援 SSL 必須的。

 

3. 為原始碼安裝修補程式

要為原始碼安裝修補程式,先切換至 apache 目錄並執行:

./FixPatch

現在所有東西應該已安裝修補程式了,FixPatch 在找不到 openssl 時會停止執行 script。在那種情況下,您需要 FixPatch 背後指往 openssl 的路徑:

./FixPatch /opt/openssl

/opt/openssl 路徑當然視乎本機的openssl 安裝。

 

4. 安裝網頁伺服器

要安裝網頁伺服器,您要執行以下指令:

 

	 ./configure --prefix= INSTALLATIONSDESTINY 例如:/opt/apache-ssl

make

make install
網頁伺服器現在已安裝好了。

然後建立一個資料夾,這個資料夾稍後會用到:

	
mkdir /opt/apache-ssl/htdocs-ssl
chmod a+rx /opt/apache-ssl/htdocs-ssl
cd /opt/apache-ssl/
目錄的結構應該是:

 

 
/opt/
apache-ssl/
htdocs/
htdocs-ssl/
conf/
...
etc.

假如您的設定與這個相同,就可以繼續。

 

5. 保安證書

現在需要討論保安證書的設定,確保您可以繼續跟隨本 HowTo。

您可以用任何名稱代替下列範例的 "server"。

要建立證書,可看看簡短的介紹:

	1.  openssl req -new > server.cert.csr
接著您需要設定密碼 (要緊記它﹗)其他設定也與證書相關﹗ 

 

	2.  openssl rsa -in privkey.pem -out server.cert.key
要寫入 RSA key ,您需要輸入密碼。
	3.  openssl x509 -in server.cert.csr -out server.cert.cert 
-req -signkey server.cert.key -days 365

這個 key 有效期為 356 天﹗

然後您要複製兩個 keys,server.cert.keyserver.cert.cert/opt/apache-ssl/conf

建立 key 的原因會在下一點清楚解釋。

 

6. 設定

設定需要很多時間,因為頗為複雜。要令過程更簡單,我會以範例解釋設定。

設定檔案在 /opt/apache-ssl/conf

首先您要修改 httpsd.conf 檔案。
這兒您要按需要設定幾行。
如果您亦想提供未經加密的網頁,就需要將該行標為註解:

#Listen 3000
在之前加上 #。

如果 (不安全的) 網頁伺服器在 port 8080 上運行,您應該以 8080 代替 3000。

主要的改變出現在檔案結尾,在最末一行之前,您要加兩個 include 指令。

/opt/apache-ssl/conf/httpsd.conf
Include "conf/ssl.conf"
Include "conf/port-443.conf"


它們的意義會在下一部分談及。

在 httpsd.conf 完成安裝 前,您需要建立連結,連結會在稍後啟動伺服器時用到。

	 ln -s httpsd.conf httpd.conf 
那就完成了。

 

6.1 ssl.conf

在 ssl .conf 中您會找到如 gcache 程式的位置,以及它使用的 port。沒有 gcache ,apache 就不能運作。

接著要建立 ssl.conf 檔案;內容應該如下:

 

/opt/apache-ssl/conf/ssl.conf
 
SSLDisable
SSLNoCAList

SSLRandomFile file /var/tmp 1024

SSLCacheServerPath /opt/apache-ssl/bin/gcache
SSLCacheServerPort logs/gcache_port

SSLCacheServerRunDir /tmp
SSLSessionCacheTimeout 15


 

6.2 port-443.conf

別讓它的名稱嚇怕,對我而言,檔案這樣命名的原因很簡單:SSL 預設的 port 為 443,在這檔案中,你可以指定伺服器等候指令(監聽) 的 port ,以及伺服器尋找保安證書的位置。 

 

/opt/apache-ssl/conf/port-443.conf
Listen 192.168.11.45:8887

<VirtualHost 192.168.11.45:8887>
DocumentRoot /opt/apache-ssl/htdocs-ssl
SSLCertificateFile /opt/apache-ssl/conf/server.cert.cert
SSLCertificateKeyFile /opt/apache-ssl/conf/server.cert.key

SSLVerifyClient 0
SSLVerifyDepth 10

SSLBanCipher NULL-MD5:NULL-SHA
CustomLog logs/ssl_log "%t %{version}c %{cipher}c %{clientcert}c"

SSLEnable
</VirtualHost>


 

7. 啟動及測試網頁伺服器

您可以透過以下指令啟動網頁伺服器:

	 /opt/apache-ssl/bin/httpsdctl start 
現在執行 apache 已支援 SSL。

您可以建立一個 index.html 檔案,將它複製至 /opt/apache-ssl/htdocs/opt/apache-ssl/htdocs-ssl 目錄,進行測試。 

當您瀏覽以下 URLs:


http://192.168.11.45:8080
https://192.168.11.45:8887
應該會看到同樣的結果。
arrow
arrow
    全站熱搜

    Bluelove1968 發表在 痞客邦 留言(0) 人氣()