Let's Encrypt免费SSL证书配置指南

HTTPS已经是现代网站的标配,Let's Encrypt提供了免费的SSL证书。

一、安装Certbot

sudo apt update
sudo apt install certbot python3-certbot-nginx

二、申请证书

# 自动配置Nginx
sudo certbot --nginx -d example.com -d www.example.com

# 仅获取证书
sudo certbot certonly --webroot -w /var/www/html -d example.com

三、证书位置

/etc/letsencrypt/live/example.com/
  ├── cert.pem       # 服务器证书
  ├── chain.pem      # 中间证书
  ├── fullchain.pem  # 完整证书链
  └── privkey.pem    # 私钥

四、自动续期

# 测试续期
sudo certbot renew --dry-run

# 添加定时任务
0 0,12 * * * /usr/bin/certbot renew --quiet

五、Nginx HTTPS配置

server {
    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    add_header Strict-Transport-Security "max-age=63072000" always;
}

Let's Encrypt证书有效期为90天,建议开启自动续期!

发表回复

后才能评论