[笔记] CentOS7 + Nginx 环境下,安装使用 Let‘s Encrypt 免费 SSL 证书 (自动续签)

4 阅读8分钟

安装 SSL/TLS 证书在服务器可以确保数据传输的安全性和完整性,验证服务器身份,增强用户信任,提升搜索引擎排名,并符合相关法规要求,从而保护敏感信息不被窃听或篡改。

网站使用 HTTPS 协议已是大势所趋,而要在 web 上使用 HTTPS 的话,我们首先需要获得一个 SSL 证书文件。本文介绍如何在 CentOS7 + Nginx 环境下,安装使用 Let's Encrypt 免费 SSL 证书 。

Let’s Encrypt 官网 : Let’s Encrypt (letsencrypt.org)

Nginx 官网 : nginx

使用 Nginx 反向代理 : [笔记] 多层 Nginx反向代理与Docker容器化前端应用部署 : 客户端 -> 本地 Nginx -> Docker 内的 Nginx -> 前端应用 - 掘金

1.配置安装环境

这里以 1.26.0 版本为例

查看所有版本 :

curl -s http://nginx.org/en/download.html | grep -o 'nginx-[0-9]\+\.[0-9]\+\.[0-9]\+\.tar\.gz'

也可以在官网去查看版本

配置环境:

# 1.下载安装包 指定下载到 /home/luchangqiu/downloads 目录下
wget -P /home/luchangqiu/downloads http://nginx.org/download/nginx-1.26.0.tar.gz

# 2.解压 解压到 /home/luchangqiu/tmp/nginx-src 目录下
tar -xvf /home/luchangqiu/downloads/nginx-1.26.0.tar.gz -C /home/luchangqiu/tmp/nginx-src

# 3.添加用户组
useradd -r -g nginx nginx -s /sbin/nologin

# 4.更改文件夹所属用户组
chown nginx:nginx /usr/local/nginx-1.26.0/ -R

# 5.检查安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.编译安装 Nginx

# 进入解压目录
cd /home/luchangqiu/tmp/nginx-src

# 执行命令 考虑到后续安装ssl证书 添加两个模块
./configure \
    --user=nginx \      # 设置 Nginx 进程运行时的用户
    --group=nginx \     # 设置 Nginx 进程运行时的组
    --prefix=/usr/local/nginx-1.26.0 \    # 指定 Nginx 的安装前缀目录
    --with-http_ssl_module \     # 启用 HTTP SSL 模块,用于支持 HTTPS 协议
    --with-http_stub_status_module \   # 用于提供简单的服务器状态页面
    --with-http_realip_module \  # 用于识别客户端的真实 IP 地址
    --with-http_slice_module \  # 启用 HTTP Slice 模块,用于支持切片响应,适用于流媒体等场
    --with-stream  # 启用 Stream 模块,用于处理非 HTTP 流量,如 TCP 和 UDP 数据流

# 执行make命令
make && make install

# 创建软连接 因为 /usr/local/bin/ 已经包含在了环境变量,所以在其他位置可以直接用 nginx 命令了
ln -s /usr/local/nginx-1.26.0/sbin/nginx  /usr/local/bin/nginx
# 查看软连接 
ls -l /usr/local/bin/nginx
# 输出结果
[root@lrnev ~]# ls -l /usr/local/bin/nginx
lrwxrwxrwx 1 root root 34 Oct 18 14:11 /usr/local/bin/nginx -> /usr/local/nginx-1.26.0/sbin/nginx

# 检查配置文件是否正确(-t是检查,-c是指定加载路径)
nginx -t -c /usr/local/nginx-1.26.0/conf/nginx.conf
# 输出结果
[root@lrnev ~]# nginx -t -c /usr/local/nginx-1.26.0/conf/nginx.conf
nginx: the configuration file /usr/local/nginx-1.26.0/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.26.0/conf/nginx.conf test is successful

# 服务启动
nginx -c /usr/local/nginx-1.26.0/conf/nginx.conf
# 服务重启
nginx -c /usr/local/nginx-1.26.0/conf/nginx.conf -s reload
# 服务暂停
nginx -c /usr/local/nginx-1.26.0/conf/nginx.conf -s stop

# 查看运行状况
ps aux | grep nginx

3.服务器开放端口

服务器要开放 80 口及 443 端口:

# 添加端口规则
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp

# 重新加载 Firewalld 配置
firewall-cmd --reload

# 验证端口以开放
sudo firewall-cmd --list-ports
# 输出结果
[root@lrnev ~]# sudo firewall-cmd --list-ports
80/tcp 443/tcp

如果提示 FirewallD is not running 系统上 Firewalld 服务没有运行

启动了 Firewalld 服务后 , 再去 运行上面的命令

# 启动 Firewalld 服务
sudo systemctl start firewalld

# 设置 Firewalld 开机自启
sudo systemctl enable firewalld

4.域名解析

域名服务器创建一条 A 记录,指向服务器的公网 IP 地址。

我这里用的是阿里的
在这里插入图片描述

等域名解析生效后,访问 yourdomain.com/ 可以看到如下页面则说明域名解析成功:

yourdomain.com你的域名
在这里插入图片描述

5.安装 certbot 工具

我们采用 certbot 脚本方式申请 let's encript 证书,依次执行如下命令安装该工具:

# 安装 EPEL 仓库
yum install -y epel-release

# 从 EPEL 仓库中安装 Certbot
yum install -y certbot

