简单三步给网站申请安装letsencrypt免费SSL证书-acme.sh自动续期

昨天晚上花了一个小时的时间把挖站否VPS监控SSL证书由原来的TrustAsia即赛门铁克(Symantec)个人DV SSL证书换成了letsencrypt免费SSL证书,原因是Chrome 70开始将开始不再信任 Symantec证书了,自己正在用的Chromium浏览器已经不能再打开Symantec证书的网站了。

赛门铁克(Symantec)个人DV SSL证书是在腾讯云申请的,实际上国内的阿里云、七牛云又拍云等都提供了免费一年赛门铁克(Symantec)个人DV SSL证书。自己犯了错,干脆把之前的SSL产品趁“打烊”之前清仓大处理了?这也怪不得赛门铁克(Symantec)在国内搞了这么多的免费推广

本篇文章就来分享一下利用acme.sh来一键申请和安装letsencrypt免费SSL证书,基本上可以适合各大VPS主机服务器,而且acme.sh可以自动更新SSL证书,再也不用担心SSL证书会过期的问题的。从我的个人使用经验来看,官方推荐的Certbot还是不如acme.sh好用一些。

简单三步给自己的网站申请和安装letsencrypt免费SSL证书

更多的VPS主机SSL证书还可以看看:

  1. 免费SSL证书收集整理汇总-免费给网站添加Https安全加密访问
  2. CN2 GIA VPS主机收集整理汇总-电信,联通,移动三网CN2 GIA线路VPS主机
  3. 启用HSTS并加入HSTS Preload List让网站Https访问更加安全-附删除HSTS方法

PS:更新记录.

1、acme.sh是一个非常优秀的免费SSL申请与管理工具,详细教程:acme.sh支持免费SSL证书整理汇总-acme.sh安装和手动切换SSL CA方法。2022.8.20

2、华为云域名DNS解析已经可以使用acme.sh ,方法参考:华为云域名DNS解析已经可以使用acme.sh 免费签发letsencrypt免费SSL证书。2021.1.22

2、想要SSL访问获得更快的速度以及更高的性能,可以试试TLSV1.3和Brotli压缩:网站优化加速-开启TLSV1.3和Brotli压缩-Oneinstack,LNMP,宝塔面板。2019.1.15

一、第一步:一键安装acme.sh

网站:

  1. https://letsencrypt.org/
  2. https://github.com/Neilpang/acme.sh

使用以下可以一键安装:

curl https://get.acme.sh | sh

或者:

wget -O -  https://get.acme.sh | sh

使用喜欢Git命令也可以用以下命令安装:

git clone https://github.com/Neilpang/acme.sh.git
cd ./acme.sh
./acme.sh --install

脚本主要做了以下事情:

1.生成复制了acme.sh 到你的($HOME): ~/.acme.sh/目录下 ,后面所有的证书都会在这个目录生成.

2.Create alias for: acme.sh=~/.acme.sh/acme.sh. 注意:安装完成后你需要关闭再打开终端才可以让alias 生效。

3.增加了一个定时任务,用于SSL证书更新.

第一步:一键安装acme.sh

你可以使用Crontab -l来查看当前的定时任务:

acme.sh查看定时任务

同时你也可以使用SFTP来查看acme.sh 文件夹。

查看acme.sh文件夹

二、第二步:一键签发SSL证书

如果执行命令:acme.sh 提示如下错误:

-bash: acme.sh: command not found

你可以使用以下命令来修复:

命令:source ~/.bashrc或source ~/.bash_profile

2.1  单个域名SSL

注意:-w后面是域名的根目录,请根据实际修改.

acme.sh --issue -d wzfou.com -w /home/wwwroot/wzfou.com
#默认签发的是RSA,如果你想签发ECC证书,请使用以下命令
acme.sh --issue -d wzfou.com -w /home/wwwroot/wzfou.com --keylength ec-256

或者:

