SSL(Certbot)
- UtilsSSLCertbotLet's EncryptUbuntu作者:VincentCorgi約 1 分鐘閱讀
功能
說明在 Ubuntu 上用 Certbot 申請/續期 Let's Encrypt 憑證(含手動續約流程、常用 certbot 子指令與頻率限制提醒)。
情境
- 自有網域要啟用 HTTPS、或憑證將到期要 續期;在無法自動排程時依 Flow 手動跑一遍。
每個網域每週最多申請 5 次。
Flow:手動續約
Code
# 1. 安裝
sudo apt install certbot
# 2. DNS 驗證(需在 DNS 加 <domain> & *.<domain>)
sudo certbot certonly --manual \
--preferred-challenges dns \
--email <email> \
--agree-tos \
--no-eff-email \
-d "*.<domain>" \
-d "<domain>"
# 憑證會在 /etc/letsencrypt/live
# 3. 建立 nginx.conf 後 docker 開 nginx、測試、設定 crontab 自動續期
docker run -d --name <container_name> --restart unless-stopped -p <port>:<port> \
-v <nginx_conf_path>:/etc/nginx/nginx.conf:ro \
-v /etc/letsencrypt:/etc/letsencrypt:ro <nginx_image>
curl -k https://localhost:<port>
docker exec <container_name> nginx -s reload
# 自動續期(crontab -e)
# 0 3 * * * certbot renew --webroot -w <webroot_path> --deploy-hook "docker exec <container_name> nginx -s reload" >> /var/log/certbot-renew.log 2>&1
Flow:自動續約(需 80 port,不能用 *. domain)
Code
sudo apt install certbot
mkdir -p <webroot_path>
# 建立 nginx.conf(listen 80 + acme-challenge),再:
sudo certbot certonly --webroot -w <webroot_path> -d <domain>
docker exec <container_name> nginx -s reload
# crontab 同上;測試:sudo certbot renew --dry-run
Certbot
檢查是否已安裝
Code
which certbot
certbot --version
檢查已申請的憑證
Code
sudo certbot certificates
檢查憑證檔案位置
Code
sudo ls -la /etc/letsencrypt/live/
sudo certbot certificates | grep -A 5 "Certificate Name"
檢查憑證到期時間
Code
sudo certbot certificates | grep "Expiry Date"
sudo openssl x509 -in /etc/letsencrypt/live/[domain-name]/cert.pem -noout -dates
刪除不要的憑證
Code
sudo certbot delete --cert-name <cert_name>
測試申請 / 測試自動續期
Code
sudo certbot certonly --manual --preferred-challenges dns -d <domain> -d *.<domain> --dry-run
sudo certbot renew --dry-run
安裝
Code
sudo apt update && sudo apt install -y certbot
certbot --version
更新與複製憑證(簡要)
Code
sudo certbot certificates
sudo certbot certonly --manual --preferred-challenges dns -d <domain> -d *.<domain>
dig +short TXT _acme-challenge.<domain>
sudo openssl x509 -in /etc/letsencrypt/live/<domain>/cert.pem -noout -dates
sudo cp /etc/letsencrypt/live/<domain>/fullchain.pem <ssl_path>/fullchain.pem
sudo cp /etc/letsencrypt/live/<domain>/privkey.pem <ssl_path>/privkey.pem
sudo docker exec <container_name> nginx -s reload