Acme申请免费SSL证书——nginx法
要通过https访问我们部署的网站时,就要用到SSL证书,利用acme脚本可以很方便快捷地申请到免费的SSL证书。
准备工作:具有公网IP的服务器一台;域名一个(已解析好的)
一、安装acme
因为acme更改了默认的CA为ZeroSSL,需要邮件注册,将下列my@example.com改为自己的邮件地址
curl https://get.acme.sh | sh -s email=my@example.com
断开终端重新连接服务器
二、更改nginx配置
备份 /etc/nginx/ 目录下的nginx.conf为nginx.conf1,新建一个nginx.conf,输入以下内容,并将example.com更改为你自己的域名,保存
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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 120;
client_max_body_size 20m;
#gzip on;
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
}
重启nginx,并在浏览器中输入你的域名,查看是否正常加载”Welcome to nginx”页面
systemctl restart nginx
三、申请证书
acme.sh --issue -d example.com --nginx
这一步执行时间可能比较长,耐心等待
四、安装证书
要特别注意上一步申请完后,不要使用~/.acme.sh/下的证书,需要安装证书到指定的位置,安装路径 /usr/local/etc/nginx/ssl/ 可以根据自己情况修改
acme.sh --install-cert -d example.com \
--key-file /usr/local/etc/nginx/ssl/key.pem \
--fullchain-file /usr/local/etc/nginx/ssl/cert.pem \
--reloadcmd "service nginx force-reload"
现在就可以享用你的SSL证书了!
五、自动更新(可选)
acme每60天会自动续签证书。可以设置自动更新以获取最新的脚本:
acme.sh --upgrade --auto-upgrade
如果想要取消:
acme.sh --upgrade --auto-upgrade 0