1. 正确安装 nginx 并配置好 ssl 模块
- 已经安装的情况下
1 | # 检查版本是否包含 configure arguments: ... --with-http_ssl_module ... |
如果 包含 --with-http_ssl_module,但仍提示错误,可能是配置文件的路径或格式有问题,请仔细检查配置文件。
如果 不包含,则需要重新安装或重新编译 Nginx 以支持 SSL。
- 这里采用从源码编译安装添加
--with-http_ssl_module
- 下载源码
1 | wget http://nginx.org/download/nginx-1.x.x.tar.gz |
- 编译时启用 SSL 模块:
1 | ./configure --with-http_ssl_module |
把
/usr/local/nginx/sbin/nginx可以软连接到/usr/local/bin中,这样全局可以执行启动新版本的 Nginx 并检查是否支持 SSL:
1 | nginx -V |
- 让
nginx开机自启
使用 systemd 设置 Nginx 开机启动(推荐)
创建或检查 Nginx 的 systemd 服务文件Nginx 的 systemd 服务文件通常位于 /etc/systemd/system/nginx.service 或 /lib/systemd/system/nginx.service。你可以检查是否已存在该文件。
使用以下命令查看文件:
1 | sudo systemctl status nginx |
如果返回结果中包含 nginx.service,说明 systemd 已经为 Nginx 配置了服务文件。
如果没有服务文件,手动创建服务文件
如果 Nginx 的 systemd 服务文件不存在,你可以手动创建一个。首先,创建或编辑 nginx.service 文件:
1 | sudo vi /etc/systemd/system/nginx.service |
添加以下内容到 nginx.service 文件:
1 | [Unit] |
其中:
ExecStart 是 Nginx 启动的命令路径。ExecReload 用于重新加载配置。ExecStop 用于停止 Nginx。
重新加载 systemd 配置:
1 | sudo systemctl daemon-reload |
启用 Nginx 自启动:
现在,启用 Nginx 在系统启动时自动启动:
1 | sudo systemctl enable nginx |
这会将 Nginx 服务加入到系统的启动项中。
1 | sudo systemctl start nginx |
使用以下命令检查 Nginx 服务是否正在运行:
1 | sudo systemctl status nginx |
3. 给域名申请证书
这里使用 Let's Encrypt 免费证书为例子
安装 certbot
1 | sudo yum install -y epel-release |
因为没有装 nginx 插件,这里采用不通过插件的方式生成证书
需要先停止 nginx 服务
1 | sudo certbot certonly --standalone -d your.domain.com |
如果生成成功,证书将位于 /etc/letsencrypt/live/your.domain.com/ 中。
2. 添加反向代理配置
这里我把配置文件都放到一个文件夹里,通过 nginx 主配置进行引入
1 | sudo mkdir -p /mnt/nginx/conf |
在文件中添加以下内容,以当前域名为例子 your.domain.com:
1 | server { |
- 不需要
https的配置如下
1 | server { |
3. 证书定时续期
Let's Encrypt 的证书有效期为 90 天,Certbot 可以自动续期。运行以下命令测试续期是否正常:
1 | # 测试证书续期是否正常 |
需要先关闭 nginx 占用的 80 端口
如果测试成功,Certbot 会自动通过 cron 或 systemd 定期续期,无需额外操作。