

導讀:你將擁有一個不過期的SSL DV 免費證書,請看本文。
背景
之前網站使用的是免費證書,但是免費證書(預設證書)的簽發有效期由12個月縮短至3個月,很快就到期,這是要讓大多數企業購買付費證書的節奏啊,所以我更換了證書的簽發機構。本文主要講的就是如何使用 Let’s Encrypt的證書讓自己的網站免費從HTTP升級為HTTPS。
目前比較流行的免費 SSL 證書有 Let’s encrypt 和 Cloudflare。這次我們先嚐試一下 Let’s encrypt。
Let's Encrypt 是一個由非營利性組織 網際網路安全研究小組(ISRG)提供的免費、自動化和開放的證書頒發機構(CA)。

Let's Encrypt 簡介
簡單的說,藉助 Let's Encrypt 頒發的證書可以為我們的網站免費啟用 HTTPS(SSL/TLS) 。Let’s encrypt 證書是 DV(Domain Validation)證書,只驗證域名所有權,不驗證公司資訊。
其證書的簽發/續簽都是指令碼自動化的,官方提供了幾種證書的申請方式方法,網址為 https://letsencrypt.org/zh-cn/docs/client-options/。
安裝Certbot客戶端
[root@aliyun-www ~]# yum install certbot -y
等待它下載安裝完畢,檢測是否成功。使用如下命令:
[root@aliyun-www ~]# certbot --version
certbot 2.11.0
現在已經安裝成功,接著就可以申請SSL證書了。
比如我們要為static.example.com申請證書(你可以更換為真實的域名),使用如下命令:
[root@aliyun-www ~]# certbot certonly --nginx -d static.example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
The requested nginx plugin does not appear to be installed
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.
執行時,如果出現上面的提示,需要 nginx plugin 外掛,可以執行如下命令,安裝相應的外掛解決:
[root@aliyun-www ~]# yum install python3-certbot-nginx
接著,請讓我們再次執行申請SSL的指令:
[root@aliyun-www ~]# certbot certonly --nginx -d static.example.com
接下來,是一個互動的過程。
是否同意 Let's Encrypt 協議要求=>需要同意,是否分享你的郵箱,詢問是否對域名和機器(IP)進行繫結=>需要同意。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): [email protected]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
Account registered.
Requesting a certificate for static.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/static.example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/static.example.com/privkey.pem
This certificate expires on 2025-06-18.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
證書生成完畢後,我們可以在 /etc/letsencrypt/live/ 目錄下看到對應域名的資料夾,裡面存放了指向證書的一些快捷方式。
證書續簽:自動更新 SSL 證書
Let’s Encrypt 提供的證書只有90天的有效期,證書在到期前30天才會續簽成功,我們必須在證書到期之前,重新獲取這些證書,certbot 給我們提供了一個很方便的命令,那就是 certbot renew。透過這個命令,他會自動檢查系統內的證書,並且自動更新這些證書。可以執行這個命令測試一下:
[root@aliyun-www ~]# certbot renew --dry-run
Certbot 會檢查證書是否過期,如果過期會自動續期。可以將 certbot renew 新增到Cron定時任務,定期檢查證書是否過期。
Nginx 開啟 https
證書生成完成後可以到 /etc/letsencrypt/live/ 目錄下檢視對應域名的證書檔案。編輯 nginx 配置檔案監聽 443 埠,啟用 SSL,並配置 SSL 的公鑰、私鑰證書路徑:
server {
listen 443 ssl;
server_name example.cn;
root /home/www/example.com;
index index.html index.htm index.php;
ssl_certificate /etc/letsencrypt/live/example/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
...
}
新增 HTTP 自動跳轉到 HTTPS:
server {
listen 80;
server_name example.com;
location / {
rewrite ^(.*)$ https://$host$1 permanent;
}
}
配置好Nginx後,重新啟動:
systemctl restart nginx
這樣,你就可以一直有不過期的證書了。
結語
透過執行這些步驟,我們已在 CentOS 9 伺服器上成功安裝和配置 Let's Encrypt SSL,從而為網站訪問者提供安全通訊。請定期檢查你的配置和更新,以維護安全可靠的網路狀態。
作者:場長
相關閱讀: