让我们使用Let’s Encrypt来获取SSL证书(ubuntu20.04+nginx+certbot)

由于存在相似的文章,因此需要一份完备的备忘录。

以下是环境。

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

为了方便快速确认,解释会尽量简略。

安装

sudo apt install certbot python3-certbot-nginx

防火墙

注意防火墙未打开。

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

sudo ufw status

只要输出结果如下,就可以了。

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

获取

我记得刚开始的时候应该有询问我的电子邮件地址之类的。

sudo certbot --nginx -d example.com
sudo certbot --nginx -d example2.com -d xxx.example2.com -d yyy.example2.com  # 複数まとめて指定したい場合.こうすると証明書もひとつになる

配置

nginx配置文件。我找到它在 /etc/nginx/conf.d。

在适当的调整后,就会变得很稳定。由Certbot管理的那个用Certbot自动编写好的东西被重复利用了。还带有将http重定向到https的附加功能。

我正在使用Go语言搭建应用程序服务器并作为反向代理运行,所以location是按照这个规范设置的。

server {
    listen 80;
    server_name  example.com;
    return 301 https://$host$request_uri;
}
server{
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        proxy_pass http://127.0.0.1:xxxx;
    }
}
bannerAds