在Debian系统上使用Nginx配置SSL证书,可以按照以下步骤进行:
首先,你需要一个SSL证书。你可以从Let’s Encrypt免费获取,或者从其他证书颁发机构购买。
安装Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
运行Certbot获取证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成证书的申请和安装。
Certbot会自动修改你的Nginx配置文件,通常位于/etc/nginx/sites-available/yourdomain.com。
如果你选择手动配置,可以按照以下步骤进行:
编辑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 / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
启用配置:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
测试Nginx配置:
sudo nginx -t
重新加载Nginx:
sudo systemctl reload nginx
Let’s Encrypt证书通常有效期为90天,因此需要设置自动续期。
编辑Certbot的续期配置:
sudo crontab -e
添加以下行以每天检查证书续期:
0 */12 * * * certbot renew --post-hook "systemctl reload nginx"
你可以使用以下命令验证SSL配置是否正确:
sudo openssl s_client -connect yourdomain.com:443 -servername yourdomain.com
如果一切正常,你应该会看到SSL握手成功的信息。
通过以上步骤,你就可以在Debian系统上成功配置Nginx使用SSL证书了。