Linux下使用acme.sh 配置https 免费证书
简单来说acme.sh 实现了 acme 协议, 可以从 let‘s encrypt 生成免费的证书。
acme.sh 有以下特点:
一个纯粹用Shell(Unix shell)语言编写的ACME协议客户端。
完整的ACME协议实施。 支持ACME v1和ACME v2 支持ACME v2通配符证书
简单,功能强大且易于使用。你只需要3分钟就可以学习它。
Let's Encrypt免费证书客户端最简单的shell脚本。
纯粹用Shell编写,不依赖于python或官方的Let's Encrypt客户端。
只需一个脚本即可自动颁发,续订和安装证书。 不需要root/sudoer访问权限。
支持在Docker内使用,支持IPv6
Github链接:https://github.com/acmesh-official/acme.sh
本文记录了我在把网站从HTTP升级到 HTTPS ,申请和安装SSL证书路上踩过的几个坑。
安装环境:
操作系统:centos 7 X64
SSL证书来源:Let's Encrypt
安装用脚本:acme.sh
服务器:nginx
域名:*.abc.com
1、安装acme.sh
# yum -y isntall curl
# curl https://get.acme.sh | sh
出现了红色的提醒:
It is recommended to install socat first
We use socat for standalone server if you use standalone mode.
If you don't use standalone mode, just ignore this warning.
大概意思就是叫我最好安装 socat
yum install epel-release -y
yum install socat -y
curl https://get.acme.sh | sh
检查是否安装好
#直接运行
acme.sh
#如报错请运行如下
source ~/.bashrc
安装完成后的工作目录在当前用户的.acme.sh【~/.acme.sh】
2、安装后的配置
把 acme.sh 安装到你的 home 目录下:~/.acme.sh/并创建 一个 bash 的 alias, 方便你的使用:
alias acme.sh=~/.acme.sh/acme.sh
echo 'alias acme.sh=~/.acme.sh/acme.sh' >>/etc/profile
安装过程中会自动为你创建 cronjob, 每天 0:00 点自动检测所有的证书, 如果快过期了, 需要更新, 则会自动更新证书(可执行crontab -l 查看)。
13 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
3、获取阿里云的ID和Secret
# 替换成从阿里云后台获取的密钥
export Ali_Key="LTAI4G***********Pp8dZVzS"
export Ali_Secret="8GaI2***************zgzfwmhTyb732"
# 添加完成后
~/.acme.sh/account.conf能看到阿里的ID和Secret
其他地方注册的请参考这里
4、生成证书
新版本acme.sh --register-account -m 邮箱 --server zerossl
https://github.com/acmesh-official/acme.sh/wiki/ZeroSSL.com-CA
# 换成自己的域名
阿里云
acme.sh --issue --dns dns_ali -d abc.com -d *.abc.com
[Mon Apr 30 15:35:14 CST 2018] Your cert is in /root/.acme.sh/xxx.com/xxx.com.cer
[Mon Apr 30 15:35:14 CST 2018] Your cert key is in /root/.acme.sh/xxx.com/xxx.com.key
[Mon Apr 30 15:35:14 CST 2018] The intermediate CA cert is in /root/.acme.sh/xxx.com/ca.cer
[Mon Apr 30 15:35:14 CST 2018] And the full chain certs is there: /root/.acme.sh/xxx.com/fullchain.cer
DNSPOD
export DP_Id=""
export DP_Key="*********"
acme.sh --issue --dns dns_dp -d abc.com -d *.abc.com
申请的证书会放到 ~/.acme.sh/ 目录里面,所有的 acme.sh 配置都记录在 ~/.acme.sh/ 目录里面,acme.sh 有自动的配置读取,并按域名划分,下次你再次执行的时候,它知道你之前是用的那个目录,只需要告诉它域名就好了。
5、安装证书
注意, 默认生成的证书都放在安装目录下: ~/.acme.sh/, 请不要直接使用此目录下的文件,
例如: 不要直接让 nginx/apache 的配置文件使用这下面的文件.
这里面的文件都是内部使用, 而且目录结构可能会变化.
正确的使用方法是使用 --installcert 命令,并指定目标位置, 然后证书文件会被copy到相应的位置,
默认情况下,证书将每60天更新一次(可配置)。更新证书后,将通过以下命令自动重新加载Apache / Nginx服务:service apache2 force-reload
或service nginx force-reload
。
请注意:reloadcmd非常重要。证书可以自动续订,但是,如果没有正确的“reloadcmd”,证书可能无法刷新到您的服务器(如nginx或apache),那么您的网站将无法在60天内显示续订证书。
acme.sh --install-cert -d abc.com
--key-file /usr/local/nginx/conf/ssl/abc/abc.com.key
--fullchain-file /usr/local/nginx/conf/ssl/abc/abc.com.cer
--reloadcmd "/usr/local/nginx/sbin/nginx -s reload"
[Mon Apr 30 16:19:32 CST 2018] Reload success
指定了存放证书的目录,nginx配置文件会配置我这里是创建了一个key文件
6、配置nginx
server {
listen 80;
listen 443 ssl;
server_name h5.7dianchina.com;
access_log /var/log/nginx/qidian_h5.log;
ssl on;
ssl_certificate key/abc.com.cer;
ssl_certificate_key key/abc.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
参考:https://www.jianshu.com/p/b6b172f69c14
https://www.jianshu.com/p/06e3aba8c62e
https://www.derror.com/p/configure-https-for-nginx-using-acmesh/
https://my.oschina.net/kimver/blog/1634575
时间到期续费证书
acme.sh --renew -d baoko.cn --webroot /www/nginx/key/
openssl x509 -in abc.crt -noout -dates