mkdir -p /acme-tiny/account
cd /acme-tiny/account
openssl genrsa 4096 > lets.encrypt.account.key
mkdir -p /acme-tiny/domain
cd /acme-tiny/domain
openssl genrsa 4096 > lee23.top.key
单个域名申请,以open.service.lee23.top
为例
cd /acme-tiny/domain
openssl req -new -sha256 -key lee23.top.key -subj "/CN=open.service.lee23.top" > open.service.lee23.top.csr
多个域名同时申请,以open.service.lee23.top
,open.source.lee23.top
,blog.lee23.top
为例
cd /acme-tiny/domain
openssl req -new -sha256 -key lee23.top.key -subj "/" -reqexts SAN -config <(cat /etc/pki/tls/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:open.service.lee23.top,DNS:open.source.lee23.top,DNS:blog.lee23.top")) > lee23.top.csr
备注:
/etc/pki/tls/openssl.cnf
是openssl
的配置文件位置,不同系统可能位置不同,可以通过命令 openssl version -a
查看,其中OPENSSLDIR
就是openssl.cnf
的位置目录
我们用csr通过acme-tiny的脚本申请证书时,先在服务器上生成一个随机验证文件,Let’s Encrypt通过创建 CSR 时指定的域名访问该文件,如果可以访问则表明你对这个域名有控制权
创建验证目录
mkdir -p /www/challenge
Nginx 服务配置,让 Let’s Encrypt 能访问验证文件,创建challenge.conf
location /.well-known/acme-challenge/ {
alias /www/challenge;
try_files $uri =404;
}
Nginx 引入challenge.conf
server {
listen 80;
server_name open.service.lee23.top;
include challenge.conf
......
}
server {
listen 80;
server_name open.source.lee23.top;
include challenge.conf
......
}
我们用acme-tiny获取签名证书
cd /acme-tiny
git clone https://github.com/diafygi/acme-tiny.git
mv /acme-tiny/acme-tiny /acme-tiny/acme-script
用python的docker镜像运行脚本,不用再单独安装python,但需要安装docker
docker run -it --rm --name acme-tiny-script -v /acme-tiny:/acme-tiny -w /acme-tiny python:2 python /acme-tiny/acme-script/acme_tiny.py --account-key /acme-tiny/account/lets.encrypt.account.key --csr /acme-tiny/domain/lee23.top.csr --acme-dir /www/challenge/ > /acme-tiny/domain/lee23.top.signed.crt
备注:
-v
docker目录映射
-w
docker工作目录
--account-key
Let’s Encrypt账户私钥 lets.encrypt.account.key
--csr
创建域名证书申请的请求文件 lee23.top.csr
--acme-dir
Let’s Encrypt验证域名所有权目录
lee23.top.signed.crt
生成的证书文件,该文件里有打印脚本执行过程,可打开查看debug,若没报错,则证明一切顺利,debug记录无需删除
crt 转化成 pem 文件,得到SSL证书的最终文件lee23.top.chained.pem
yum install -y ca-certificates
cd /acme-tiny/domain
wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem
cat lee23.top.signed.crt intermediate.pem > lee23.top.chained.pem
创建SSL配置lee23.top.ssl.conf
ssl_certificate /acme-tiny/domain/lee23.top.chained.pem;
ssl_certificate_key /acme-tiny/domain/lee23.top.key;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security max-age=15768000;
备注:
ssl_certificate
SSL证书pem文件位置
ssl_certificate_key
域名私钥位置
新增Nginx配置,端口为443,引入SSL配置
server {
listen 443 ssl;
server_name open.service.lee23.top;
......
include lee23.top.ssl.conf;
......
}
server {
listen 443 ssl;
server_name open.source.lee23.top;
......
include lee23.top.ssl.conf;
......
}
原来的80端口的Nginx配置不能删除,并配置强制跳转443端口,即强制跳转https (可选)
server {
listen 80;
server_name open.service.lee23.top;
......
return 301 https://$server_name$request_uri;
.....
}
备注:
强制跳转443端口的代码段须配置在challenge.conf
下面,避免Let’s Encrypt 验证域名所有权失败
把上面的主要流程写进脚本 refresh.lee23.top.ssl.sh
#!/bin/bash
docker run --rm --name acme-tiny-script -v /acme-tiny:/acme-tiny -w /acme-tiny python:2 python /acme-tiny/acme-script/acme_tiny.py --account-key /acme-tiny/account/lets.encrypt.account.key --csr /acme-tiny/domain/lee23.top.csr --acme-dir /www/challenge/ > /acme-tiny/domain/lee23.top.signed.crt || exit
wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > /acme-tiny/domain/intermediate.pem
cat /acme-tiny/domain/lee23.top.signed.crt /acme-tiny/domain/intermediate.pem > /acme-tiny/domain/lee23.top.chained.pem
/usr/local/nginx/sbin/nginx -s reload
备注
/usr/local/nginx/sbin/nginx -s reload
Nginx配置重载
配置crontab
定时执行脚本自动更新证书,证书有效期3个月,我们每个月28日更新一次
0 0 28 * * /acme-tiny/refresh-ssl/refresh.lee23.top.ssl.sh 2>> /acme-tiny/refresh-ssl/refresh.lee23.top.error.log