在Debian上配置LNMP(Linux, Nginx, MySQL, PHP)以使用SSL涉及几个步骤。以下是一个基本的指南,帮助你设置SSL证书并配置Nginx以使用HTTPS。
Certbot是一个自动化的工具,用于获取和续订Let’s Encrypt SSL证书。你可以使用以下命令安装Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
使用Certbot获取SSL证书。运行以下命令并按照提示操作:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot会自动配置Nginx以使用SSL证书,并创建一个指向你的网站的符号链接。
Certbot会自动测试你的Nginx配置文件是否有语法错误。如果有错误,它会提示你修复它们。你可以手动测试Nginx配置文件的语法:
sudo nginx -t
如果没有错误,重新加载Nginx以应用更改:
sudo systemctl reload nginx
Certbot会自动设置一个cron作业来定期续订你的SSL证书。你可以手动测试续订过程以确保一切正常:
sudo certbot renew --dry-run
如果一切正常,Certbot会在证书到期前30天自动续订证书。
确保你的防火墙允许HTTPS流量。如果你使用的是ufw,可以运行以下命令:
sudo ufw allow 'Nginx Full'
HSTS(HTTP Strict Transport Security):强制浏览器始终通过HTTPS访问你的网站。
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
OCSP Stapling:提高SSL握手速度和安全性。
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
将上述配置添加到你的Nginx服务器块中,以启用这些可选功能。
以下是一个基本的Nginx服务器块配置示例,包括SSL设置:
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;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
root /var/www/html;
index index.php index.html index.htm;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location / {
try_files $uri $uri/ =404;
}
}
请根据你的实际情况调整配置。
完成这些步骤后,你的Debian服务器上的LNMP环境应该已经成功配置了SSL。