20

给阿里云的个人博客添加免费的HTTPS证书

 3 years ago
source link: https://www.pengrl.com/p/20060/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

给阿里云的个人博客添加免费的HTTPS证书

我的博客的证书过期了,使用chrome浏览器没法正常访问,显示不安全,具体相关信息如下:

1
2
3
Encryption Everywhere DV TLS CA - G1
2020年6月4日 星期四 中国标准时间 下午8:00:00
www.digicert.com

我的机器和域名用的都是阿里云。去年申请的时候,送了一年免费的HTTPS证书,现在到期了。

上阿里云看了看证书,都贼贵,掏钱是不可能掏钱的。

网上找个免费的,以下是操作记录。

登录阿里云主机,先安装acme.sh

1
2
3
4
$git clone https://github.com/acmesh-official/acme.sh.git
$cd acme.sh
$INSTALLONLINE=1
$sh acme.sh --install

输出信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
[Sat Jun  6 09:40:07 CST 2020] It is recommended to install socat first.
[Sat Jun 6 09:40:07 CST 2020] We use socat for standalone server if you use standalone mode.
[Sat Jun 6 09:40:07 CST 2020] If you don't use standalone mode, just ignore this warning.
[Sat Jun 6 09:40:07 CST 2020] Installing to /root/.acme.sh
[Sat Jun 6 09:40:07 CST 2020] Installed to /root/.acme.sh/acme.sh
[Sat Jun 6 09:40:07 CST 2020] Installing alias to '/root/.bashrc'
[Sat Jun 6 09:40:07 CST 2020] OK, Close and reopen your terminal to start using acme.sh
[Sat Jun 6 09:40:07 CST 2020] Installing alias to '/root/.cshrc'
[Sat Jun 6 09:40:07 CST 2020] Installing alias to '/root/.tcshrc'
[Sat Jun 6 09:40:07 CST 2020] Installing cron job
[Sat Jun 6 09:40:08 CST 2020] Good, bash is found, so change the shebang to use bash as preferred.
[Sat Jun 6 09:40:08 CST 2020] OK

上阿里云的控制台获取阿里云API的AccessKey ID和AccessKey Secret。如果没有就创建一个。

地址: https://ak-console.aliyun.com/#/accesskey

弄好后需要关闭终端,重新登录一下阿里云主机,执行如下命令(填入阿里云控制台获取的Ali_KeyAli_Secretpengrl.com是我的域名):

1
2
3
$export Ali_Key="xxx"
$export Ali_Secret="xxx"
$acme.sh --issue --dns dns_ali -d pengrl.com -d *.pengrl.com

成功后,会在/root/.acme.sh/pengrl.com下生成证书相关的文件。

我的博客使用的是nginx接入,直接修改nginx配置文件中和证书相关的配置:

1
2
ssl_certificate "/root/.acme.sh/pengrl.com/fullchain.cer";
ssl_certificate_key "/root/.acme.sh/pengrl.com/pengrl.com.key";

使用如下命令重启nginx:

1
$service nginx restart

重新在浏览器中访问博客,多打开几个链接看看,再看看图片啥的是否正常。一切OK。

再在chrome中点击那把小锁,看看证书的基础信息:

1
2
签发者:Let's Encrypt Authority X3
过期时间:2020年9月4日 星期五 中国标准时间 上午8:50:17

基本上就完成了。

注意,这里写的过期日期是大概3个月后,我看acme.sh在crontab中添加了一个以周为单位的定时任务,应该是证书续签用的,我们过段时间再看看过期时间是否会更新。

另外,还可以用这个网站,测试一下。 https://myssl.com/

20200905补充:

blog在0904证书到期了,并没有自动更新。
使用$crontab -e查看所有定时任务,找到7 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
命令行执行该命令$"/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh"
得到结果:

1
2
3
4
5
6
===Starting cron===
[Sat Sep 5 12:16:00 CST 2020] Renew: 'pengrl.com'
[Sat Sep 5 12:16:00 CST 2020] Skip, Next renewal time is: Sat Oct 3 16:08:23 UTC 2020
[Sat Sep 5 12:16:00 CST 2020] Add '--force' to force to renew.
[Sat Sep 5 12:16:00 CST 2020] Skipped pengrl.com
[Sat Sep 5 12:16:00 CST 2020] ===End cron===

使用添加参数--force,再次执行/root/.acme.sh/acme.sh --force --cron --home "/root/.acme.sh"
成功生成新的证书。
重启nginx,blog访问恢复正常。

之后我更新了crontab任务,时间间隔改为30天,并添加force参数。

本文完,作者yoko,尊重劳动人民成果,转载请注明原文出处: https://pengrl.com/p/20060/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK