首页/发现新知
视界 · 深度阅读

centos lnmp怎么部署ssl

在 CentOS + LNMP(Linux + Nginx + MySQL + PHP) 环境下部署 SSL(HTTPS),通常有以下几种方式。下面以 最常用、最推荐的方式:Nginx + Let’s Encrypt 免费证书 为例,一步步说明。


一、准备工作

1. 系统要求

  • CentOS 7 / 8 / Stream
  • 已安装 LNMP(Nginx 可正常访问)
  • 域名已解析到服务器 IP
  • 80 端口可访问(申请证书需要)

二、使用 Let’s Encrypt 免费 SSL(推荐)

方式一:使用 certbot(官方推荐)

1. 安装 EPEL 和 certbot

yum install epel-release -y
yum install certbot python2-certbot-nginx -y

CentOS 8 可用:

dnf install certbot python3-certbot-nginx -y

2. 自动申请并配置 SSL

certbot --nginx -d www.example.com -d example.com

按提示输入邮箱、同意协议即可。

3. 自动续期测试

certbot renew --dry-run

✅ 证书默认 90 天,certbot 会自动续期。


三、手动配置 Nginx SSL(适合已有证书)

如果你已有 .crt 和 .key 文件:

1. 上传证书

mkdir -p /etc/nginx/ssl/example.com
cp example.com.crt /etc/nginx/ssl/example.com/
cp example.com.key /etc/nginx/ssl/example.com/

2. 修改 Nginx 配置

编辑站点配置(如 /usr/local/nginx/conf/vhost/example.com.conf 或 /etc/nginx/conf.d/example.com.conf):

server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate     /etc/nginx/ssl/example.com/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/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;
    }
}

server {
    listen 80;
    server_name www.example.com;
    return 301 https://$host$request_uri;
}

3. 测试并重载 Nginx

nginx -t
systemctl reload nginx

四、LNMP 一键包用户(军哥 LNMP)

如果你用的是 lnmp.org 一键包:

lnmp ssl add

按提示:

  • 输入域名
  • 选择 Let’s Encrypt
  • 自动配置完成

五、常见问题

1. 防火墙未放行 443

firewall-cmd --permanent --add-service=https
firewall-cmd --reload

2. 云服务器安全组

  • 放行 80 / 443

3. 证书不生效

  • 域名未解析
  • Nginx 配置未 reload
  • 证书路径错误

六、总结

✅ 推荐方案:

  • 个人 / 企业站点:Let’s Encrypt + certbot
  • 已有商业证书:手动配置 Nginx

如果你愿意,可以告诉我:

  • CentOS 版本
  • 是否使用 lnmp 一键包
  • 域名是否已解析

我可以给你精确到命令的配置方案。

读到这里,发现更多一点。探索更多内容 ↗