歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> Unix基礎知識 >> 在FreeBSD上配置Apache+SSL

在FreeBSD上配置Apache+SSL

日期:2017/2/28 10:24:15   编辑:Unix基礎知識
FreeBSDApache SSL(Secure Socket Layer)是一種在讓可持有證書的浏覽器軟件(比如Internet Explorer、Netacpe Navigator)和WWW服務器(如Netscape的Netscape Enterprise Server 、ColdFusion Server等等)之間構造的安全通道中傳輸數據的協議,他運行在TCP/IP層之上、應用層之下,為應用程序提供加密數據通道。
  Apache,眾所周知,是UNIX世界最為流行的WWW服務器軟件,性能好,可擴展性強。同時他也有相應的Win32版本。
  在訪問一些網站時,我們會注意到它的URL不是一般的HTTP://aa.bb.cc ,而是https://aa.bb.cc 這表明網站提供了HTTPS連接功能,即SSL over HTTP. 這保證了客戶端與服務器的通信都是在SSL建立的加密通道中進行,絕對安全。為了讓Apache服務器支持https,那麼就必須配置mod_ssl模塊。下文就開始就Apache+mod_ssl on FreeBSD進行討論。

配置Apache服務器

  首先,如果您的FreeBSD系統已經配置好Apache服務器,那麼最好先備份一下,起碼要把Apache的配置文件作一備份。如果需要直接往現有的安裝上配置SSL支持,請閱讀文章的最後一部分。
  · 安裝port
  port的安裝非常方便,由於筆者已經安裝好了所有的port 框架,所以只需要 cd /usr/ports/www/apache13-modssl make install
  這樣就安裝好了Apache的二進制文件,但是這還不夠。
  · Apache以什麼方式運行?
  比如,筆者需要Apache以www用戶組的www用戶運行(這也是出於安全性考慮)。如果發現針對apache的入侵,那麼這只是針對這個組/用戶的。所以還是推薦這樣配置用戶。
可以用adduser命令添加用戶,也可以直接vipw,添加:
www:*:99:99::0:0:apache Daemon:/nonexistent:/sbin/nologin
同時需要添加www組,在/etc/group裡面添加一行
www:*:99
  · 配置文件
以下都需要在apache的配置文件中進行改變。缺省條件下,它是/usr/local/etc/apache/apache.conf

  下列表格是筆者改動的一些設置



 
  · 測試配置
  配置好apache以後,運行# /usr/local/sbin/apachectl configtest Syntax OK
  表示已經工作正常
  · 啟動和重起服務器
  現在,一切已經工作正常,可以啟動apache服務器
  # /usr/local/sbin/apachectl startssl /usr/local/sbin/apachectl startssl: httpd started
  啟動可能需要一些時間,因為需要產生隨機數。
  如果你又做了一些配置上的改動,需要重起服務器,可以
  # /usr/local/sbin/apachectl graceful /usr/local/sbin/apachectl graceful: httpd gracefully restarted
  · 校錯
  如果啟動出現問題,那麼最好去查閱log文件, 比如
  # tail apache_ssl_engine_log
  [notice] SIGUSR1 received. Doing graceful restart