acme.sh --issue -d wzfou.com -w /home/wzfou/public_html
#默认签发的是RSA,如果你想签发ECC证书,请使用以下命令
acme.sh --issue -d wzfou.com -w /home/wzfou/public_html --keylength ec-256

或者:

acme.sh --issue -d wzfou.com -w /var/www/html
#默认签发的是RSA,如果你想签发ECC证书,请使用以下命令
acme.sh --issue -d wzfou.com -w /var/www/html  --keylength ec-256

2.2  多个域名SSL

acme.sh --issue -d wzfou.com -d www.wzfou.com -d cp.wzfou.com -w /home/wwwroot/wzfou.com
#默认签发的是RSA,如果你想签发ECC证书,请使用以下命令
acme.sh --issue -d wzfou.com -d www.wzfou.com -d cp.wzfou.com -w /home/wwwroot/wzfou.com --keylength ec-256

2.3  泛域名SSL

注意:泛域名SSL证书签发要用到DNS验证的方式,参考2.4部分。

acme.sh --issue --dns dns_dp -d wzfou.com -d *.wzfou.com
#默认签发的是RSA,如果你想签发ECC证书,请使用以下命令
acme.sh --issue --dns dns_dp -d wzfou.com -d *.wzfou.com --keylength ec-256
#可选长度有:
   ec-256 (prime256v1, “ECDSA P-256”)
   ec-384 (secp384r1, “ECDSA P-384”)

2.4  无法验证域名?

上面通过指定网站根目录的验证域名的方法有时会失效,也有朋友VPS上没有安装Web环境,无法使用80端口。

letsencrypt免费SSL证书无法验证

这里acme.sh 提供了一个添加DNS API自动验证域名的方式,首先到你的域名DNS处获得API,这里我以NS1 DNS域名解析来作为演示,首先到官网后台获得API(其它的DNS,如DNSPOD、Cloudxns、阿里云DNS等使用API的方法见本文第四部分)。

letsencrypt免费SSL证书获得API

然后是导入NS1.com API

export NS1_Key="fdmlfxxxxxxxfk"

现在就可以开始签发SSL证书了:

acme.sh --issue --dns dns_nsone -d wzfou.com -d www.wzfou.com
#或者签发ECC证书
acme.sh --issue --dns dns_nsone -d wzfou.com -d www.wzfou.com --keylength ec-256

letsencrypt免费SSL证书签发成功

使用DNS API验证域名的方式有一个好处就是不需要WEB访问就可以签发SSL证书,acme.sh 会在你的DNS域名解析处添加一个TXT记录,验证成功后会自动删除该TXT记录。

letsencrypt免费SSL证书添加TXT记录

三、第三步:安装letsencrypt SSL证书

这一步部分其实可以根据各自的实际来执行了,为了让acme.sh 可以自动更新续期SSL证书,建议使用以下操作方法来执行,这样acme.sh 每次更新完了SSL证书后都自动按照你第一次安装SSL证书的方法执行一遍同步SSL证书的命令。

Apache 服务器安装letsencrypt SSL证书如下::

acme.sh --install-cert -d wzfou.com \
--cert-file      /path/to/certfile/in/apache/cert.pem  \
--key-file       /path/to/keyfile/in/apache/key.pem  \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd     "service apache2 force-reload"

Nginx 服务器安装letsencrypt SSL证书e:

acme.sh --install-cert -d wzfou.com \
--key-file       /path/to/keyfile/in/nginx/key.pem  \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd     "service nginx force-reload"

实际操作中,大家根据需要调整好证书和密钥的路径,由于我使用的ECC证书,并且重启Apache2的命令无效,所以我用的以下命令:

acme.sh --install-cert -d ping.wzfou.com --ecc \
--cert-file     /etc/pki/tls/certs/ping.wzfou.com.cer  \
--key-file       /etc/pki/tls/certs/ping.wzfou.com.key  \
--fullchain-file /etc/pki/tls/certs/fullchain.cer \
--reloadcmd     "/etc/init.d/httpd force-reload"

