如何在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
Theme Stack designed by Jimmy