[notice] Apache/1.3.14 (Unix) mod_ssl/2.7.1 OpenSSL/0.9.4
configured -- resuming normal operations
然後,看看SSL引擎的log
# tail apache_ssl_engine_log
[info] Init: 1st restart round (already detached)
[info] Init: Reinitializing OpenSSL library
[info] Init: Seeding PRNG with 1160 bytes of entropy
[info] Init: Configuring temporary RSA private keys (512/1024 bits)
[info] Init: Configuring temporary DH parameters (512/1024 bits)
[info] Init: Initializing (virtual) servers for SSL
[info] Init: Configuring server new.host.name:443 for SSL protocol
[warn] Init: (new.host.name:443) RSA server certificate CommonName
(CN) `www.snakeoil.dom' does NOT match server name!?
  問題就一幕了然了,其中snakeoil是安裝的時候缺省的證書,以後會用我們自己的證書來代替之。
  · 別忘了自己的防火牆
  如果你的機器是在防火牆後面,別忘了讓防火牆允許對443端口的訪問,這就是https使用的端口。
  · 浏覽一下
  然後在浏覽器裡面輸入http://server server是你的服務器的IP地址,然後會出現以下的字樣:
   Hey, it worked !
   The SSL/TLS-aware Apache webserver wassuccessfully installed on this website.
  然後同樣的試試https://server, 應該也有同樣的字眼。
  · 獲取一個證書
  下面描述的是怎樣產生自己的一個測試性證書。這個證書不能公開使用,但是怎樣獲得一個可以公開使用的證書就不在本文所講述的范圍了。
  利用下列命令得到一個測試證書
  # cd /usr/ports/www/apache13-modssl
  # make certificate
  筆者已經改動了所能夠看懂的所有名稱,其他的都跟缺省的相同,然後會在下列目錄產生一個證書文件
/usr/ports/www/apache13-modssl/work/apache_1.3.14/conf
  應該可以找到兩個子目錄,ssl.crt和ssl.key。這些目錄需要拷貝到apache的根目錄,但是之前筆者還是對原來的文件做了一個備份。
# cd /usr/local/etc/apache
# mv ssl.crt ssl.crt-default
# mv ssl.key ssl.key-default
  然後把剛才生成的文件拷貝過去
# cd /usr/ports/www/apache13-modssl/work/apache_1.3.14/conf
# cp -rp ssl.key /usr/local/etc/apache
# cp -rp ssl.crt /usr/local/etc/apache
  然後就需要重起apache,但是筆者剛開始用的是graceful restart,但是沒能讀取新的證書。於是就做了stop然後start.下面是屏幕輸出
# /usr/local/sbin/apachectl startssl
Apache/1.3.14 mod_ssl/2.7.1 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide us with the pass phrases.

Server new.host.name:443 (RSA)
Enter pass phrase:

Ok: Pass Phrase Dialog successful.
/usr/local/sbin/apachectl startssl: httpd started
  這樣就好了。同時筆者還在生成證書的時候加上了一個passphrase。WWW服務器啟動的時候需要它。
  · 取消passphrase
如果後來你有不想要passhprase了,那麼也可以取消它。可以參閱http://www.modssl.org/docs/2.7/ssl_faq.html#ToC31 然而取消passphrase會有一些安全問題,上面的url裡面也提到了,可以根據需要來斟酌。
  · 確認安全問題
  為了確認我的https連接確實是被經過加密的,於是在Netscape裡面我查閱了View-->Page Info,裡面有下列字樣:
  
  所以https連接確實是加密的。
  · 筆者遇到的問題
  當我試圖在Netscape 4.74裡面訪問https://server時,下面的對話框冒了出來:When I tried to browse to the website from Netscape 4.74, I was created with the following message box:
SSL has recieved an error from the server indicating an incorrect Message Authentication Code. This could indicate a network error, a bad server implementation, or a security violation.
然後在/var/log/apache_error_log發現了下面的錯誤:
[error] mod_ssl: SSL handshake failed (server new.host.name:443,
client 10.0.0.99) (OpenSSL library error follows)
[error] OpenSSL: error:0407106B::lib(4) :func(113) :reason(107)
[error] OpenSSL: error:04065072::lib(4) :func(101) :reason(114)
[error] OpenSSL: error:1408F071::lib(20) :SSL3_GET_RECORD:bad
mac decode [Hint: Browser still remembered details of a
re-created server certificate?]
  而在IE5.5裡面卻沒有問題,實在很令人困擾。更好笑的是,第二天用Netscape再次訪問的時候卻好了!我想大概是因為我重起了Netscape的進程,當沒有重起的時候Netscape認為這是個新的證書,對自己不適用,重起以後就認得了(Netscape確實雄風不再,唉)。
  · 在已有的安裝上配置ssl
  筆者開始時就嘗試在已有的apache上安裝SSL,按照以上的步驟。但是不幸的是,竟然把以前的apache也搞崩潰了。然後采取的辦法就是:
  · 為配置文件作一備份
  · 卸載所有apache,ssl和php(從pkg_info的輸出查看一下,然後用pkg_delete卸載)
  · 按照上面的步驟重新來過
  · 安裝mod_php4

Copyright © Linux教程網 All Rights Reserved