letsencrypt免费SSL证书安装成功

执行命令后,我们就可以在路径中看到已经复制过来的证书还有Key等文件了,下次acme.sh 执行更新SSL证书时也会同样执行上述的命令。否则,需要自己手动复制粘贴证书。

letsencrypt免费SSL证书查看文件

最后记得到Apache或者Nginx的配置中调整好证书和Key的路径,重启Apache或者Nginx就算完成了。

letsencrypt免费SSL证书调整路径

如果你发现letsencrypt SSL证书不能定时更新,你也可以自己手动强制更新:

acme.sh --renew -d example.com --force

如果是ECC cert,使用以下命令:

acme.sh --renew -d example.com --force --ecc

目前wzfou.com用的就是letsencrypt SSL证书。

letsencrypt免费SSL证书使用

四、附录:各大DNS API获取与签发SSL

说明

  1. 附录参考自:https://github.com/Neilpang/acme.sh/blob/master/dnsapi/README.md
  2. DNS解析服务汇总:https://wzfou.com/mianfei-dns/
  3. 签发泛域名SSL时请把命令部分:-d www.example.com 改成 :-d *.example.com

4.1  CloudFlare DNS API

First you need to login to your CloudFlare account to get your API key.

export CF_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
export CF_Email="[email protected]"

Ok, let’s issue a cert now:

acme.sh --issue --dns dns_cf -d example.com -d www.example.com

The CF_Key and CF_Email will be saved in ~/.acme.sh/account.conf and will be reused when needed.

4.2  DNSPod DNS API

First you need to login to your DNSPod account to get your API Key and ID.

export DP_Id="1234"
export DP_Key="sADDsdasdgdsf"

Ok, let’s issue a cert now:

acme.sh --issue --dns dns_dp -d example.com -d www.example.com

The DP_Id and DP_Key will be saved in ~/.acme.sh/account.conf and will be reused when needed.

4.3  CloudXNS DNS API

First you need to login to your CloudXNS account to get your API Key and Secret.

export CX_Key="1234"
export CX_Secret="sADDsdasdgdsf"

Ok, let’s issue a cert now:

acme.sh --issue --dns dns_cx -d example.com -d www.example.com

The CX_Key and CX_Secret will be saved in ~/.acme.sh/account.conf and will be reused when needed.

4.4  阿里云Aliyun DNS API

First you need to login to your 阿里云 Aliyun account to get your API key. https://ak-console.aliyun.com/#/accesskey

export Ali_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
export Ali_Secret="jlsdflanljkljlfdsaklkjflsa"

Ok, let’s issue a cert now:

acme.sh --issue --dns dns_ali -d example.com -d www.example.com

The Ali_Key and Ali_Secret will be saved in ~/.acme.sh/account.conf and will be reused when needed.

4.5  GoDaddy DNS API

First you need to login to your GoDaddy account to get your API Key and Secret. https://developer.godaddy.com/keys/

Please create a Production key, instead of a Test key.

export GD_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
export GD_Secret="asdfsdafdsfdsfdsfdsfdsafd"

Ok, let’s issue a cert now:

acme.sh --issue --dns dns_gd -d example.com -d www.example.com

The GD_Key and GD_Secret will be saved in ~/.acme.sh/account.conf and will be reused when needed.

4.6  PowerDNS DNS API

First you need to login to your PowerDNS account to enable the API and set your API-Token in the configuration. https://doc.powerdns.com/md/httpapi/README/

export PDNS_Url="http://ns.example.com:8081"
export PDNS_ServerId="localhost"
export PDNS_Token="0123456789ABCDEF"
export PDNS_Ttl=60

Ok, let’s issue a cert now:

acme.sh --issue --dns dns_pdns -d example.com -d www.example.com

The PDNS_Url, PDNS_ServerId, PDNS_Token and PDNS_Ttl will be saved in ~/.acme.sh/account.confand will be reused when needed.

4.7  Amazon Route53 DNS API

