Acme.sh创建https证书

65

说明

  1. 本文档的宗旨是为了给个人公网域名配置免费的HTTPS证书提供参考教程

  2. 需要先有一个公网的域名

证书制作

公网域名解析

配置个人公网域名的解析,这个步骤一般是在域名提供商那里配置。配置好之后就可以到解析后的服务器上制作证书了。制作好的证书建议配合Nginx代理一起使用

安装Acme.sh

安装方式

  • 通过curl安装

curl https://get.acme.sh | sh

  • 通过wget安装

wget -O - https://get.acme.sh | sh

配置并验证

  • 运行以下命令使别名生效

source ~/.bashrc

  • 确认是否安装成功

acme.sh --version

Yum安装Nginx

其他方式部署Nginx也是可以的,本参考教程使用的方式Yum直接装

  1. 配置yum源

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
  1. 更新yum

yum update -y

  1. 安装nginx

yum install nginx -y

  1. 自启动nginx

systemctl start nginx && systemctl enable nginx

  1. 查看nginx状态

systemctl status nginx

nginx配置文件目录:/etc/nginx/conf.d/

安装Socat

yum install socat -y

注册账户信息

acme.sh --register-account -m YOUR_EMAIL --server zerossl

输出如下

[root@VM-4-10-centos .acme.sh]# acme.sh --register-account -m YOUR_EMAIL --server zerossl
[Tue Dec 24 17:40:16 CST 2024] Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: 52
[Tue Dec 24 17:40:16 CST 2024] Cannot init API for: https://acme.zerossl.com/v2/xx.
[Tue Dec 24 17:40:16 CST 2024] Sleeping for 10 seconds and retrying.
[Tue Dec 24 17:40:34 CST 2024] Registering account: https://acme.zerossl.com/v2/xx
[Tue Dec 24 17:40:46 CST 2024] Already registered
[Tue Dec 24 17:40:47 CST 2024] ACCOUNT_THUMBPRINT='xxxx'

生成HTTPS证书

  1. 先关闭nginx,不然会报错80端口占用

systemctl stop nginx

  1. 生成自己公网域名的证书

acme.sh --issue --standalone -d stupidheart.cn -d www.stupidheart.cn

  1. 日志中出现了.cer.key证书已经证书链的存放路径

故障排错

假如上一步生成证书的过程有如下报错,并且一直retry一直报错

Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: 7

解决方式:

  1. 更新acme:acme.sh --upgrade

  2. 更新证书请求地址:acme.sh --set-default-ca --server letsencrypt

安装证书

下面命令中的key和cer路径都是生成证书过程中输出的,自行更改一下

acme.sh --install-cert -d stupidheart.cn --key-file path_to/stupidheart.cn.key --fullchain-file path_to/fullchain.cer --reloadcmd "service nginx force-reload"

自动续签

会有定时任务自动执行证书的生成和安装代替手动工作

acme.sh --install-cronjob

Nginx配置证书

本次生成的证书结合Nginx代理使用,并通过配置实现80自动转发443,443转发到后端服务

  1. 修改nginx配置文件:/etc/nginx/conf.d/xxx.conf

server {
    listen 80;
    server_name stupidheart.cn www.stupidheart.cn;
    location / {
        rewrite ^ https://$host$request_uri? permanent;
    }
}

server {
    listen 443 ssl;
    server_name stupidheart.cn www.stupidheart.cn;

    ssl_certificate path_to/fullchain.cer;
    ssl_certificate_key path_to/stupidheart.cn.key;

    location / {
        proxy_pass http://xxx:1234;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
  1. 重启nginx使配置生效,重启前建议先nginx -t检查一下配置文件是否有误

  2. 访问公网域名验证服务是否可用即可