在Debian系统上,使用Nginx和Let’s Encrypt(现在称为Certbot)来配置SSL证书是一个相对简单的过程。以下是详细步骤:
首先,你需要安装Certbot及其Nginx插件。打开终端并运行以下命令:
sudo apt update
sudo apt install certbot python3-certbot-nginx
Certbot会自动检测你的Nginx配置文件,并提示你输入域名。运行以下命令开始获取证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
将 yourdomain.com
和 www.yourdomain.com
替换为你的实际域名。
Certbot会自动修改你的Nginx配置文件以启用SSL。你需要确保你的Nginx配置文件中有以下内容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# 其他配置...
}
在重新加载Nginx之前,测试配置文件是否有语法错误:
sudo nginx -t
如果没有错误,重新加载Nginx以应用更改:
sudo systemctl reload nginx
Certbot会自动设置一个cron任务来定期检查并续期你的SSL证书。你可以手动测试续期过程:
sudo certbot renew --dry-run
如果一切正常,Certbot会输出续期成功的信息。
确保你的防火墙允许HTTP(端口80)和HTTPS(端口443)流量。如果你使用的是UFW,可以运行以下命令:
sudo ufw allow 'Nginx Full'
通过以上步骤,你可以在Debian系统上使用Nginx和Let’s Encrypt来配置SSL证书。Certbot会简化整个过程,并自动处理证书的获取和续期。