CentOS安装snapd
sudo yum install epel-release
sudo yum install snapd
sudo systemctl enable --now snapd.socket
安装Certbot
sudo snap install --classic certbot
sudo yum remove certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
安装证书
sudo certbot --nginx
- 如果配置文件存在于非标准位置/etc/nginx/nginx.conf,可以使用 --nginx-server-root 参数来指定 Nginx 配置文件的路径,注意指定到nginx.conf所在的文件夹即可,例如/opt/nginx/conf/nginx.conf
sudo certbot --nginx --nginx-server-root=/opt/nginx/conf
sudo certbot certonly --nginx
- 如果出现The error was:PluginError('Nginx build is missing SSL module (-- with-http_ssl_module).'),说明安装Nginx时没有ssl_module,可以使用预编译的版本或用yum重新安装,如果自己编译记得--with-http_ssl_module,举个栗子:
sudo ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/root/file/fastdfs-nginx-module/src --with-http_ssl_module
sudo make && sudo make install
nginx -V 2>&1 | grep -- '--with-http_ssl_module'
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
这里输入邮箱会收到紧急续订和安全通知
Would you be willing, once your first certificate is successfully issued, to
这个yn都行
Which names would you like to activate HTTPS for?
为哪些激活,直接回车全选即可,后面哪个不需要自己改写nginx.conf即可
sudo certbot renew --dry-run
使用
- 编辑nginx.conf文件,会发现server {}里多了几行监听443和密钥位置的配置,底部还有重定向的,不需要重定向可以删掉,例如接口,需要同时http和https复制一个server {}没有监听443的即可。
- 记得重启nginx
sudo nginx -t
sudo systemctl reload nginx
nginx
nginx -s stop
nginx -s reload