网络知识
Debian Nginx SSL如何配置Let's Encrypt
2025-07-08 17:01  点击:0

在Debian系统上使用Nginx配置Let’s Encrypt SSL证书,可以按照以下步骤进行:

1. 安装Certbot

Certbot是Let’s Encrypt的官方客户端工具,用于自动获取和续订SSL证书。

sudo apt updatesudo apt install certbot python3-certbot-nginx
2. 获取SSL证书

使用Certbot获取SSL证书。你可以选择自动配置Nginx或手动配置。

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

Certbot会自动检测Nginx配置文件,并提示你进行一些配置更改。按照提示操作即可。

手动配置Nginx

如果你选择手动配置,首先需要停止Nginx服务:

sudo systemctl stop nginx

然后运行Certbot来获取证书:

sudo certbot certonly --standalone -d yourdomain.com -d www.yourdomain.com

Certbot会启动一个临时的HTTP服务器来验证你的域名所有权。按照提示操作即可。

3. 配置Nginx

Certbot会自动修改Nginx配置文件以启用SSL。你可以手动检查并编辑配置文件以确保一切正确。

打开Nginx配置文件:

sudo nano /etc/nginx/sites-available/yourdomain.com

确保配置文件中有以下内容:

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;root /var/www/html;index index.html index.htm;location ~ /\.ht {deny all;}}
4. 启动Nginx

保存并关闭配置文件后,启动Nginx服务:

sudo systemctl start nginx
5. 测试配置

确保Nginx配置正确并且SSL证书已生效:

sudo nginx -t

如果没有错误,重新加载Nginx:

sudo systemctl reload nginx
6. 设置自动续订

Certbot会自动设置一个cron任务来定期续订证书。你可以手动测试续订过程:

sudo certbot renew --dry-run

如果一切正常,证书将会自动续订。

通过以上步骤,你就可以在Debian系统上使用Nginx配置Let’s Encrypt SSL证书了。