论如何给自己的小站加一把小绿锁
安装Certbot工具
Certbot是Let’s Encrypt官方推荐的自动化工具,支持多种操作系统和Web服务器(如Nginx、Apache),以Ubuntu为例安装Certbot及插件:
sudo apt update
sudo apt install certbot python3-certbot-nginx # 若使用Nginx则安装此插件
sudo apt install certbot-dns-cloudflare # 若需DNS验证(如Cloudflare)
选择验证方式并申请证书
Let’s Encrypt支持两种主流验证方式,需根据场景选择:
HTTP-01验证(适合单域名)
适用场景:服务器可直接开放80/443端口,且域名已解析到服务器IP
申请证书:
certbot certonly --webroot -w /var/www/html/your_domain -d your_domain.com
其中 -w:指定该域名网站的根目录,确保可通过 http://example.com/.well-known/acme-challenge 访问验证文件;-d:指定域名
可在对应的 server 块(通常是 80 端口的 HTTP 配置)中添加以下规则,允许公开访问 .well-known/acme-challenge 目录:
server {
listen 80;
server_name example.com www.example.com;
# 强制所有流量使用 HTTPS(可选,但建议申请证书时暂时注释掉)
# return 301 https://$host$request_uri;
# 允许 Let's Encrypt 验证文件的访问
location ^~ /.well-known/acme-challenge/ {
root /var/www/html; # 替换为你的 Web 根目录(与Certbot的 -w 参数一致)
default_type "text/plain";
try_files $uri =404;
}
# 其他规则...
}
或自动配置Nginx(若安装插件):
certbot --nginx -d your_domain.com
DNS-01验证(适合通配符域名或服务器无法开放端口)
适用场景:通配符证书(如*.example.com)或使用Cloudflare等DNS服务商
获取DNS服务商API密钥(以Cloudflare为例):
# 配置文件(如~/.secrets/certbot/cloudflare.ini)
dns_cloudflare_email = [email protected]
dns_cloudflare_api_key = YOUR_API_KEY
申请证书:
certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini -d *.example.com
证书路径
证书文件通常位于/etc/letsencrypt/live/your_domain/,包含fullchain.pem(公钥)和privkey.pem(私钥)
配置自动续期
Let’s Encrypt证书有效期为90天,需定期续期
测试续期命令:
certbot renew --dry-run
添加定时任务(Cron):
# 每天检查续期,证书到期前30天内自动续签
0 0 */5 * * certbot renew --quiet --renew-hook "systemctl restart nginx"
–renew-hook参数用于续期后重启服务
查看和管理证书
列出所有证书:
certbot certificates
撤销证书:
certbot revoke --cert-path /etc/letsencrypt/live/www.example.com/fullchain.pem
删除证书:
certbot delete
申请Let’s Encrypt免费SSL证书
https://ailitonia.com/archives/%e7%94%b3%e8%af%b7lets-encrypt%e5%85%8d%e8%b4%b9ssl%e8%af%81%e4%b9%a6/
本文被阅读了:4,913次