Linux 技术手札

RHEL / CentOS 安装 Certbot 取得及更新 Let’s Encrypt 凭证

Let’s Encrypt 推出免费 SSL 凭证,让想使用 SSL 的网站节省不少成本,而且使用 Let’s Encrypt 提供的客户端申请及 renew 凭证,比起在网上购买更加方便。虽然 Let’s Encrypt 的凭证每次只可以申请 3 个月,但可以使用 crontab 自动 renew 凭证,使用上也是很方便。以下介绍在 RHEL 及 CentOS 7 安装 Let’s Encrypt 客户端 Certbot,及使用 Certbot 申请及 renew 凭证 的方法。

1. 安装 Certbot:

在 CentOS 7 要安装 Certbot, 只要启用 EPEL 便可以用 yum 安装,如果没有启动 EPEL Repo, 执行以下指令启用:

# yum install epel-release -y

然后用 yum 安装所需套件,以下假设设定 Apache 支援 SSL, 需要安装 mod_ssl:

# yum install mod_ssl

现在可以安装 Certbot:

# yum install certbot -y

2. 取得凭证

现在假设要申请 www.mydomain.com 及 mydomain.com 的凭证,先要在 DNS Server 将以上 hostname 指向服务器的 IP,Let’s Encrypt 也会用 hostname 验证是否域名持有人,而假设网页的目录在 /var/www/html, 可以执行以下指令:

当验证成功后,SSL 凭证, private key 及 LE chain 会放在 /etc/letsencrypt/live/www.mydomain.com/ 下面。

3. 设定 Apache

开启档案 /etc/httpd/conf.d/ssl.conf:

# vi /etc/httpd/conf.d/ssl.conf

找到 SSLCertificateFile, SSLCertificateKeyFile 及 SSLCACertificateFile 这 3 行,这 3 行是分开的,改成这样:

最后重新启动 Apache 便会生效:

# systemctl restart httpd

4. 设定自动 renew SSL 凭证

Let’s Encrypt 凭证目前的有效期只有 3 个月,在凭证到期前 1 个月可以 renew以下设定自动 renew 凭证,以下是更新凭证的指令:

以上指令会自动更新所有 SSL 凭证,如果成功更新,会执行 “systemctl reload httpd” 这条指令,让 apache 重新加载新凭证。

为了日后方便管理,建立一个 renew SSL 的 Shell Script:

# vi /root/renew.sh

加入以下内容:

然后把上面 Shell Script 加入可执行权限及放到 crontab:

# chmod +x /root/renew.sh

加入 crontab

# crontab -e

加入以下一行:

这样 certbot 便会在每天凌晨 3:00 自动检查及更新凭证。

Exit mobile version