用certbot获取免费https证书

环境

CentOS 7.9

nginx/1.13.7

背景

建站时有必要让域名也能被https访问,这就需要在nginx中配置 443端口的转发 同时也需要https的证书支持,也有其他场景需要https的证书,故本文提供一种快速免费获得https证书的方法:使用certbot获取。

操作步骤

步骤参考以下文章:

https://certbot.eff.org/instructions?ws=nginx&os=centosrhel7

https://snapcraft.io/docs/installing-snap-on-centos

https://www.cnblogs.com/hushuning/p/16987289.html

1 安装snap

1.1 安装EPEL

yum install epel-release

1.2 安装Snapd

yum install snapd

注:Snap是一种软件包管理系统,用于安装、更新和管理应用程序。Snap是由Canonical开发的,它是Ubuntu的默认软件包管理器,但也可以在其他Linux发行版上使用。

# snap 管理软件的一些命令
# 查看已安装的软件包
snap list
# 删除已安装的软件
snap remove <package-name>
# 安装软件
snap install <package-name>

1.3 启动snapd.socket的systemd unit

安装snapd成功后, 管理 main snap communication socket 的 systemd unit 需要被启用

systemctl enable --now snapd.socket

1.4 创建软链接使snap命令可用

ln -s /var/lib/snapd/snap /snap

1.5 check是否成功

在任意位置输入

snap --version && snap --help

2 安装certbot

2.1 先删除已有certbot

yum remove certbot && snap remove certbot

2.2 安装certbot

sudo snap install --classic certbot

2.3 创建软链接使certbot命令可用

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

2.4 测试certbot是否安装成功

certbot --version && certbot --help

2.5 生成证书

sudo certbot certonly --nginx

跟着提示操作即可,傻瓜式生成。获得证书和key的url在nginx.conf中配置即可。

这里会有一个坑:

可能会有以下回报

Saving debug log to /var/log/letsencrypt/letsencrypt.log
The nginx plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError("Could not find a usable 'nginx' binary. Ensure nginx exists, the binary is executable, and your PATH is set correctly.",)

certbot有它自己的找nginx的逻辑,上面提示信息显示没有找到nginx,那么需要将nginx放到环境变量中,并设置nginx软连接

# 这里前提是nginx是安装在/usr/local/nginx下的
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx

2.6 查看已生成的证书

certbot certificates

会返回你的对应域名 过期时间 证书和key的Path

另:关于证书过期与更新

来自Certbot官网 https://certbot.eff.org/instructions?ws=nginx&os=centosrhel7

The Certbot packages on your system come with a cron job or systemd timer that will renew your certificates automatically before they expire. You will not need to run Certbot again, unless you change your configuration.

您系统上的 Certbot 软件包附带一个 cron 作业或 systemd 计时器,可在证书过期之前自动更新您的证书。 您不需要再次运行 Certbot,除非您更改配置。

防止他这个自动续期不靠谱,下面是手动续期的命令:

certbot renew
  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值