# 如何在CentOS下安裝Let’s Encrypt永久免費SSL證書
## 前言
在當今互聯網環境中,網站安全已成為不可忽視的重要議題。SSL/TLS證書作為保障數據傳輸加密的核心技術,已經從可選配置變為必備要素。Let's Encrypt作為非營利性證書頒發機構(CA),提供完全免費的自動化證書服務,極大降低了HTTPS的部署門檻。本文將詳細介紹在CentOS系統上使用Certbot工具獲取、安裝和自動續期Let's Encrypt證書的全過程。
## 一、準備工作
### 1.1 系統要求
- CentOS 7/8(本文以CentOS 7為例)
- 已安裝Nginx/Apache等Web服務(本文以Nginx為例)
- 域名已解析到服務器IP
- 開放80和443端口
### 1.2 環境檢查
```bash
# 檢查系統版本
cat /etc/centos-release
# 檢查Web服務狀態
systemctl status nginx
# 檢查防火墻設置
firewall-cmd --list-ports
yum install epel-release -y
yum install certbot python3-certbot-nginx -y
certbot --version
# 應輸出類似:certbot 1.21.0
certbot --nginx -d example.com -d www.example.com
執行后會交互式詢問: 1. 輸入郵箱(用于安全通知) 2. 同意服務條款 3. 是否訂閱電子報(可選否)
certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com
需要確保:
- -w參數指定的目錄是網站根目錄
- 可通過http://example.com/.well-known/acme-challenge/訪問驗證文件
Certbot會自動修改Nginx配置,典型片段如下:
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
# 強制HTTPS跳轉
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
# 增強SSL安全性
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384...';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
certbot renew --dry-run
crontab -e
添加以下內容(每天凌晨2點檢查續期):
0 2 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"
需要DNS驗證方式:
certbot certonly --manual --preferred-challenges=dns -d *.example.com
# 單個證書包含多個域名
certbot --nginx -d example.com -d api.example.com -d static.example.com
# 查看現有證書
certbot certificates
certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem
解決方法:
- 臨時停止Nginx:systemctl stop nginx
- 使用--standalone模式:certbot certonly --standalone -d example.com
檢查日志:
tail -f /var/log/letsencrypt/letsencrypt.log
cert.pem:域名證書chain.pem:中間證書fullchain.pem:完整證書鏈privkey.pem:私鑰文件私鑰保護:設置600權限
chmod 600 /etc/letsencrypt/live/example.com/privkey.pem
OCSP Stapling 配置:
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
HSTS 頭加強:
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
使用Certbot內置提醒或第三方監控工具:
certbot renew --notify-hook "/path/to/notification-script.sh"
建議備份整個/etc/letsencrypt目錄:
tar -czvf letsencrypt-backup-$(date +%Y%m%d).tar.gz /etc/letsencrypt
通過本文的詳細指導,您應該已經成功在CentOS服務器上部署了Let’s Encrypt SSL證書。這種全自動化的免費證書方案,配合合理的續期機制,可以長期保障網站的安全通信。隨著HTTP/2的普及和瀏覽器對HTTPS的強制要求,部署SSL證書已成為現代網站的標配操作。Let’s Encrypt的出現,真正實現了”加密整個互聯網”的愿景。
注意:Let’s Encrypt證書有效期仍為90天,但通過自動化續期可以實現”永久免費”的效果。建議定期檢查cron任務和續期日志確保系統正常運行。 “`
本文共計約2300字,涵蓋了從基礎安裝到高級配置的全流程,并包含故障排查和安全建議,適合不同技術水平的用戶參考使用。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。