6.申请证书

  • 接下来我们要使用 certbot 命令初次申请证书,命令格式如下:

    注意:联系人 email 地址最好填写真实有效的地址,Let's Encrypt 会在证书在过期以前发送通知提醒邮件。

    certbot certonly --webroot -w [Web站点目录] -d [站点域名] -m [联系人email地址] --agree-tos
    

    举例 :

    certbot certonly --webroot -w /usr/local/nginx-1.26.0/html -d yourdomain.com -m youremail@163.com --agree-tos
    

    输出结果 :

在这里插入图片描述

  • 申请成功后,证书会保存在 /etc/letsencrypt/live/yourdomain.com/ 下面:
    在这里插入图片描述

  • 使用如下命令可以查看证书的有效期:

    openssl x509 -noout -dates -in /etc/letsencrypt/live/yourdomain.com/cert.pem
    

7.更新证书

  • Let's Encrypt 证书的有效期是 90 天,需要长期使用的话,需要在失效前进行延长申请。我们可以执行如下命令去更新:

    # 更新证书
    certbot renew --dry-run
     
    # 如果不需要返回的信息,可以用静默方式
    certbot renew --quiet
    
  • 我们也可以将更新证书的脚本写到定时任务来自动完成,免得我们手动操作。首先执行如下命令开始编辑定时任务:

    # 编辑当前用户的 cron 任务表的命令
    crontab -e
    
  • 此时会进入 vi 的编辑界面让你编辑工作(每项工作都是一行)。我们在末尾添加如下一行内容,表示每月 15 时会执行执行一次更新,并重启 nginx 服务器:

    00 05 01 * * /usr/bin/certbot renew --quiet && /bin/systemctl restart nginx
    

    也可以指定向日志文件 :

    00 05 01 * * /usr/bin/certbot renew --quiet && /bin/systemctl restart nginx >> /var/log/certbot-renew.log 2>&1
    

    所有输出(包括错误信息)都会被记录到 /var/log/certbot-renew.log 文件中,方便查看和排查问题

  • 保存后退出,执行 crontab -l 命令可以查看 crontab 服务是否创建成功:

    # 查看当前用户的 cron 任务表
    crontab -l
    
    # 输出结果
    [root@lrnev ~]# crontab -l
    00 05 01 * * /usr/bin/certbot renew --quiet && /bin/systemctl restart nginx >> /var/log/certbot-renew.log 2>&1
    

8. 配置 Nginx

  • 首先执行如下命令生成 Perfect Forward Security(PFS)键值:

    # 创建目录
    mkdir /etc/ssl/private/ -p
    
    # 进入目录
    cd /etc/ssl/private/
    
    # 生成 DH 参数文件
    openssl dhparam 2048 -out dhparam.pem
    
    # 解释
    # openssl:OpenSSL 命令行工具,用于处理各种与 SSL/TLS 相关的任务。
    # dhparam:生成 Diffie-Hellman 参数。
    # 2048:生成的 DH 参数的长度,单位是位。2048 位是一个常用的长度,提供了良好的安全性。
    # -out dhparam.pem:指定生成的 DH 参数文件的输出路径和文件名。
    

    作用 : Diffie-Hellman 参数文件 (dhparam.pem) 用于增强 SSL/TLS 连接的安全性,特别是在使用 DHE(Diffie-Hellman Ephemeral)密钥交换算法时。DHE 密钥交换算法提供了前向安全性(Forward Secrecy),即使私钥泄露,也无法解密过去捕获的会话数据。

  • 编辑 nginx 配置文件:

    vi /usr/local/nginx-1.26.0/conf/nginx.conf
    
    • 修改默认的 server 配置:

          server {
              listen       80;
              server_name  yourdomain.com;
              rewrite ^ https://$server_name$request_uri? permanent;
      
    • HTTPS server 配置前面的注释(#)去掉,把内容改成如下:

      # HTTPS server
      #
      server {
          listen       443 ssl;
          server_name  yourdomain.com;
       
          ssl_certificate      /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
          ssl_certificate_key  /etc/letsencrypt/live/yourdomain.com/privkey.pem;
       
          ssl_session_cache    shared:SSL:1m;
          ssl_session_timeout  5m;
       
          ssl_dhparam /etc/ssl/private/dhparam.pem;
          ssl_protocols TLSv1.2 TLSv1.3;
       
          ssl_ciphers  'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128:AES256:AES:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK';
              ssl_prefer_server_ciphers  on;
       
              location / {
                  root   html;
                  index  index.html index.htm;
              }
          }
      
  • 保存退出后,重启 nginx 服务:

    nginx -s reload
    
  • 再次使用浏览器访问 **yourdomain.com**,如果正常跳转到 **yourdomain.com**,并且地址栏有个小锁图标则说明 https 配置成功了
    在这里插入图片描述

9. 通配符域名 获取 SSL

a.获取证书

注意 : key 出来后 先不要按回车 先去域名解析添加记录 !!!

sudo certbot certonly --manual -d "*.yourdomain.com" -d yourdomain.com -m youremail@foxmail.com --agree-tos

如果上面的命令不行 需指定 --preferred-challenge

sudo certbot certonly --manual -d "*.yourdomain.com" -d yourdomain.com -m youremail@foxmail.com --agree-tos --preferred-challenges dns
  • 如果没有指定 --preferred-challenges,Certbot 会按顺序尝试这些挑战类型。
  • --preferred-challenges dns:明确指定优先使用 DNS-01 挑战进行验证。这对于通配符域名特别重要,因为 HTTP-01 挑战无法验证通配符域名。

在这里插入图片描述

b. 添加 DNS TXT 记录

在这里插入图片描述

c. 继续获取证书

添加完解析记录再继续按回车
在这里插入图片描述

d. 回到 7.更新证书 继续往下 (定时任务自动续签不需要修改)