准备工作:
申请官网地址:Let’s Encrypt、Certbot
使用如上命令在对应服务器安装好certbot后,使用:sudo certbot certonly
命令安装,certbot (实际上是 certbot-auto ) 有两种方式生成证书:
按如下步骤进行:
$ sudo certbot certonly
Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
-------------------------------------------------------------------------------
1: Spin up a temporary webserver (standalone)
2: Place files in webroot directory (webroot)
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Plugins selected: Authenticator webroot, Installer None
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel): api.techeffic.com
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for api.techeffic.com
Select the webroot for api.techeffic.com:
-------------------------------------------------------------------------------
1: Enter a new webroot
-------------------------------------------------------------------------------
Press 1 [enter] to confirm the selection (press 'c' to cancel): 1
Input the webroot for api.techeffic.com: (Enter 'c' to cancel): /usr/local/yihui/
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/api.techeffic.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/api.techeffic.com/privkey.pem
Your cert will expire on 2018-03-06. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
需要注意的是,生成证书过程中,certbot会验证http://域名/.well-known/acme-challenge/28uFZcNfrnCeQObOvdZwKn_7dASGQckboRLns285zig
,需要搭配服务器来完成认证,认证通过后,会生成对应的证书/私钥到指定目录,如上面所示:Congratulations字样。
试图进入这个目录查看时,会有权限限制,重新授权后查看发现证书已经被软连接到:
/etc/letsencrypt/archive/api.techeffic.com/
sudo certbot renew //这个必须到期后才会刷新成功
#certbot renew --force-renew //强制刷新,就算没到期也刷新
sudo certbot renew --dry-run
如果出现:
则表示可以正常更新,如果要自动更新,将更新命令加入系统任务,或使用如下命令:
sudo certbot renew --quiet --no-self-upgrade
Let’s Encrypt于20180314开始支持通配符CA认证,即可以一次性给一个根域名下的所有子域名颁发受信的CA证书(如:给*.exmple.com颁发证书后,test.exmple.com和demo.exmple.com都被信任)
有了这个特性,对用户来说方便多了。Let’s Encrypt的通配符认证是通过ACME v2实现的。以下是为域名申请证书的过程(参考:https://my.oschina.net/kimver/blog/1634575):
使用与上面列出来的有点不同,我尝试用这种方法在申请过程出现下载jdk失败404错误,莫名其妙,所以不推荐用这种方法是正确的
# 下载wget https://dl.eff.org/certbot-auto
# 设为可执行权限
chmod a+x certbot-auto
# 注xxx.com请根据自己的域名自行更改
./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory-d "*.xxx.com" -d "xxx.com" --manual --preferred-challenges dns-01 certonly
执行完这一步之后,会下载一些需要的依赖,稍等片刻之后,会提示输入邮箱,随便输入都行【该邮箱用于安全提醒以及续期提醒】
执行到上图最后一步时,先暂时不要回车。申请通配符证书是要经过 DNS 认证的,接下来需要按照提示在域名后台添加对应的 DNS TXT 记录。添加完成后,先输入以下命令确认 TXT 记录是否生效:
$ dig -t txt _acme-challenge.xxx.com @8.8.8.8
确认生效后,回车继续执行,最后会输出Congratulations! 证书保存在/etc/letsencrypt/live/xxx.com/
,校验证书信息,输入如下命令:
$ openssl x509 -in /etc/letsencrypt/live/xxx.com/cert.pem -noout -text
# 可以看到证书包含了 SAN 扩展,该扩展的值就是 *.xxx.com
...
Authority Information Access:
OCSP - URI:http://ocsp.int-x3.letsencrypt.org
CA Issuers - URI:http://cert.int-x3.letsencrypt.org/
X509v3 Subject Alternative Name:
DNS:*.xxx.com
...
到此
source ~/.bashrc
curl https://get.acme.sh | sh
# 替换成从阿里云后台获取的密钥
export Ali_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
export Ali_Secret="jlsdflanljkljlfdsaklkjflsa"
# 换成自己的域名
acme.sh --issue --dns dns_ali -d zhuziyu.cn -d *.zhuziyu.cn
这里是通过线程休眠120秒等待DNS生效的方式,所以至少需要等待两分钟,正常情况下等待完成后,出现下面的内容
到了这一步大功告成,撒花。
acme.sh比certbot的方式更加自动化,省去了手动去域名后台改DNS记录的步骤,而且不用依赖Python,墙裂推荐。
第一次成功之后,acme.sh会记录下App_Key跟App_Secret,并且生成一个定时任务,每天凌晨0:00自动检测过期域名并且自动续期。对这种方式有顾虑的,请慎重,不过也可以自行删掉用户级的定时任务,并且清理掉~/.acme.sh文件夹就行
关于将证书复制到某个目录或更新问题,参考:使用 acme.sh 制作 HTTPS 证书