安装 CertBot 证书工具
阅读 Certbot 官网,有不同系统环境的安装教程,这里使用 Ubuntu + Nginx
# 安装 Certbot
apt-get install certbot
申请 Let’s Encrypt 证书
首先,确定申请证书的域名,例如:example.com
,并且保证域名可以正常访问
Certbot 工具在申请证书时,会先在网站 /.well-known/acme-challenge
目录下生成一个文件,并通过域名访问它
通过终端命令,申请证书
# /path/to/example.com/webroot 表示域名example.com的web服务器根目录路径
certbot certonly --webroot -w /path/to/example.com/webroot -d example.com
# 会输出证书保存的位置
申请的证书保存在 /etc/letsencrypt/live/example.com
文件夹下
证书文件:fullchain.pem
,密钥文件:privkey.pem
,
接下去,生成 DH 参数文件;通过 OpenSSL 命令行工具进行生成
openssl dhparam -out /path/to/dhparam.pem 2048
配置Nginx
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
# HTTP 重定向到 HTTPS
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
# 证书和密钥文件
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m; # about 40000 sessions
ssl_session_tickets off;
# DH 密钥参数文件
ssl_dhparam /path/to/dhparam.pem;
# intermediate configuration
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
# HSTS (ngx_http_headers_module is required) (15768000 seconds)
# 向 HTTP 响应头添加 Strict-Transport-Security (STS) 头信息
# Strict-Transport-Security 是一个安全策略机制,它允许网站声明在一段时间内(这里是 15768000 秒),只能通过 HTTPS 安全连接访问
add_header Strict-Transport-Security "max-age=15768000" always;
# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
# verify chain of trust of OCSP response using Root CA and Intermediate certs
# 使用 Root CA 和中间证书验证 OCSP 响应的信任链,指定信任的证书链文件
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
# replace with the IP address of your resolver
resolver 127.0.0.1;
# ......
}
参考链接: