在 LNMP 环境(Linux + Nginx + MySQL + PHP) 中安装 SSL,通常是指 给 Nginx 配置 HTTPS 证书。下面按 最常见、最推荐 的方式一步步说明,适合新手和线上服务器。
example.comyum install -y epel-release
yum install -y certbot python3-certbot-nginx
apt update
apt install -y certbot python3-certbot-nginx
certbot --nginx -d example.com -d www.example.com
✅ 它会自动:
certbot renew --dry-run
Let’s Encrypt 证书:
如果你已有:
example.com.crtexample.com.keymkdir -p /etc/nginx/ssl
cp example.com.crt /etc/nginx/ssl/
cp example.com.key /etc/nginx/ssl/
编辑配置文件(示例):
vim /etc/nginx/conf.d/example.conf
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
root /home/wwwroot/example.com;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
# HTTP 自动跳转 HTTPS
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
nginx -t
systemctl reload nginx
ss -lntp | grep 443
检查:
fullchain.crt
而不是 cert.crt
LNMP 自带 SSL 支持,可直接:
lnmp ssl add
| 场景 | 推荐方式 |
|---|---|
| 新站点 | ✅ Let’s Encrypt |
| 企业证书 | 手动安装 |
| LNMP 一键包 | lnmp ssl add |
| 国内备案域名 | 可选国内 CA |
如果你愿意,可以告诉我:
我可以给你 完全针对你服务器的命令 ✅