[!前言] 让你的服务域名申请一个ssl证书是很有必要的,github、谷歌等比较出名的网站打开F12其实可以看到密码是明文传输的,为什么他们会这么做呢?其实ssl就是一个保障,实现数据信息在客户端和服务器之间的加密传输,可以防止数据信息的泄露,保证了双方传递信息的安全性。域名服务商大多只提供一年的免费证书,甚至有些已经缩短到3个月,每次手动申请并配置肯定是很繁琐的,那么如何让你的服务域名有一个免费的证书,并且能够在快过期的时候自动申请呢?
注意
国内的域名服务商需要备案,国外域名服务商解析到国内服务器也需要备案!否则无法正常解析。
工具开源地址
脚本的下载安装
下载安装脚本
如果下载失败可以多执行几次
curl https://get.acme.sh | sh
可能存在的错误,没有的话跳过
提示"curl: command not found"
CentOS/RHEL/Aliyun OS等衍生系统使用:yum install -y curl,Debian/Ubuntu及衍生系统使用:apt update && apt install -y curl 安装curl,然后再次运行上面的acme.sh安装命令。
建议切换Let’s Encrypt的CA
可以指定切换一下CA,有些是需要注册的
# 这个不需要注册但是一小时限制五次
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
确定定时任务是否正确添加
定时任务保证了证书在到期前能自动续期。输入指令可以看到类似如下输出,定时任务头部两个数字肯定都不一样代表的是秒和分,但是都是固定一小时运行一次。
开启acme.sh的自动升级
由于ACME协议和Let’s Encrypt CA都在频繁的更新,因此建议开启acme.sh的自动升级
~/.acme.sh/acme.sh --upgrade --auto-upgrade
开始签发证书
注意:非DNS验证方式签发证书的前提都是域名的IP需要指向当前服务器
根据web服务器的不同执行不同命令
服务器上已经运行了web软件,则指定webroot签发证书
~/.acme.sh/acme.sh --issue -d 域名 --webroot web目录
网站已经运行Nginx/Apache,指定对应插件就可以了
# 如果是apache,换成 --apache
~/.acme.sh/acme.sh --issue -d 域名 --nginx
没有运行web软件并且80端口空闲,可以使用acme.sh自己监听80端口进行验证
~/.acme.sh/acme.sh --issue -d 域名 --standalone
使用DNS方式,手动添加DNS记录进行验证
~/.acme.sh/acme.sh --issue --dns -d 域名
# 命令结束后,acme.sh会显示解析记录,需要到DNS后台设置解析
# 设置好解析后,生成证书
~/.acme.sh/acme.sh --renew -d 域名
使用方式,acme.sh内置了许多DNS服务商的插件,使用方法参考: github.com/acmesh-offi…
安装证书
其中--reloadcmd是可选的,用来安装好证书后重启web服务器。
~/.acme.sh/acme.sh --install-cert -d 域名 \
--key-file 密钥存放目录 \
--fullchain-file 证书存放路径 \
--reloadcmd "service nginx force-reload"
签发示例
当前nginx内配置参考
使用当前方式时,nginx配置不能出错,建议先执行以下reload命令查看是否存在问题。
user root;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#include ../test/*.conf;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 600m;
upstream waline { #可以指定多个项目
server 127.0.0.1:8360 weight=1;
}
server {
listen 80;
server_name waline.allbs.xyz; #修改为申请证书绑定的域名
root html;
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
index index.html index.htm;
ssl_session_timeout 10m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
add_header Strict-Transport-Security "max-age=31536000";
location / {
proxy_pass http://waline;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; # 转发时使用https协议
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
expires 12h;
}
}
}
注意事项
如果套了cloudflare记得先把代理关掉
cloudflare的默认解析式ipv6,而且还没办法在网页中调整,只能使用api设置。所以cloudflare中的域名会有点麻烦
关闭cloudflare的默认ipv6
获取自己的api token
调用接口改变ipv6的设置
域名解析现在默认到了ipv4
签发免费的Let’s Encrypt,服务使用的是nginx,域名用的是我网站的子域名waline.allbs.cn
==注意使用的CA服务器,上面设置的letsencrypt签发证书频率是有限制的,每小时五次。==
# 可以在最后指定以下nginx配置文件的位置,如果运行不成功可以指定一下,多域名直接多个-d 域名即可
~/.acme.sh/acme.sh --issue -d 你的域名 --nginx
执行了更新命令和签发命令,可以看到证书已经签发成功
安装证书
~/.acme.sh/acme.sh --install-cert -d waline.allbs.cn \
--key-file /home/nginx/ssl/waline.allbs.cn.key \
--fullchain-file /home/nginx/ssl/waline.allbs.cn_bundle.pem \
--reloadcmd "systemctl force-reload nginx.service"
使用腾讯的dns api的方式申请证书,国内域名商基本都覆盖,下面以腾讯的dnspod.cn示例
使用手册地址
创建密钥:
执行指令
export DP_Id="你的密钥id"
export DP_Key="你的密钥"
~/.acme.sh/acme.sh --issue --dns dns_dp -d 你需要签发的子域名
nginx配置中配置好ssl示例
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 600m;
upstream waline { #可以指定多个项目
server 127.0.0.1:8360 weight=1;
}
server {
listen 80;
listen 443 ssl;
server_name waline.allbs.cn; #修改为申请证书绑定的域名
root html;
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
index index.html index.htm;
ssl_session_timeout 10m;
ssl_certificate /home/nginx/ssl/waline.allbs.cn_bundle.pem; # 这个是证书的crt文件所在目录
ssl_certificate_key /home/nginx/ssl/waline.allbs.cn.key; # 这个是证书key文件所在目录
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
add_header Strict-Transport-Security "max-age=31536000";
location / {
proxy_pass http://waline;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; # 转发时使用https协议
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
expires 12h;
}
}
}
安装并强制nginx重载配置
注意不同系统nginx重载配置指令不一样,根据实际情况填。
~/.acme.sh/acme.sh --install-cert -d waline.allbs.cn \
--key-file /home/nginx/ssl/waline.allbs.cn.key \
--fullchain-file /home/nginx/ssl/waline.allbs.cn_bundle.pem \
--reloadcmd "systemctl reload nginx.service"