前言
前文 樹莓派/Debian Apache2 實現 HTTPS(SSL) 服務 提到,Apache2 實現 HTTPS(SSL) 服務有兩種方法以及之間的區別,這里講述如何通過 騰訊雲 申請免費 SSL 證書實現 HTTPS(SSL) 服務。
注:本來打算使用阿里雲的 SSL 證書服務,但是實在太慢了,改用騰訊雲。
准備
-
已有域名且實名
-
騰訊雲賬號
-
LAMP 服務器環境
其他環境(Nginx等)的 SSL 證書同時下發,可以自己搜索配置方法。
申請免費 SSL 證書
-
在 “騰訊雲 -> SSL 證書 -> 申請免費證書”
-
“確認證書類型 -> TRUSTASIA 免費版 DVSSL 證書”
-
填寫免費證書申請表單
-
域名身份驗證
由於我是阿里雲的服務器,我選擇“手動DNS驗證”,騰訊雲的應該可以選擇“自動DNS驗證”。
-
“溫馨提示 -> 查看證書詳情”
-
將證書信息填寫到域名解析:“添加記錄”
-
回到騰訊雲證書詳情頁,點擊 “自動診斷” 然后 “驗證”
-
然后回郵件通知審核通過
下載免費 SSL 證書
-
點擊頒發下載
-
將壓縮包通過 WinSCP 等軟件傳輸到服務器上
-
解壓壓縮包
騰訊雲一般是zip格式的壓縮包,輸入解壓命令即可:
unzip xxxxx.zip
配置 Apache
編輯 SSL 配置文件 default-ssl.conf
-
編輯 SSL 配置文件 default-ssl.conf
sudo vim /etc/apache2/sites-enabled/default-ssl.conf
如果忽略注釋會顯示:
<IfModule mod_ssl.c> <VirtualHost _default_:443> ServerAdmin webmaster@localhost DocumentRoot /var/www/html SSLEngine on SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> </VirtualHost> </IfModule>
-
添加
ServerName <域名>
,例:ServerAdmin webmaster@localhost ServerName www.example.com # 你的域名 DocumentRoot /var/www/html
修改加載 SSL 證書位置到你解壓的證書文件夾 ,例:
SSLCertificateFile /etc/apache2/ownSSL/Apache/2_www.xxxxx.xxx.crt SSLCertificateKeyFile /etc/apache2/ownSSL/Apache/3_www.xxxxx.xxx.key
並添加
SSLCertificateChainFile /etc/apache2/ownSSL/Apache/1_root_bundle.crt
-
:wq
保存退出
加載 SSL 配置文件 default-ssl.conf
-
以 root 權限啟用SSL模塊:
sudo a2enmod ssl
如果執行成功顯示:
Considering dependency setenvif for ssl: Module setenvif already enabled Considering dependency mime for ssl: Module mime already enabled Considering dependency socache_shmcb for ssl: Enabling module socache_shmcb. Enabling module ssl. See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates. To activate the new configuration, you need to run: systemctl restart apache2
-
它最后提醒你執行一條重啟命令:
sudo /etc/init.d/apache2 restart
這里要以 root 權限執行這條命令,否則報錯。
注意:這里重啟了 Apache2。
-
以 root 權限執行命令:
Copysudo a2ensite default-ssl
正確返回結果:
Copyyogile@debyogile:/etc/apache2# sudo a2ensite default-ssl a2ensite default-sslEnabling site default-ssl. To activate the new configuration, you need to run: systemctl reload apache2
它提醒執行
systemctl reload apache2
,先不管它,進行下一步 啟動 Apache2 服務 。 -
這時 啟動 Apache2 服務 :
sudo /etc/init.d/apache2 start
啟動成功顯示:
[ ok ] Starting apache2 (via systemctl): apache2.service.
-
啟動 Apache2 服務成功后,加載 SSL 配置文件 default-ssl.conf
sudo systemctl reload apache2
成功無顯示。
重啟 Apache2 服務
-
輸入命令重啟 Apache2 服務
Copysudo /etc/init.d/apache2 restart
成功會顯示:
Copy[ ok ] Restarting apache2 (via systemctl): apache2.service.
-
配置成功。
網頁登錄測試
在瀏覽器中輸入 https://<域名>
即可驗證
HTTP 強制重定向 HTTPS
-
啟動重定向
sudo a2enmod rewrite
重啟 Apache 服務
sudo systemctl restart apache2
-
在
/etc/apache2/sites-enabled/000-default.conf
文件的<VirtualHost *:80></VirtualHost>
中寫入以下內容RewriteEngine on RewriteCond %{HTTPS} !=on RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R=301]
重啟 Apache 服務
sudo systemctl restart apache2