主機認證方式(Challenge)
透過Challenge來驗證你是否擁有你所申請的網域。
DNS:將指定文字利用 TXT 紀錄放在 DNS 的主機名稱 _acme-challenge.<YOUR_DOMAIN> 底下。
HTTP:將包含 token 和帳號金鑰指紋的檔案,放到網頁伺服器中 http://<YOUR_DOMAIN>/.well-known/acme-challenge/<TOKEN> 的位置
以下方式為透過HTTP進行主機認證與憑證申請,且使用的是nginx,如果使用的是apache,請將nginx關鍵字替換成httpd
網站未來將透過 HTTPS(Port 443)提供服務,請在防火牆端先設定 HTTPS(Port 443) 放行。
※需使用 root 權限進行以下指令
設定防火牆放行 https
firewall-cmd --zone=public --permanent --add-service=https
防火牆重新載入
firewall-cmd --reload
安裝 SSL 模組
yum install -y mod_ssl
安裝 Certbot
yum install -y certbot
如果出現「 No package certbot available」表示需要先安裝擴充資源庫
安裝擴充資源庫
yum install -y epel-release
透過 certbot 指令到 Let's Encrypt 進行驗證,完成後憑證會產生在「 /etc/letsencrypt/live/網域名稱 」
連線測試
certbot --dry-run certonly --webroot -w /var/www/html -d 網域名稱 --email 電子郵件信箱 --agree-tos
測試無誤會回傳: The dry run was successful.
申請憑證
certbot certonly --webroot -w /var/www/html -d 網域名稱 --email 電子郵件信箱 --agree-tos
列出所有憑證及到期日
certbot certificates
測試是否能更新
certbot renew --dry-run
手動立即更新SSL憑證
certbot renew
驗證 Certbot 是否有自動更新
service status certbot.timer
建立自動更新批次檔
nano /root/certbot_renew.sh
內容
自動更新憑證、並且重新載入nginx →如果使用apache,reload的目標就必須修改為httpd或apache
#!/bin/sh
/usr/bin/certbot renew --quiet --agree-tos --post-hook "systemctl reload nginx"
賦權
chmod 755 /root/certbot_renew.sh
切換至root權限並設定排程
sudo -s
crontab -e
加入排程在每個星期六凌晨二點檢查更新,並且將執行的結果都紀錄到 certbot_renew.log
0 2 * * 6 /root/certbot_renew.sh >> /var/log/certbot_renew.log 2>&1
Certbot 只有在憑證到期前一個月才會進行更新,如果憑證尚未到期是不會更新的。
列出目前的排程清單
crontab -l
建立 /etc/systemd/system/certbot.service SystemD 服務
nano /etc/systemd/system/certbot.service
內容
[Unit]
Description=Renew Let's Encrypt certificates
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --renew-hook "/bin/systemctl --no-block reload nginx" --quiet --agree-tos
建立/etc/systemd/system/certbot.timer SystemD 計時器以每天更新證書,包括隨機延遲,以便更新請求分散在一天中。
nano /etc/systemd/system/certbot.timer
內容
[Unit]
Description=Daily renewal of Let's Encrypt's certificates
[Timer]
OnCalendar=daily
RandomizedDelaySec=1day
Persistent=true
[Install]
WantedBy=timers.target
啟動並啟用certbot.timer
systemctl daemon-reload
systemctl start certbot.timer
systemctl enable certbot.timer
結果:Created symlink from /etc/systemd/system/timers.target.wants/certbot.timer to /etc/systemd/system/certbot.timer.
使用以下命令檢查時間是否處於活動狀態
systemctl list-timers certbot.timer
編輯 SSL 設定檔
nano /etc/httpd/conf.d/ssl.conf
修改 SSLCertificateFile 、 SSLCertificateKeyFile 、 SSLCACertificateFile 檔案路徑
SSLCertificateFile /etc/letsencrypt/live/網域名稱/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/網域名稱/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/網域名稱/fullchain.pem
編輯 Apache 設定檔
nano /etc/httpd/conf/httpd.conf
增加配置內容,本範例以<Directory "/var/www/html">為例
<Directory "/var/www/html">
…
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
…
</Directory>
存檔以後重新載入 Apache
service httpd reload
Certbot 簽發新的憑證後,沒有呼叫 Nginx 進行 reload 來載入新的憑證,導致 Nginx 繼續用舊憑證
Step 1:新增更新掛勾設定
nano /etc/letsencrypt/renewal-hooks/post/01-nginx-reload
Step 2:加入指令更新憑證之後重新載入nginx
systemctl reload nginx
Step 3:賦權
chmod +x 01-nginx-reload
Step 4:測試
certbot renew --dry-run
最後一行會出現「Running post-hook command: /etc/letsencrypt/renewal-hooks/post/01-nginx-reload」即可
yum install python-certbot-apache
CentOS 8 已於 2021 年結束支援,轉為 CentOS 8 Stream 與其他後繼版本,原先 CentOS 8 的 Repo 已轉移至 vault.centos.org,使用以下指令即可轉換更新源。
cd /etc/yum.repos.d/
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*