安装

安装Snapd

  • 添加CentOS repository

Centos 8

sudo dnf install epel-release
sudo dnf upgrade

Centos 7

sudo yum install epel-release
  • 添加EPEL repository后,进行Snapd的安装
sudo yum install snapd
  • 安装后,需要启用管理snap通信套接字的systemd unit
sudo systemctl enable --now snapd.socket
  • 为了启用classic snap的支持,需要创建如下软连接
sudo ln -s /var/lib/snapd/snap /snap

安装Certbot

  • 安装
sudo snap install --classic certbot
  • 配置Certbot命令行
sudo ln -s /snap/bin/certbot /usr/bin/certbot

运行Certbot

方法一,运行此命令获取证书,并让Certbot自动编辑Nginx配置以提供服务,只需一步即可打开HTTPS访问

sudo certbot --nginx

注:Certbot默认nginx配置文件在 /etc/nginx/nginx.conf/usr/local/etc/nginx/nginx.conf,若你的nginx配置文件不在此处(以/usr/local/nginx/conf/nginx.conf为例),需在命令后加上 --nginx-server-root /usr/local/nginx/conf

方法二,仅获得证书。如果你希望手动配置nginx,输入如下命令

sudo certbot certonly --nginx
  • 自动续期

Certbot包带有cron作业或systemd计时器,它将在证书过期之前自动续订证书。除非更改配置,否则不需要再次运行Certbot。通过运行以下命令,可以测试证书的自动续订

sudo certbot renew --dry-run

renew的命令被配置到以下位置中的一个:

/etc/crontab/
/etc/cron.*/*
systemctl list-timers

举例:

certbot --nginx --nginx-server-root /www/server/nginx/conf -d xxx.example.com

code

根据提示选择1

注:可能第一次生成后,打开https链接提示证书不安全,需要重新再生成一遍,选择2即可。

可以通过下面命令查看指令参数

certbot -h