方法见:https://github.com/Neilpang/acme.sh/wiki/How-to-use-Amazon-Route53-API

export  AWS_ACCESS_KEY_ID=XXXXXXXXXX
export  AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXX

To issue a cert:

acme.sh --issue --dns dns_aws -d example.com -d www.example.com

The AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY will be saved in ~/.acme.sh/account.conf and will be reused when needed.

4.8  Linode DNS API

First you need to login to your Linode account to get your API Key. https://manager.linode.com/profile/api

Then add an API key with label ACME and copy the new key.

export LINODE_API_KEY="..."

Due to the reload time of any changes in the DNS records, we have to use the dnssleep option to wait at least 15 minutes for the changes to take effect.

Ok, let’s issue a cert now:

acme.sh --issue --dns dns_linode --dnssleep 900 -d example.com -d www.example.com

The LINODE_API_KEY will be saved in ~/.acme.sh/account.conf and will be reused when needed.

4.9  DigitalOcean DNS API (native)

You need to obtain a read and write capable API key from your DigitalOcean account. See: https://www.digitalocean.com/help/api/

export DO_API_KEY="75310dc4ca779ac39a19f6355db573b49ce92ae126553ebd61ac3a3ae34834cc"

Ok, let’s issue a cert now:

acme.sh --issue --dns dns_dgon -d example.com -d www.example.com

4.10 Namesilo DNS API

You’ll need to generate an API key at https://www.namesilo.com/account_api.php Optionally you may restrict the access to an IP range there.

export Namesilo_Key="xxxxxxxxxxxxxxxxxxxxxxxx"

And now you can issue certs with:

acme.sh --issue --dns dns_namesilo --dnssleep 900 -d example.com -d www.example.com

4.11  使用自定义API

If your API is not supported yet, you can write your own DNS API.

Let’s assume you want to name it ‘myapi’:

  1. Create a bash script named ~/.acme.sh/dns_myapi.sh,
  2. In the script you must have a function named dns_myapi_add() which will be called by acme.sh to add the DNS records.
  3. Then you can use your API to issue cert like this:
acme.sh --issue --dns dns_myapi -d example.com -d www.example.com

五、总结

letsencrypt免费SSL证书申请与安装过程还是挺简单的,只需要一个VPS主机,不管有没有安装Web环境都可以签发SSL证书,个人觉得DNS验证方式是最方便的。

letsencrypt免费SSL证书使用小结

没有VPS主机的同学该如何使用letsencrypt免费SSL证书呢?方法一:手动在线申请,三个在线免费SSL证书申请地址; 方法二:直接用Cpanel、DirectAdmin面板自带的SSL申请。

文章出自:挖站否 https://wzfou.com/letsencrypt/,版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。


