c handbook-master/docs/CentOS

使用免费SSL证书让网站支持HTTPS访问
Edited: Tuesday 7 July 2026

使用免费SSL证书让网站支持HTTPS访问

我们使用的服务器是在公司内部,用联通送的ip,通过路由器隐射使得外网可以访问,我们在这个服务器搭建了很多工具,比如Gitlab,聊天工具,网盘等,访问都很麻烦,没有备案,都必须带上端口号访问对应的服务,据说80端口被封了,假设有了https就可以默认443端口,就不用带端口号了,通过https访问默认浏览器会给你带上443端口,下面是我使用Let’s Encrypt提供的SSL证书,记录配置SSL的安装实践过程。

下面两种方法均在 CentOS7 环境下操作滴。

certbot-auto

刚看到新闻,Let’s Encrypt发布的 ACME v2 现已正式支持通配符HTTPS证书,就立马使用上了 certbot

安装

1# 下载
2wget https://dl.eff.org/certbot-auto
3
4# 设为可执行权限
5chmod a+x certbot-auto

申请证书

1# 注xxx.com请根据自己的域名自行更改
2./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory -d "*.xxx.com" --manual --preferred-challenges dns-01 certonly
3
4# 如需把主域名也增加到证书的覆盖范围,请在开始申请证书步骤的那个指令把主域名也加上`-d "xxx.com"`,如下:
5# 需要注意的是,这样的话需要修改两次解析记录
6./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory -d "*.xxx.com" -d "xxx.com" --manual --preferred-challenges dns-01 certonly

sudo ./certbot-auto certonly –standalone –email my@qq.com -d abc.com -d www.abc.com


选项 `--no-self-upgrade`。certbot-auto默认始终尝试更新到最新版,但对已经稳定的应用而言,可以不用更新,因此可以使用此选项直接更新证书,而不用更新certbot-auto导致其它意外问题。

从服务器到目的地的出站端口 443 是否被防火墙阻止

