Centos7安装certbot获得并更新证书(使用Snap)

先来看张图

图片.jpg

是不是有很多小伙伴,访问一些网站的时候,会遇到这样的提示,这时候可以看到提示:

此服务器无法证明其所在网域是 www.xxxxx.com;其安全证书已在 xx 天前过期。出现此问题的原因可能是配置有误,或是有攻击者拦截您的连接。攻击者可能会试图从 www.xxxxx.com 窃取您的信息(例如:密码、通讯内容或信用卡信息)

这时候很多访问者,就会选择不访问这个网站,这时候网站访问量就会受到影响,那么接下来博主就会教大家,如何给自己的小站,上一把安全锁🔒

背景

目前certbot不推荐在centos7上使用仓库安装,官方建议使用snap进行certbot的安装和更新。官方原文如下:

原文:

While the Certbot team tries to keep the Certbot packages offered by various operating systems working in the most basic sense, due to distribution policies and/or the limited resources of distribution maintainers, Certbot OS packages often have problems that other distribution mechanisms do not. The packages are often old resulting in a lack of bug fixes and features and a worse TLS configuration than is generated by newer versions of Certbot. They also may not configure certificate renewal for you or have all of Certbot’s plugins available. For reasons like these, we recommend most users follow the instructions at https://certbot.eff.org/instructions and OS packages are only documented here as an alternative.

翻译:

译文:

Certbot团队试图使各种操作系统提供的Certbot包在最基本的意义上保持工作,但由于分发策略和/或分发维护人员的资源有限,Certbot OS包通常存在其他分发机制无法解决的问题。这些包通常比较旧,导致缺少bug修复和功能,并且TLS配置比更新版本的Certbot更糟糕。他们也可能没有为您配置证书更新,或者Certbot的所有插件都可用。出于这些原因,我们建议大多数用户按照https://certbot.eff.org/instructions 和操作系统软件包仅作为替代文件记录在此处。

安装Snapd

官方给出的文档https://snapcraft.io/docs/installing-snapd,这里翻译一下CentOS版本

Snap在CentOS 7.6版本以上均可用,如果不知道你的版本,使用如下命令查看

cat /etc/centos-release
  • 添加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

重新登录或者重启你的系统,以确保snap的路径正确更新,至此snap安装完成。

安装Certbot

  • 升级snap

执行如下命令以保证Snap为最新的版本

sudo snap install core
sudo snap refresh core
  • 卸载Certbot和其他Certbot OS包

如果使用操作系统包管理器(如apt、dnf或yum)安装了任何Certbot包,则应在安装Certbot snap之前将其删除,以确保在运行命令Certbot时使用snap,而不是从操作系统包管理器安装。执行此操作的确切命令取决于你的操作系统,常见的示例有sudo apt get remove certbot、sudo dnf remove certbot或sudo yum remove certbot。

如果以前通过Certbot auto脚本使用Certbot,还应该按照此处的说明删除其安装。

  • 安装Certbot
sudo snap install --classic certbot
  • 配置Certbot命令行

执行如下命令以确保Certbot命令行可用

sudo ln -s /snap/bin/certbot /usr/bin/certbot
  • 运行Certbot(二选一) 

1、运行此命令获取证书,并让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

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

sudo certbot certonly --nginx

图片.jpg

图片.jpg

注:同上

  • 自动续期

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

sudo certbot renew --dry-run

图片.jpg

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

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

写在最后

入门使用就这些,更高深的用法在实践中去发现吧。。。
THE END
分享
二维码
打赏
< <上一篇
下一篇>>