之前主機還在 Bluehost 上時,雖然申請了 Let's Encrypt 的憑證,但始終無法順利的將憑證掛上,前陣子將主機搬到 Google Cloud Platform 之後,在同事的推薦下改用 Certbot 來部署,終於順利完成,將主要的重點筆記一下。

參考 Cerbot 上 CentOS 6 的教學,可以選定 Web 服務器跟作業系統,我使用的是 Apache 跟 CentOS 6。

部署指令很簡單:

step 1. 抓取安裝檔

wget https://dl.eff.org/certbot-auto

step 2. 設定權限讓 certbot-auto 可執行

chmod a+x certbot-auto

step 3. 進行 certbot-auto 的安裝,安裝時若所有網域都要憑證,可以用空白鍵,或是選某個數字對單一網域安裝憑證

sudo ./certbot-auto --apache

step 4. 安裝完畢後,憑證的路徑會放在 /etc/letsencrypt/live/xxx.xxx.xxx.xxx/的路徑下
會有四個檔案(cert.pem、privkey.pem、chain.pemvi、fullchain.pem)
若無權限進入資料夾,可以先下 sudo 指令(sudo 無法直接搭配 cd 指令):

sudo -i
cd /etc/letsencrypt/live/xxx.xxx.xxx.xxx/

step 5. 若有使用到 Virtual Host,需手動設定 Config 檔
先設定 ssl.conf(CentOS 6的路徑是 /etc/httpd/conf.d/ssl.conf)
確認下列資訊有設定在檔案中

LoadModule ssl_module modules/mod_ssl.so
#
# When we also provide SSL we have to listen to the
# the HTTPS port in addition.
#
NameVirtualHost *:443
Listen 443

step 6. 設定 httpd-vhosts.conf 可以設定多組網域,[]需自行改為<>

[virtualhost *:443]
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/xxx.xxx.xxx/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/xxx.xxx.xxx/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/xxx.xxx.xxx/chain.pemvi
SSLCACertificateFile /etc/letsencrypt/live/xxx.xxx.xxx/fullchain.pem
ServerName xxx.xxx.xxx
ServerAlias xxx.xxx.xxx
ServerAdmin xxx@xxx.xxx.xxx
DocumentRoot "/xxxx/xxx/xxx/xxx"
Options FollowSymLinks Indexes AllowOverride All Order Allow,Deny Allow from all
AddType application/x-httpd-php .php3 .php
DirectoryIndex index.html index.htm index.php index.asp index.aspx index.jsp index.jspa index.shtml index.shtm
[/virtualhost]

step 7. 重啟 Apache 服務

apachectl restart

若重啟後報錯找不到憑證,可以到憑證位置用 chmod 修改憑證權限
例:

chmod 755 cert.pem
chmod 755 privkey.pem
chmod 755 chain.pem
chmod 755 fullchain.pem

step 8. 利用 .htaccess 來將網站從 http 改寫到 https,.htaccess 檔案要放在根路徑下

Options -Indexes


RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

order allow,deny allow from all

step 9. 用 Chrome 開啟網址,確認網址顯示是否安全,若為否,用 Chrome 的「檢查」(呼叫開發人員工具),查看是否有混和內容
改用 https 連線後,頁面載入的內容,都必須改為 https 的連線方式來取得,像圖檔、js、css等都是,透過工具可以看到報錯內容,修正後即會顯示安全

step 10. 用 crontab 來自動更新憑證,Let's Encrypt 憑證每三個月會失效,需更新
加入下列這一行,設定每個月1號跟15號的凌晨三點,自動檢查憑證(編輯這個檔案通常是針對系統的例行性任務)

vi /etc/crontab
00 3 1,15 * *  root ./certbot-auto renew

若是要使用 crontab -e 的指令也可以(通常這個指令是針對使用者的例行性任務),兩者選其一做就行

crontab -e
00 3 1,15 * *  root ./certbot-auto renew

以上為使用 Certbot 自動部署 Let's Encrypt 憑證,在 GCP 的 CentOS 6的 Apache服務(含憑證自動展延)的筆記。

2017-08-12  -  duncan Email  -  4661  -  資訊工程 - 讀者回應