現在的網頁伺服器與瀏覽器都支援SSL加密的傳輸協定來提高網路安全性,我們自己架設的網站就必須自己簽署一份金鑰才能進行加密傳輸。
首先先安裝相關需要的工具,執行以下指令安裝網頁伺服器的SSL模組與 openssl 認證工具:
yum install mod_ssl openssl
建立金鑰
先進入憑證所需的目錄
cd /etc/pki/tls/certs
產生私鑰
openssl genrsa -out ca.key 2048
產生 CSR
openssl req -new -key ca.key -out ca.csr
產生自我簽署的金鑰
預設憑證有效日期是365天,也就是一年,如果覺得憑證效期太短,可以改成:
openssl x509 -req -days 3650 -in ca.csr -signkey ca.key -out ca.crt
將key檔案搬到 /etc/pki/tls/private/
cp ca.key /etc/pki/tls/private/ca.key
範例中例的ca.key ca.csr ca.crt 只是示範,檔名可以隨自己喜好,例如bell.ley bell.csr bell.crt,只要後面設定時能正確對應即可。
這時候相關憑證已正確l建立完畢,接下來修改ssl設定檔,
編輯 /etc/httpd/conf.d/ssl.conf
修改
openssl req -new -key ca.key -out ca.csr
產生自我簽署的金鑰
預設憑證有效日期是365天,也就是一年,如果覺得憑證效期太短,可以改成:
openssl x509 -req -days 3650 -in ca.csr -signkey ca.key -out ca.crt
將key檔案搬到 /etc/pki/tls/private/
cp ca.key /etc/pki/tls/private/ca.key
範例中例的ca.key ca.csr ca.crt 只是示範,檔名可以隨自己喜好,例如bell.ley bell.csr bell.crt,只要後面設定時能正確對應即可。
這時候相關憑證已正確l建立完畢,接下來修改ssl設定檔,
編輯 /etc/httpd/conf.d/ssl.conf
修改
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
以及拿掉註解
# line 43: uncomment
DocumentRoot "/var/www/html"
# line 44: uncomment and specify hostname
ServerName www.example.com:443
重啟apache即可
——————————————————————————————————————————————————————
將http的連線導向https
[root@www ~]#vi /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.example.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>