Certbot🔗Nginx自动化
Certbot自动获签SSL证书,Crontab实现自动化
自动化脚本(以 Aliyun 为例)
其他域名服务商平台的脚本直接上
Github
上找就可以,关键词就找github
+域名商
获取脚本
此处以阿里云为例:
git clone https://github.com/wangy8961/certbot-dns-aliyun.git
cp certbot-dns-aliyun/certbot-dns-aliyun /etc/letsencrypt/
获取accessKey
在阿里云右上角头像菜单栏里的密钥管理,获取accessKey
并在服务器的/etc/letsencrypt/
目录下创建config.json
文件
{
"accessKeyID": "你的AccessKeyID",
"accessKeySecret": "你的AccessKeySecret"
}
Certbot 安装
Ubuntu
apt install certbot python-certbot-nginx
CentOS
先安装EPEL
库,Certbot
在这个源里
sudo yum install epel-release
安装Certbot
sudo yum install certbot
使用Certbot申请证书
注意以下命令需要修改email
以及域名
域名支持支持通配符*
,当然如果想自定义某个二级域名也是可以的
certbot certonly \
--non-interactive \
--email xxx@xx.com \
--agree-tos \
--manual-public-ip-logging-ok \
--manual --preferred-challenges dns-01 \
--manual-auth-hook "/etc/letsencrypt/certbot-dns-aliyun -o authenticator" \
--manual-cleanup-hook "/etc/letsencrypt/certbot-dns-aliyun -o cleanup" \
-d *.example.com -d example.com \
--server https://acme-v02.api.letsencrypt.org/directory
命令参数介绍
non-interactive:非交互式运行,即运行过程中不需要询问用户输入,但需要额外的命令行参数,当客户端发现参数缺失时会给出相应的说明
–email:用于注册和恢复联系的电子邮件
–agree-tos:同意ACME服务器的订阅协议
–manual-public-ip-logging-ok:自动允许公共IP记录
–manual:以交互方式获取证书,或使用shell脚本钩子获取证书
–perferred-challenges:以逗号分隔的排序列表,列出在授权过程中使用的首选
challenges
–manual-auth-hook:验证脚本命令
–manual-cleanup-hook:清理脚本命令
-d:域名列表,使用逗号隔开
–server:服务器,这里填写的是
ACME
输入命令后会给出
DNS
的TXT
记录值,需要前往域名服务商添加域名解析,以校验域名的所有权
自动续期
利用crontab
工具在Linux
环境下可设定定时任务
安装 crontab
Ubuntu
sudo apt-get install cron
CentOS
sudo yum install vixie-cron crontabs
添加定时任务
crontab -e
# 添加
/usr/bin/certbot renew --manual --preferred-challenges dns-01 --manual-auth-hook "/etc/letsencrypt/certbot-dns-aliyun -o authenticator" --manual-cleanup-hook "/etc/letsencrypt/certbot-dns-aliyun -o cleanup" --deploy-hook "docker restart nginx"
Nginx
Docker 安装 Nginx
使用Docker
一键部署Nginx
docker run --detach \
--restart always \
--name nginx \
-p 443:443 \
-p 80:80 \
-v /你的外部映射/nginx/www:/usr/share/nginx/html:rw \
-v /你的外部映射/nginx/conf.d/:/etc/nginx/conf.d/:rw \
-v /你的外部映射/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:rw \
-v /你的外部映射/nginx/logs:/var/log/nginx/:rw \
-v /etc/letsencrypt/:/etc/nginx/ssl/:rw \
-d nginx
注意Volume
要映射好,下面需要在外部修改Nginx
配置
修改Nginx配置
需要修改的文件是/你的外部映射/nginx/conf/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# 强制http跳转https
server {
listen 80;
server_name *.example.com;
return 301 https://$http_host$request_uri;
}
server {
listen 443 ssl; # https对应端口,
ssl_certificate /etc/nginx/ssl/live/example.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/example.com/privkey.pem;
server_name *.example.com; # ip,域名,我这里以泛域名举例,毕竟是做反向代理,http就不用配置了
# 此处是端口映射,若无需求删掉即可
location / {
proxy_pass http://172.17.0.1:8080; # 映射的frp服务端frps.ini的 vhost_http_port端口
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_redirect off;
proxy_read_timeout 240s;
}
# 无端口映射,则删除是上面这中间的部分
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
OK,兄弟们,今天的大话就讲到这里,我们下期再见嘞