CentOS 8 設定ssl 加密及導向加密網頁 – 王元聖BLOG

CentOS 8 設定ssl 加密及導向加密網頁

現在的網頁伺服器與瀏覽器都支援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

修改

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>

重啟apache即可

資料來源:https://luyaku.pixnet.net/blog/post/351874801-centos-8.1-15%EF%BC%9A%E7%B6%B2%E9%A0%81%E4%BC%BA%E6%9C%8D%E5%99%A8%E8%88%87%E5%8A%A0%E5%AF%86%E9%80%A3%E7%B7%9A-https%3A—%E5%8D%94