acmephp获取let's encrypt证书

Acme php是一个简单而强大的命令行工具,可以自由自动地获取和续订Let's Encrypt HTTPS证书。
Acme php也是php中Acme协议的一个健壮且完全兼容的实现,可以直接在应用程序中深度集成证书管理。
一、Acme php安装
Acme php可作为单个PHAR文件在Github上下载。出于安全考虑, 此PHAR文件使用OpenSSL进行签名,以确保您使用的是有效的Acme php二进制文件。
您可以通过运行以下命令来安装它:
cd ~
php -r "copy('https://github.com/acmephp/acmephp/releases/download/1.0.1/acmephp.phar', 'acmephp.phar');"
php -r "copy('https://github.com/acmephp/acmephp/releases/download/1.0.1/acmephp.phar.pubkey', 'acmephp.phar.pubkey');"
php acmephp.phar --version
最新的开发版本—虽然我们强烈建议您使用稳定(或至少预发布)版本,但您也可以使用最新的开发版本,如果您需要最新功能。
您可以通过运行以下命令来安装它:
cd ~
php -r "copy('https://acmephp.github.io/downloads/acmephp.phar', 'acmephp.phar');"
php -r "copy('https://acmephp.github.io/downloads/acmephp.phar.pubkey', 'acmephp.phar.pubkey');"
php acmephp.phar --version
php acmephp.phar --version,执行后如果显示了版本,证明安装成功。
二、Acme php获取Let's Encrypt证书(配置文件config.yml)
注意: 这是官方推荐的申请证书的方式,您也可以多个命令一步一步获取证书。
创建配置文件config.yml
contact_email: contact@company #联系您的电子邮件
defaults: #定义默认的证书配置选项
distinguished_name:
country: FR #国家
locality: Paris #地区
organization_name: MyCompany #组织名称
solver: http #验证域名所有权的方法
certificates: #要生成和管理的 SSL 证书的配置信息
- domain: example.com #要生成证书的域名
distinguished_name:
organization_name: MyCompany Internal #组织名称
solver: route53 #使用route53验证方法
subject_alternative_names: #证书的备用主题名称
- '*.example.com'
- www.subdomain.example.com
install: #安装证书的动作
- action: install_aws_elb
region: eu-west-1
loadbalancer: my_elb
listener: 443
- domain: www.example.com #要生成证书的域名
solver:
name: http-file #使用http-file验证方法
adapter: ftp #您可以选择使用FTP、SFTP或local本地文件系统作为存储选项, see https://flysystem.thephpleague.com/
root: /var/www/
host: ftp.example.com #上面选择了ftp,所以这里配置的是ftp的信息
username: username
password: password
# port: 21
# passive: true
# ssl: true
# timeout: 30
# private_key: path/to/or/contents/of/privatekey
获取Let's Encrypt证书
执行命令:
php acmephp.phar run config.yml
将执行:
- 在 Let's Encrypt/ACME 服务器中注册您的帐户密钥,并将其与您的电子邮件地址相关联
- 对于文件中配置的每个证书
- 向 ACME 服务器请求令牌,并要求配置的求解器公开令牌
- 本地检查令牌是否公开良好
- 要求 ACME 服务器验证域
- 要求 ACME 服务器生成证书
- 使用配置的操作安装证书
三、获取Let's Encrypt证书(使用命令)
配置文件也相当复杂,不是吗,如果使用命令来创建证书,又是怎样的?
1、在 Let's Encrypt/ACME 服务器上注册
需要生成一个全局私钥(您的“帐户密钥”),它将用于将来与 Let's Encrypt 的交流,以证明您的身份。
请运行以下命令:
php acmephp.phar register youremail@example.com
此命令将执行三项主要操作:
- 创建默认配置文件 ~/.acmephp/acmephp.conf 您可以编辑 Acme PHP 的高级用法
- 为您的账户生成密钥对并将其存储在 ~/.acmephp/master/account
- 在 Let's Encrypt/ACME 服务器中注册您的帐户密钥,并将其与您的电子邮件地址相关联
2、证明您拥有该域名
您只能请求您拥有的域的证书,因此您需要向Let's Encrypt服务器证明您拥有该域。
原理很简单:
- 您向服务器请求令牌,并将其公开在服务器给定的特定URL上。
- 服务器检查URL并验证令牌是否在那里正确公开。
注意:这种技术被称为HTTP检查,因为它使用HTTP请求来检查您的所有权。您也可以使用DNS检查,该检查使用DNS TXT字段来公开令牌。
您需要定期证明您拥有域名(证书续订可能需要新令牌)。
获取并公开令牌:
php acmephp.phar authorize yourdomain.org
运行此命令后,工具将显示有关如何公开令牌的说明。通常,只需将令牌放在一个简单的文本文件中,以便验证器可以访问它。
3、要求服务器检查您的证明
运行以下命令询问服务器 要检查您的证明:
php acmephp.phar check yourdomain.org
服务器将检查要公开的令牌的URL。如果成功,您就可以申请此域名的证书
4、获取证书
您是域名的证明所有者,您现在可以申请和续订此域名的证书。运行request命令获取证书:
php acmephp.phar request yourdomain.org
获取的证书:
/.acmephp/master/certs/yourdomain.org/fullchain.pem。 完整的证书链文件,它包括了您的域名证书和中间证书,但不包括根证书。apache下通常会使用此证书。
/.acmephp/master/private/yourdomain.org/private.pem。与证书对应的私钥文件,私钥用于解密通过公钥加密的数据。apache下通常会使用此证书。
/.acmephp/master/certs/yourdomain.org/chain.pem。证书链文件,它包含了整个证书链,从您的域名证书到根证书的所有中间证书。
/.acmephp/master/certs/yourdomain.org/cert.pem。您的域名的公钥证书文件,它包含了用于加密通信的公钥。
/.acmephp/master/certs/yourdomain.org/combined.pem。合并的证书,一些 Web 服务器需要将全链和证书私钥组合在一起的单个文件。
/.acmephp/master/private/yourdomain.org/public.pem。 证书公钥。
5、续订证书
如果证书即将过期,重新运行request命令:
php acmephp.phar request yourdomain.org
默认情况下, request命令直到证书到期前一周才会续订。因此,您 可以每天将其作为 CRON,并且仅在需要时才会更新。
证书续订不会询问任何信息,因为第一次申请证书的时候已经提交过了,它会记录下来。
6、陈列acme php处理过的证书
如果您想查询使用acme php处理过的证书,使用status命令:
php acmephp.phar status
结果示例:
+---------------+----------------------------+---------------------+---------------------+----------------+
| Domain | Issuer | Valid from | Valid to | Needs renewal? |
+---------------+----------------------------+---------------------+---------------------+----------------+
| acmephp.com | Let's Encrypt Authority X3 | 2016-06-17 13:08:00 | 2016-09-15 13:08:00 | No |
+---------------+----------------------------+---------------------+---------------------+----------------+
四、设置自动续订Let's Encrypt证书
Let's Encrypt证书的有效期为3个月,所以,自动化续订是一个很不错的功能,而Let's Encrypt提供了自动续订的功能。 Acme php从一开始就构建了易于放入CRON的想法。
CRON是一个Linux下的定时执行工具,全称为“Cron Table”,它可以根据配置文件约定的时间来执行特定的任务。在Linux系统中,计划任务一般是由cron承担,可以把cron设置为开机时自动启动。cron启动后,它会读取它的所有配置文件(全局性配置文件/etc/crontab,以及每个用户的计划任务配置文件),然后cron会根据命令和执行时间来按时调用度工作任务。
CRON的配置文件包含了一系列定时任务,每个任务由6个字段组成,分别表示分钟、小时、日期、月份、星期几以及要执行的命令。这6个字段可以使用特定的符号来表示范围或特定值,例如“*”表示任意值,“?”表示不特定指代某些值,“/”表示增量,“L”表示最后几天,“W”表示工作日等。
创建CRON定时任务
Acme php很智能,它知道何时续订证书:您不需要仔细考虑何时安排CRON任务,只需每天运行它,Acme php就会检查证书是否需要续订。
注意:绝大多数Web服务器在证书更新后需要重新启动或重新加载,因此您应该以root身份运行CRON。如果您不想以root身份执行AcmePHP,也可以创建两个独立的CRON。
示例:
0 0 * * * php /home/youruser/acmephp.phar request example.com >/dev/null 2>&1 && service httpd restart
0 0 * * *:表示系统将于每天0点0分执行此任务;
php /home/youruser/acmephp.phar request yourdomain.org: 这是要执行的命令。它使用php解释器来执行 /home/youruser/acmephp.phar 文件,并将 request example.com作为参数传递给它。
>/dev/null 2>&1: 这部分是重定向输出的语法,将标准输出和标准错误输出都重定向到 /dev/null,即将输出丢弃,不会显示在终端上;
&& service httpd restart:表示如果上一个命令执行成功,则执行service httpd restart命令,重启httpd服务。请根据软件版本号更改此命令,如果您使用nginx系统,该命令可能不适用,例如改为service nginx reload。
如果您希望将输出保存到日志文件中:
0 0 * * * php /home/youruser/acmephp.phar request example.com >> /var/log/acmephp.log && service httpd restart
配置监视
Acme php的一个非常强大的功能是监视:当续订失败时,CLI客户端会向您发出警告。提供不同的警报,如电子邮件或Slack。
要启用监视,您需要编辑Acme PHP配置文件(~/.acmephp/acmephp.conf):
###################################################################
# Monitoring
#
# This section let you configure a simple monitoring mechanism that
# will warn you if an error occurs during a CRON job.
#
monitoring: ~ # Monitoring is disabled by default
# You can enabled it by configuring at least one alert handler.
# You can change the default handler level to decide when to be alerted
# (only when an error occurs or every time the CRON is started).
#monitoring:
# email: # Only SMTP(S) support for now
# to: galopintitouan@gmail.com
# host: smtp.example.com
# # port: 25
# # username: ~
# # password: ~
# # encryption: ~
# # subject: An error occured during Acme PHP CRON renewal
# # level: error # By default, only on error for email handler
#
# slack:
# token: your_token
# channel: #general
# # username: Acme PHP
# # level: info # By default, on every CRON for slack handler
此配置非常简单:只需删除 monitoring: ~ 行,取消对监控部分的注释 并根据您的需要进行编辑。
配置您的邮箱信息,这里不使用slack,示例:
###################################################################
# Monitoring
#
# This section let you configure a simple monitoring mechanism that
# will warn you if an error occurs during a CRON job.
#
# monitoring: ~ # Monitoring is disabled by default
# You can enabled it by configuring at least one alert handler.
# You can change the default handler level to decide when to be alerted
# (only when an error occurs or every time the CRON is started).
monitoring:
email:
to: your-email@example.com
host: smtp.example.com
port: 25
username: your_username
password: your_password
encryption: ssl
subject: An error occured during Acme PHP CRON renewal
level: error
# slack:
# token: your_token
# channel: #general
# # username: Acme PHP
# # level: info # By default, on every CRON for slack handler
请注意,监视警报处理程序有两个级别:
info level 表示处理程序将在每次续订时触发,即使它成功了;
Error level 表示只有在续订过程中发生错误时才会触发处理程序;
使用DNS验证您拥有某个域名
默认情况下,Acme php将使用http验证来证明您拥有某个域名,您将创建一个Acme服务器将访问的文件来验证您公开的令牌。
您也可以使用DNS来检查您对域名的所有权,而不是公开一个文件,您将公开一个TXT字段。
获取要公开的令牌
要使用DNS验证,请运行带有--solver dns参数的authorize命令:
php acmephp.phar authorize --solver dns yourdomain.org
结果示例:
The authorization token was successfully fetched!
Add the following TXT record to your DNS zone
Domain: _acme-challenge.yourdomain.org.
TXT value: zExIQaKdxouvZ6rbpYApsawiwgINj3zbmXym-KhuNsA
Wait for the propagation before moving to the next step
Tips: Use the following command to check the propagation
host -t TXT _acme-challenge.yourdomain.org.
Then, you can ask to the CA to check the challenge!
Call the check command to ask the server to check your URL:
php acmephp.phar check -s dns yourdomain.org
为令牌添加 DNS TXT 字段
下面的操作是必须的:
Add the following TXT record to your DNS zone
Domain: _acme-challenge.yourdomain.org.
TXT value: zExIQaKdxouvZ6rbpYApsawiwgINj3zbmXym-KhuNsA
要求服务器检查您的证明
完成后,等待几分钟以进行DNS传播。字段准备就绪后,运行带有check命令的 --solver dns
php acmephp.phar check --solver dns yourdomain.org