```bash
nc -vz acme-v02.api.letsencrypt.org 443 -w2
# Ncat: Version 7.50 ( https://nmap.org/ncat )
# Ncat: Connected to 23.77.214.183:443.
# Ncat: 0 bytes sent, 0 bytes received in 0.07 seconds.

执行完这一步之后,会下载一些需要的依赖,稍等片刻之后,会提示输入邮箱

邮箱很重要,主要用于安全提醒,以及续期提醒

 1Complete!
 2Creating virtual environment...
 3Installing Python packages...
 4Installation succeeded.
 5Saving debug log to /var/log/letsencrypt/letsencrypt.log
 6Plugins selected: Authenticator manual, Installer None
 7Enter email address (used for urgent renewal and security notices) (Enter 'c' to
 8- cancel): 
 9+ cancel):  这里填写邮箱地址
10
11-------------------------------------------------------------------------------
12Please read the Terms of Service at
13https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
14agree in order to register with the ACME server at
15<div>  <a href="https://acme-v02.api.letsencrypt.org/directory">    <img src="" loading="lazy" class="mr-3 is-pulled-left" style="display: block; border-radius: 0.5em; object-fit: cover; object-position: 0 50%; max-height: 10em; width: 10em;"/>    <strong style="display:block;overflow: hidden;                   text-overflow: ellipsis;                   display: -webkit-box;                   -webkit-line-clamp: 1; /* number of lines to show */                   line-clamp: 1;                   -webkit-box-orient: vertical;">        https://acme-v02.api.letsencrypt.org/directory    </strong>  </a>    <div class="is-clearfix"></div></div>
16-------------------------------------------------------------------------------
17- (A)gree/(C)ancel: 
18+ (A)gree/(C)ancel: A
19
20-------------------------------------------------------------------------------
21Would you be willing to share your email address with the Electronic Frontier
22Foundation, a founding partner of the Let's Encrypt project and the non-profit
23organization that develops Certbot? We'd like to send you email about EFF and
24our work to encrypt the web, protect its users and defend digital rights.
25-------------------------------------------------------------------------------
26- (Y)es/(N)o: 
27+ (Y)es/(N)o: Y
28Obtaining a new certificate
29Performing the following challenges:
30dns-01 challenge for showgold.com
31
32-------------------------------------------------------------------------------
33NOTE: The IP of this machine will be publicly logged as having requested this
34certificate. If you're running certbot in manual mode on a machine that is not
35your server, please ensure you're okay with that.
36
37Are you OK with your IP being logged?
38-------------------------------------------------------------------------------
39- (Y)es/(N)o: 
40+ (Y)es/(N)o: Y
41
42-------------------------------------------------------------------------------
43Please deploy a DNS TXT record under the name
44+ _acme-challenge.xxx.com
45with the following value:
46
47+ VBsfRHG______4t_drxcEFQlyOS0puAlJFypAYQTA
48
49Before continuing, verify the record is deployed.
50-------------------------------------------------------------------------------
51Press Enter to Continue
52+ 不要心急着按回车,先执行dig _acme-challenge.xxx.com txt确认解析记录是否生效,生效之后再回去按回车确认
53Waiting for verification...
54
55IMPORTANT NOTES:
56 - Congratulations! Your certificate and chain have been saved at:
57+   /etc/letsencrypt/live/xxx.com/fullchain.pem
58   Your key file has been saved at:
59+   /etc/letsencrypt/live/xxx.com/privkey.pem
60   Your cert will expire on 2018-06-13. To obtain a new or tweaked
61   version of this certificate in the future, simply run certbot-auto
62   again. To non-interactively renew *all* of your certificates, run
63+   "certbot-auto renew"
64 - If you like Certbot, please consider supporting our work by:
65
66   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
67   Donating to EFF:                    https://eff.org/donate-le

注意:
申请通配符证书是要经过DNS认证的,按照提示,前往域名后台添加对应的DNS TXT记录。
添加之后,不要心急着按回车,先执行 dig xxxx.xxx.com txt确认解析记录是否生效,生效之后再回去按回车确认

看到 Congratulations 你就大功告成了!

续期HTTPS证书

1certbot-auto renew
2certbot-auto delete -d chat.xxx.cn # 删除证书
3./certbot-auto delete --cert-name xxx.com # 删除证书重新生成

⚠️ 注意这里会有升级操作,并且有安装 Python 包,有时候会非常慢,不要停止,停止操作可能会造成麻烦。

查看证书过期时间

1openssl x509 -noout -dates -in /etc/letsencrypt/live/<你的域名>/cert.pem

简单的查看方法

1certbot-auto certificates 

nginx应用该证书的例子

 1server {
 2    server_name xxx.com;
 3    listen 443 http2 ssl;
 4    ssl on;
 5    ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem;
 6    ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem;
 7
 8    location / {
 9      proxy_pass http://127.0.0.1:6666;
10    }
11}

无法应用到主域名

如需把主域名也增加到证书的覆盖范围,请在开始申请证书步骤的那个指令把主域名也加上-d "xxx.com",如下:

需要注意的是,这样的话需要修改两次解析记录

./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory -d "*.xxx.com" -d "xxx.com" --manual --preferred-challenges dns-01 certonly

certbot-nginx

安装 EPEL 仓库

首先要安装 Let's Encrypt 证书用的工具,这个可以在CentOS 的 EPEL 仓库里找到它,在找到它之前,先检查是否存在 EPEL 源:

1# 进入目录检查是否存在 EPEL 源,一般情况文件名称 epel.repo
2cd /etc/yum.repos.d/

如果不存在可以直接安装

1sudo yum install epel-release -y

安装签发证书工具

1sudo yum install certbot-nginx -y
2# 查看版本
3certbot --version
4# certbot 0.21.1

申请证书

报nginx命令不存在错误

1sudo certbot --nginx
2# Saving debug log to /var/log/letsencrypt/letsencrypt.log
3# The nginx plugin is not working; there may be problems with your existing configuration.
4# The error was: NoInstallationError()
5# 如果你报上面错误运行下面,命令解决问题
6which nginx # 查看目录
7#输出 /usr/local/nginx/sbin/nginx
8yum info nginx

解决方法

1ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
2ln -s /usr/local/nginx/conf/ /etc/nginx

报nginx配置文件目录不对错误

1sudo certbot --nginx
2# Saving debug log to /var/log/letsencrypt/letsencrypt.log
3# Error while running nginx -c /etc/nginx/nginx.conf -t.
4
5# nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
6# nginx: configuration file /etc/nginx/nginx.conf test failed
7
8# The nginx plugin is not working; there may be problems with your existing configuration.
9# The error was: MisconfigurationError('Error while running nginx -c /etc/nginx/nginx.conf -t.\n\nnginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)\nnginx: configuration file /etc/nginx/nginx.conf test failed\n',)

解决方法,这个解决方法就是让certbot认为你的,你的配置存在,并且将SSL配置写入你的nginx配置文件中,然后拷贝配置到你的默认 nginx 配置中。哈哈为了方便 nginx 启动不用指定配置,也没有看到 certbot 工具提供指定目录的命令,暂时我就这么解决吧。

 1# nginx 默认配置文件目录不 /etc/nginx/ 目录下,
 2# 需要拷贝/usr/local/nginx/conf目录下的全部文件
 3# 复制到/etc/nginx/目录下
 4cp -r /usr/local/nginx/conf/* /etc/nginx/
 5
 6# 编辑开机启动将所有目录换成/etc/nginx/
 7vim /lib/systemd/system/nginx.service
 8cp /lib/systemd/system/nginx.service{,.bak}
 9
10# 测试配置是否正确
11nginx -t -c /etc/nginx/nginx.conf

正确之后将nginx中的 SSL 配置复制到你的原来正在运行的配置中,在默认安装目录配置/usr/local/nginx/conf/。会在配置中生成如下内容,主要是复制这个。

1{
2  ssl_certificate /etc/letsencrypt/live/chat.wangchujiang.com/fullchain.pem; # managed by Certbot
3  ssl_certificate_key /etc/letsencrypt/live/chat.wangchujiang.com/privkey.pem; # managed by Certbot
4}

正式申请申请证书

首先运行证书生成命令,选择你要配置SSL证书的网站,这个是基于你网站已经在nginx中配置好了的情况。

 1sudo certbot --nginx certonly
 2# Saving debug log to /var/log/letsencrypt/letsencrypt.log
 3# Plugins selected: Authenticator nginx, Installer nginx
 4# Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
 5# 
 6# Which names would you like to activate HTTPS for?
 7# -------------------------------------------------------------------------------
 8# 1: chat.wangchujiang.com
 9# 2: g.wangchujiang.com
10# 3: pan.wangchujiang.com
11# -------------------------------------------------------------------------------
12# Select the appropriate numbers separated by commas and/or spaces, or leave input
13# blank to select all options shown (Enter 'c' to cancel): 2

上面选择了g.wangchujiang.com生成证书,下面是验证过程,添加 certonly参数表示只生成证书。

 1# Obtaining a new certificate
 2# Performing the following challenges:
 3# tls-sni-01 challenge for g.wangchujiang.com
 4# Waiting for verification...
 5# Cleaning up challenges
 6# 
 7# IMPORTANT NOTES:
 8#  - Congratulations! Your certificate and chain have been saved at:
 9#    /etc/letsencrypt/live/g.wangchujiang.com/fullchain.pem
10#    Your key file has been saved at:
11#    /etc/letsencrypt/live/g.wangchujiang.com/privkey.pem
12#    Your cert will expire on 2018-03-13. To obtain a new or tweaked
13#    version of this certificate in the future, simply run certbot
14#    again. To non-interactively renew *all* of your certificates, run
15#    "certbot renew"
16#  - If you like Certbot, please consider supporting our work by:
17# 
18#    Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
19#    Donating to EFF:                    https://eff.org/donate-le

上面生成成功了,可以添加到 nginx 配置中,这下完事儿了,下面是一端nginx的配置实例。

配置nginx

 1# http 重定向到 https
 2server {
 3    listen       80;
 4    server_name  g.wangchujiang.com;
 5    rewrite ^ https://$http_host$request_uri? permanent;
 6    # Enables or disables emitting nginx version on error pages and in the "Server" response header field.
 7    server_tokens off;
 8}
 9# https 的配置
10server {
11  listen       443 ssl;
12  server_name  g.wangchujiang.com;
13
14  ssl_certificate /etc/letsencrypt/live/g.wangchujiang.com/fullchain.pem;
15  ssl_certificate_key /etc/letsencrypt/live/g.wangchujiang.com/privkey.pem;
16  # 禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击
17  server_tokens off;
18  # 设置ssl/tls会话缓存的类型和大小。如果设置了这个参数一般是shared,buildin可能会参数内存碎片,默认是none,和off差不多,停用缓存。如shared:SSL:10m表示我所有的nginx工作进程共享ssl会话缓存,官网介绍说1M可以存放约4000个sessions。 
19  ssl_session_cache    shared:SSL:1m; 
20
21  # 客户端可以重用会话缓存中ssl参数的过期时间,内网系统默认5分钟太短了,可以设成30m即30分钟甚至4h。
22  ssl_session_timeout  5m; 
23
24  # 选择加密套件,不同的浏览器所支持的套件(和顺序)可能会不同。
25  # 这里指定的是OpenSSL库能够识别的写法,你可以通过 openssl -v cipher 'RC4:HIGH:!aNULL:!MD5'(后面是你所指定的套件加密算法) 来看所支持算法。
26  ssl_ciphers  HIGH:!aNULL:!MD5;
27
28  # 设置协商加密算法时,优先使用我们服务端的加密套件,而不是客户端浏览器的加密套件。
29  ssl_prefer_server_ciphers  on;
30
31  location / {
32    root   html;
33    index  index.html index.htm;
34  }
35}

错误处理

  1. 证书提示 certbot-auto 无法升级
1[root@localhost ~]# ./certbot-auto certificates
2Upgrading certbot-auto 1.0.0 to 1.3.0...
3Couldn't download https://raw.githubusercontent.com/certbot/certbot/v1.3.0/letsencrypt-auto-source/letsencrypt-auto. <urlopen error [Errno 111] Connection refused>

certbot-auto 将始终尝试从最新版本中获取自身的最新版本。

解决办法:如果希望将其锁定到特定版本并且不接收自动更新,只需在命令后加 –no-self-upgrade 即可

./certbot-auto certificates  --no-self-upgrade

参考阅读