分享到:
Avatar for Qi
关于站长(Qi),2008年开始混迹于免费资源圈中,有幸结识了不少的草根站长。之后自己摸爬滚打潜心学习Web服务器、VPS、域名等,兴趣广泛,杂而不精,但愿将自己经验与心得分享出来与大家共勉。
已有 42 条评论
  1. 我怎么感觉我的acme脚本没有自动更新证书呢?上次就是手动强制更新了,这次感觉又快到期了,它还没更新,我有点方,什么命令是查看它有无正常工作,有无自动更新呢?

    2021年7月7日 19:37 回复
    • Avatar for Qi Qi

      应该是更新了,但是你的Nginx没有重新,导致证书没有生效。你可以查看你的证书文件,可以看到证书的时间确实会更新,我也经常遇到这样的情况。

      2021年7月8日 16:53 回复
  2. Avatar for Star Star

    https://ttfou.com/image/WWcc
    好惨,为啥我折腾的东西,不论多简单都会遇到意想不到的坑[泪]

    2021年2月21日 11:46 回复
    • Avatar for Qi Qi

      应该是VPS的IP的问题,该IP存在异常,所以被官方给封掉了。

      2021年2月22日 12:39 回复
  3. 哈哈 很好的ssl教程。认真学习哦!

    2020年9月4日 17:29 回复
  4. Avatar for 石头 石头

    你好,我用谷歌云搭建的tro##jan,因为SSL证书到期,现在不能用了。我按照您的步骤,到第三步,安装不成功,能不能指点一下?谢谢

    2020年5月17日 13:25来自QQ 回复
  5. Avatar for 鱼丸粗面 鱼丸粗面

    安装证书的时候提示 input file is output file …… 是怎么回事额
    搜索了一下,好像也看到有人遇到这个问题

    2020年4月5日 00:53 回复
    • Avatar for 鱼丸粗面 鱼丸粗面

      这个问题已解决。。是我自己没看好教程。
      现在这一步已经完成了,到了配置证书路径那一步了。你这里是apache的,nginx服务器的配置搜到了很多,都不一样,哈哈哈,看晕了

      2020年4月5日 01:58 回复
      • Avatar for Qi Qi

        嗯,nginx配置比较多。

        2020年4月5日 19:35 回复
  6. Avatar for shyper shyper

    @Qi 前两天的cpanel好像是旧版,有些不习惯,今天更新成了新版用着比较舒服了。有个问题请教,申请的自己的SSL证书显示未经过验证怎么解决?另外ftp也一直登陆不上,地址和密码应该都没错。端口是21吗?

    2020年3月16日 18:04 回复
    • Avatar for Qi Qi

      端口是:2001。你是用的自定义SSL证书吗?

      2020年3月17日 12:31 回复
      • Avatar for shyper shyper

        一开始是自定义,显示没经过验证,后来用服务器,还是显示未经过验证,又用了网站提供的免费&自动的Let’s Encrypt证书,也还是显示未经过验证。以前都是用的qyfou的DNS,今天改DNS走cloudflare,然后网站识别经过验证的 CloudFlare Inc ECC CA-2证书。DA里关闭了SSL,结果现在网页不显示了,而且连shyper.wzfou.me也打不开网页了,郁闷

        2020年3月17日 12:44 回复
      • Avatar for shyper shyper

        现在shyper.ml可以了,谢谢e

        2020年3月17日 12:48 回复
  7. Avatar for ixinshang ixinshang

    现在好多都是上ssl了。目前也在计划中。

    2019年8月11日 13:42 回复
  8. Avatar for Eldest_brother Eldest_brother

    大佬,我到第三步就不会了,首先pem已经保存下来了保存为key.pem和cert.pem。这两个是同一个证书。已经上传到根目录的home文件夹里。并用了宝塔面板,运行后提示:Unknown parameter : –reloadcmd“service。期待你的解答

    2018年10月30日 02:55 回复
    • Avatar for Qi Qi

      证书记得合并一下,还有一个根证书。你是想在宝塔上启用SSL吗?

      2018年10月30日 13:12 回复
  9. 訪問Qi的網站有問題,要嘗試2~3次才成功= =

    2018年10月21日 00:17 回复
    • Avatar for Qi Qi

      难道是Https握手又出了问题?

      2018年10月21日 13:37 回复
      • 我这儿正常,江苏电信。不过我自己的站出现过类似情况,需要刷新一下或者打不开后隔几秒重新访问,或者访问一下次级域名什么的才正常。我估计(自己的)可能是PHP-FPM的配置问题。不过我的站没什么访问,你这个站大,换一下配置试试。

        2018年10月21日 21:25 回复
        • Avatar for Qi Qi

          哦,有可能,我之前把PHP-FPM参数调小了,遇到内存耗尽的话,会自动断开重启PHP。

          2018年10月22日 18:09 回复
  10. Avatar for KK KK

    教程很详细。

    2018年10月20日 17:08 回复
  11. 两个事。
    一是LET用ACME申请安装的话,不用那么麻烦,执行那些命令,直接改写两个配置文件即可。
    一是安装目录即acme.sh目录下的account.conf,以CloudFlare的API方式为例,直接增加两行如下:
    SAVED_CF_Key=’API KEY’
    SAVED_CF_Email=’邮箱’

    另一个配置文件在acme.sh的域名目录下,比如站长这个就是wzfou.com这个目录下有一个比如wzfou.com.conf的文件,打开并修改或增加
    Le_RealKeyPath=’/path/to/wzfou.com.key’
    Le_RealFullChainPath=’/path/to/fullchain.cer’
    Le_ReloadCmd=’service nginx restart(或force reload)’

    另一个事,VestaCP又出大事了,自上次那个被植入木马事件并且事后也没解决的问题事件后,这次又一类似事件
    https://www.lowendtalk.com/discussion/152905/take-heed-vestacp#latest
    这种事连出两次,估计用户会越来越少,现在我已经完全自己搭LEMP用了,反正个人小网站把安装编译全写成一条一条命令放在一起执行,其实也挺快的。

    2018年10月19日 20:54 回复
    • Avatar for Qi Qi

      非常感谢,已经更新。[害羞]
      另外,VestaCP看来是不建议用了。

      2018年10月21日 13:50 回复
  12. Avatar for lzy20021010 lzy20021010

    OneinStack自带的Let ‘s Encrypt现在也可以用DNS API验证域名了。

    2018年10月19日 20:07 回复
    • Avatar for Qi Qi

      OneinStack好像好久没有更新了。

      2018年10月21日 13:51 回复
      • 更的。你别看他网站上那个更新日志一栏,那个是大版本主要更新,你看他首页和Github,首页上标记的PHP的版本如7.2系列的已经是7.2.11了。这个11号版本发布的时间并不长。那个站长估计主版没大的Bug也没什么新鲜功能就不动了,只是在储存上更换了新的组件版本。

        2018年10月21日 21:21 回复
        • Avatar for Qi Qi

          哦,那就好,还以为不更新呢。

          2018年10月22日 18:41 回复
  13. 点赞点赞,我现在还是在用腾讯云的证书[可爱]

    2018年10月19日 00:05 回复
    • Avatar for Qi Qi

      腾讯云重新签发的证书是可以正常使用的。

      2018年10月21日 13:51 回复
  14. Avatar for 量晨 量晨

    Https访问老是时断时续是怎么回事?

    2018年10月18日 17:33 回复
  15. 今年一月份TrustAsia签发的 在Chrome Canary下还是信任的
    https://myssl.com/chrome_symantec.html 下检测提示无需升级
    详情见
    https://www.trustasia.com/chrome-70-symantec-update-of-2018

    2018年10月15日 18:55 回复
    • 是的,在2017下半年就已经更换了根证书,因此新签发的是没有任何问题的。

      2018年10月17日 11:37 回复
    • Avatar for Qi Qi

      哦,谢谢。

      2018年10月18日 17:35 回复
  16. Avatar for we we

    我喜欢用一键包。[吃惊]

    2018年10月15日 11:321 回复
  17. dns api功能满分

    2018年10月14日 22:33来自移动端 回复
    • 不过不装服务器软件还要什么https(笑)

      2018年10月14日 22:38来自移动端 回复
  18. Symantec可惜了。我的服务器开始卖啦,文件发到你微信了,十分感谢[嘻嘻]

    2018年10月14日 21:31 回复
    • Avatar for Qi Qi

      谢谢,已经收到。

      2018年10月18日 17:37 回复
    • 你俩现在都在做服务器VPS生意吧?链接公开放出来也优惠一下网友呗。

      2018年10月21日 21:23 回复
      • wzfou50off,五折亏本卖了,老铁快来买呀[可怜]

        2018年10月21日 22:39 回复
      • Avatar for Qi Qi

        哈哈,友情帮忙推广。是居若搞的。[可爱]

        2018年10月22日 18:10 回复

Login

Welcome! Login in to your account

Remember me Lost your password?

Don't have account. Register

Lost Password

Register