woff 發表於 2014-11-17 16:31:48

CentOS 設置一個SSL 加密的網頁服務器

這個指引會解釋如何設置一個支持https 的網站。這個教程使用一個自我簽署的金鑰,因此它適用於個人網站或作測試用途。這個指引並未經修訂,因此請自行承擔風險,並進行備份!
1. 取得所需的軟件你需要為一台SSL 加密的網頁服務器預備數件東西。視乎你的安裝,你可能並未安裝OpenSSL 或mod_ssl,Apache 連接到OpenSSL 的界面。如果你有需要,請用yum 來安裝它們。yum install mod_ssl opensslyum 會告訴你它們已經安裝,或者為你安裝它們。
2. 產生一張自我簽署的憑證我們將會利用OpenSSL 來產生一張自我簽署的憑證。如果你在一台生產用的服務器上做這個動作,你應該會想從一個被信賴的憑證機構取得一條金鑰,但假若你只是用在一個私人網站上或作測試之用,自我簽署的憑證已經足夠了。要創建金鑰,你必須是root 用戶,因此你可使用su 變為root 用戶,或在指令前面運用sudo# 產生私鑰openssl genrsa -out ca.key 2048# 產生CSRopenssl req -new -key ca.key -out ca.csr# 產生自我簽署的金鑰openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt# 複製文件至正確位置cp ca.crt /etc/pki/tls/certs
cp ca.csr /etc/pki/tls/certs
cp ca.key /etc/pki/tls/certs
http://wiki.centos.org/ArtWork/WikiDesign?action=AttachFile&do=get&target=icon-admonition-alert.png警告:如果你採用SELinux,請確保你複製這些文件而不是遷移它們。否則Apache將會投訴關於違漏了的憑證檔,因為它不能讀入這些擁有錯誤SELinux脈絡的憑證檔。

假如你遷移了這些文件而不是複制它們,你可以用以下的指命來矯正這些文件的SELinux 脈絡,因為/etc/pki/* 的正確脈絡定義已包含在SELinux 政策裡。restorecon -RvF /etc/pki接著我們須要更新Apache SSL 的配置文件vi +/SSLCertificateFile /etc/httpd/conf.d/ssl.conf請修改路徑至金鑰文件的存儲位置。如果你採用上面的方法,這會是SSLCertificateFile /etc/pki/tls/certs/ca.crt然後在再低數行的位置為憑證金鑰文件設置正確路徑。如果你按照上面的指引,這會是:SSLCertificateKeyFile /etc/pki/tls/private/ca.key存儲及離開文件,然後重新引導Apache/etc/init.d/httpd restart假若一切正常的話,你現在應該可以通過https 連接到你的服務器,並看見CentOS 的缺省頁面。由於憑證是自我簽署的,瀏覽器一般會徵詢你是否接納這個憑證。
3. 設置虛擬主機一如你為http在端口80上設立VirtualHost,你亦可為https在端口443上作樣似的設置。一個在端口80上的網站的典型VirtualHost有如下樣子<VirtualHost *:80>
      <Directory /var/www/vhosts/yoursite.com/httpdocs>
      AllowOverride All
      </Directory>
      DocumentRoot /var/www/vhosts/yoursite.com/httpdocs
      ServerName yoursite.com
</VirtualHost>要在端口443 上增加一個姊妹網站,你需要在你的文件頂部加入下列內容NameVirtualHost *:443然後再加入一個類似如下的VirtualHost記錄:<VirtualHost *:443>
      SSLEngine on
      SSLCertificateFile /etc/pki/tls/certs/ca.crt
      SSLCertificateKeyFile /etc/pki/tls/certs/ca.key
      <Directory /var/www/vhosts/yoursite.com/httpsdocs>
      AllowOverride All
      </Directory>
      DocumentRoot /var/www/vhosts/yoursite.com/httpsdocs
      ServerName yoursite.com
</VirtualHost>利用這個指令重新引導Apache/etc/init.d/httpd restart4. 設置防火牆你現在應該擁有一個以自我簽署憑證來支持https 的網站。如果你未能連接,你或許需要在防火牆上打開端口。要這樣做,請更改你的iptables 規則:iptables -A INPUT -p tcp --dport 443 -j ACCEPT
/sbin/service iptables save
iptables -L -vTranslation of revision 7
當出現錯誤訊息Invalid command 'SSLEngine', perhaps misspelled or defined by a module not included in the server configuration


結果發現是少Load Modules
找到
vi /etc/httpd/conf/httpd.conf
加入LoadModule ssl_module modules/mod_ssl.so然後/etc/init.d/httpd restart
頁: [1]
查看完整版本: CentOS 設置一個SSL 加密的網頁服務器