如何在Ubuntu20.04 上使用 Certbot 给Nginx 安装 免费 并且 自动更新的Https证书

最终效果

在安装完成后能够获得以下效果:

  1. 给Nginx的web服务安装免费的证书
  2. 证书过期时能够自动更新证书,并安装。在更新的过程中无需人工的介入。

安装过程

防火墙配置

如果防火墙未安装使用下面的命令安装防火墙

1
2
sudo apt update
sudo apt install ufw

防火墙开放ssh (port 22),http (port 80),https (port 443) 的端口通讯

1
2
3
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https

启用防火墙

1
sudo ufw enable

使用下面的命令查看防火墙状态

1
sudo ufw status

安装Certbot

首先安装snapd,然后使用snapd安装Certbot

1
2
3
4
5
6
sudo apt update
sudo apt install snapd
sudo snap install core
sudo snap refresh core
sudo apt remove certbot
sudo snap install --classic certbot

创建链接

1
sudo ln -s /snap/bin/certbot /usr/bin/certbot

使用Certbot获取证书,并安装

  1. 运行Certbot获取证书

    • 获取证书并自动安装到Nginx(推荐) 这是推荐的方法,会在获取证书后,自动修改Nginx的配置文件,安装证书。 简单一步配置完成
    1
    
     sudo certbot --nginx
    

    ``

    • 获取证书但不安装到Nginx 这种方法仅仅获取证书,之后需要手工修改Nginx的配置文件,完成安装证书的过程。
    1
    
     sudo certbot certonly --nginx
    

    `` 2.输入email地址 3.接受服务条款 4.可选择订阅邮件列表 5.输入域名 6.然后 Certbot 与 Let’s Encrypt 通信以请求证书。

如果操作成功,Certbot 会确认证书已启用。 它还显示有关存储证书和密钥链的目录以及到期日期的一些信息。 证书通常在 90 天后到期。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Requesting a certificate for www.example.com
Performing the following challenges:
http-01 challenge for www.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/default
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/default

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://www.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
...
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.example.com/privkey.pem
our certificate will expire on 2021-05-29.

使用 Certbot 更新 TLS/SSL 证书

安装完成后,Certbot 被配置为自动更新任何证书。无需手动请求更新的证书或再次运行 Certbot。 Certbot 会自动更新所有证书。

记录并分享
Built with Hugo
主题 StackJimmy 设计