如何在CentOS 7上安装Letsencrypt证书
本教程介绍了如何在CentOS 7上安装Letsencrypt SSL证书7.让我们的加密是一个证书颁发机构,可免费提供SSL/TLS证书。
Let’s Encrypt 通过自动化进程提供可信证书,而无需任何成本。
准备工作和要求
要安装Let的加密证书,我们需要与管理权限的CentOS系统进行shell访问。
防火墙配置:确保从防火墙中允许HTTP和HTTPS服务:
firewall-cmd --permanent --add-service http firewall-cmd --permanent --add-service https firewall-cmd --reload
我们还假设我们已经有一个虚拟主机为我们的HTTP(非安全)版本配置了:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog "logs/example.com-error_log" CustomLog "logs/example.com-access_log" combined </VirtualHost>
在此过程中,将根据此配置自动创建HTTPS站点的新虚拟主机配置。
DNS ......
为了证明的所有权,我们必须确保存在DNS条目,因此可以通过其完全合格的域名(FQDN)达到:
如果在安装证书之前无法将指向服务器,则可以使用DNS-01验证方法来证明域名的所有权。
如何在移动DNS之前获取Let’s Encrypt 证书(DNS-01验证)
怎么做...
执行以下步骤以在CentOS 7上为Apache Web服务器安装Letsencrypt证书:
- 安装Certbot客户端。
- 安装证书。
- 验证虚拟主机文件。
- 自动化更新过程。
CERTBOT命令行工具允许我们请求新的证书并续订。
首先启用EPEL存储库安装CERTBOT客户端:
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm yum install python2-certbot-apache
要为安装SSL/TLS证书,请运行以下命令(在命令中根据需要修改域名):
certbot --apache -d example.com -d www.example.com
当我们第一次运行Certbot命令时,我们必须提供有关我们证书的对应关系的有效电子邮件地址。
然后,我们应该必须同意服务条款(按键盘上的A在键盘上,然后按Enter键接受服务条款):
(A)gree/(C)ancel: A
作为最后一步,我们将被要求选择是否将HTTP流量重定向到HTTPS。
此时,我们不应将HTTP流量重定向到HTTPS。
所以回答1没有重定向。
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
这就是我们需要操作的全部,以安装让我们在CentOS 7上加密证书。
我们将在/etc/httpd/conf.d目录下找到https站点的虚拟主机文件(例如,examply.com-le-ssl.conf):
<IfModule mod_ssl.c> <VirtualHost *:443> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog "logs/example.com-error_log" CustomLog "logs/example.com-access_log" combined SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem </VirtualHost> </IfModule>
自动化续约
发布的证书有效期为90天。
我们可以运行certbot renew命令续订CentOS 7服务器上的所有证书。
certbot renew
我们可以设置一个Cron作业(预定任务),以在临近到期时自动续订证书。
例如,在/etc/cron.d目录下创建一个名为certbot的新文件,并在一行上添加以下内容:
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew
这集合CRON作业每12小时执行CERTBOT RENEW命令,并将续订临近到期的所有获得的证书。
请注意,SSL证书,私钥,帐户凭据和其他所有内容都保存在/etc/letsencrypt中的Certbot配置目录中。
确保保留此文件夹的定期备份。