惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
月光博客
月光博客
Recent Announcements
Recent Announcements
S
Secure Thoughts
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Troy Hunt's Blog
量子位
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
云风的 BLOG
云风的 BLOG
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
WordPress大学
WordPress大学
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
P
Privacy & Cybersecurity Law Blog
aimingoo的专栏
aimingoo的专栏
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V2EX - 技术
V2EX - 技术
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
Darknet – Hacking Tools, Hacker News & Cyber Security
U
Unit 42
Schneier on Security
Schneier on Security
大猫的无限游戏
大猫的无限游戏
I
Intezer
Hacker News: Ask HN
Hacker News: Ask HN
H
Heimdal Security Blog
Cisco Talos Blog
Cisco Talos Blog
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Help Net Security
Latest news
Latest news
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LangChain Blog
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
小众软件
小众软件
M
MIT News - Artificial intelligence
A
About on SuperTechFans
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
NISL@THU
NISL@THU
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
T
Tenable Blog

宋浩志的博客

Windows安装nacos Edge用户登录一直转圈 SpringBoot整合Socket 使用coding持续集成SpringBoot项目 CentOS安装Nginx 使用Windows内置虚拟机Hyper-v安装CentOS ruoyi-vue项目集成flyway实现自动创建表 Docker常用命令记录 vue学习笔记四-列表渲染 vue学习笔记三条件渲染 vue学习笔记二模板语法 vue学习笔记一创建项目
使用acme.sh来申请SSL证书
宋浩志 · 2022-09-23 · via 宋浩志的博客

简介

acme.sh 实现了 acme 协议, 可以从 letsencrypt 生成免费的证书.

官方文档:https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E

本篇文章以Nginx为例子,来实现获取SSL证书,以及配置Nginx

安装

安装很简单, 一个命令:

1
curl https://get.acme.sh | sh -s email=my@example.com

my@example.com是你的邮箱地址

创建 一个 shell 的 alias, 例如 .bashrc,方便你的使用:

1
alias acme.sh=~/.acme.sh/acme.sh

普通用户和 root 用户都可以安装使用. 安装过程进行了以下几步:

  1. 把 acme.sh 安装到你的 home 目录下:
1
~/.acme.sh/
  1. 自动为你创建 cronjob, 每天 0:00 点自动检测所有的证书, 如果快过期了, 需要更新, 则会自动更新证书.

更高级的安装选项请参考: https://github.com/Neilpang/acme.sh/wiki/How-to-install

配置Nginx

先把Nginx配置文件写好,让这个域名可以通过http访问

1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 80;
server_name 你的域名;
location / {
#转发的ip和端口
proxy_pass http://127.0.0.1:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

生成证书

关于生成证书,官方的例子有apache、nginx、手动 dns 的方式,我这里只演示Nginx的方式

官方文档上这样写的

https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E#1-http-%E6%96%B9%E5%BC%8F%E9%9C%80%E8%A6%81%E5%9C%A8%E4%BD%A0%E7%9A%84%E7%BD%91%E7%AB%99%E6%A0%B9%E7%9B%AE%E5%BD%95%E4%B8%8B%E6%94%BE%E7%BD%AE%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6-%E6%9D%A5%E9%AA%8C%E8%AF%81%E4%BD%A0%E7%9A%84%E5%9F%9F%E5%90%8D%E6%89%80%E6%9C%89%E6%9D%83%E5%AE%8C%E6%88%90%E9%AA%8C%E8%AF%81-%E7%84%B6%E5%90%8E%E5%B0%B1%E5%8F%AF%E4%BB%A5%E7%94%9F%E6%88%90%E8%AF%81%E4%B9%A6%E4%BA%86

如果你用的 nginx服务器, 或者反代, acme.sh 还可以智能的从 nginx的配置中自动完成验证, 你不需要指定网站根目录:

执行以下命令:

1
acme.sh --issue -d 你的域名.com --nginx

生成好的证书会存放在~/.acme.sh/example.com

copy/安装证书

前面证书生成以后, 接下来需要把证书 copy 到真正需要用它的地方.

注意, 默认生成的证书都放在安装目录下: ~/.acme.sh/, 请不要直接使用此目录下的文件, 例如: 不要直接让 nginx/apache 的配置文件使用这下面的文件. 这里面的文件都是内部使用, 而且目录结构可能会变化.

正确的使用方法是使用 –install-cert 命令,并指定目标位置, 然后证书文件会被copy到相应的位置, 例如:

1
2
3
4
5
acme.sh --force --install-cert 
-d example.com
--fullchain-file /etc/nginx/ssl/example.com.crt
--key-file /etc/nginx/ssl/example.com.key
--reloadcmd "service nginx force-reload" > /dev/null

–install-cert: 这个参数代表cpoy
-d: 你的域名
–key-file: copy你的私钥文件,需要和nginx指定的一致
–fullchain-file: copy你的证书链文件(此文件包含自己和中间ca机构的证书),需要和nginx指定的一致
–reloadcmd: 指定重新加载证书的命令

(一个小提醒, 这里用的是 service nginx force-reload, 不是 service nginx reload, 据测试, reload 并不会重新加载证书, 所以用的 force-reload)

Nginx 的配置 ssl_certificate 使用 /etc/nginx/ssl/fullchain.cer ,而非 /etc/nginx/ssl/.cer ,否则 SSL Labs 的测试会报 Chain issues Incomplete 错误。

–install-cert命令可以携带很多参数, 来指定目标文件. 并且可以指定 reloadcmd, 当证书更新以后, reloadcmd会被自动调用,让服务器生效.

详细参数请参考: https://github.com/Neilpang/acme.sh#3-install-the-issued-cert-to-apachenginx-etc

值得注意的是, 这里指定的所有参数都会被自动记录下来, 并在将来证书自动更新以后, 被再次自动调用.

修改Nginx配置文件

执行上面的命令以后,会发现在/etc/nginx/ssl目录下会多出两个文件,我们需要把这两个文件在Nginx配置文件中配置好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 443 ssl;
server_name example.com;
ssl on;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

Nginx配置好后重启Nginx,访问你的域名。

更新证书

目前证书在 60 天以后会自动更新, 你无需任何操作. 今后有可能会缩短这个时间, 不过都是自动的, 你不用关心.

请确保 cronjob 正确安装, 看起来是类似这样的:

1
2
3
crontab  -l

56 * * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

其他常用命令

acme.sh --info -d example.com :查看已安装证书信息

acme.sh --list: 列出所有证书

acme.sh --renew -d soulchild.site -d *.soulchild.site --force: 手动强制更新证书

acme.sh --renew-all: 手动更新所有证书

acme.sh --revoke: 撤销证书

acme.sh --remove -d soulchild.site: 删除证书

acme.sh --cron: 通过cronjob更新所有证书。

acme.sh --upgrade: 升级acme.sh

acme.sh --uninstall: 卸载acme.sh

acme.sh --upgrade:升级到最新版

acme.sh --upgrade --auto-upgrade:开启自动更新

acme.sh --upgrade --auto-upgrade 0:关闭自动更新

参考

https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E

https://github.com/oneinstack/oneinstack/blob/master/vhost.sh#L265