创建 Let's Encrypt 的证书

Tags
webtls
Created
Sep 4, 2018 9:16 AM

https://imququ.com/post/letsencrypt-certificate.html

用openssl生成账户私钥、域名私钥。在利用这两个生成证书请求文件(可在本地生成在上传到服务器):

openssl genrsa 4096 > account.key

openssl genrsa 4096 > domain.key

openssl req -new -sha256 -key domain.key -subj "/" -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:xianqiao.wang,DNS:www.xianqiao.wang,DNS:*.xianqiao.wang")) > domain.csr

或交互式创建证书请求:

openssl req -new -sha256 -key domain.key -out domain.csr

// CN 必须是域名,Let's Encrypt规定(?)

目前 Let’s Encrypt 没有 EV 证书的计划。

验证服务器,配置一个 HTTP 服务,以 Nginx 为例 (更新证书还要用到,建议一直保留):

server {

server_name www.yoursite.comyoursite.com;

location ^~ /.well-known/acme-challenge/ {

alias /home/xxx/www/challenges/;

try_files $uri =404;

}

location / {

rewrite ^/(.*)$ https://yoursite.com/$1 permanent;

}

}

在服务器上(因为Let's Encrypt是通过acme-tiny在服务器上执行用证书请求文件和账户私钥生成证书)用acme-tiny生成证书。

acme-tiny 访问的是.well-known/acme-challenge/目录,命令如下:

wget https://raw.githubusercontent.com/diafygi/acme-tiny/master/acme_tiny.py

python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir ./ > ./signed.crt

最后合并Let's Encrypt的中间证书:

wget -O - https://letsencrypt.org/certs/lets-encrypt-x1-cross-signed.pem > intermediate.pem

cat signed.crt intermediate.pem > chained.pem

最后使用的就是域名私钥合并的证书

自动更新:

新建脚本renew_cert.sh

#!/bin/bash

cd /home/xxx/www/ssl/

python acme_tiny.py --account-key account.key --csr domain.csr --acme-dir /home/xxx/www/challenges/ > signed.crt || exit

wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem

cat signed.crt intermediate.pem > chained.pem

service nginx reload

crontab 中使用绝对路径比较保险,crontab -e 加入以下内容:

0 0 1 * * /home/xxx/shell/renew_cert.sh >/dev/null 2>&1

SuperMade with Super