关于如何申请免费证书让自己的网站Https

前提

  • 一个有公网IP的服务器
  • 一个备案通过的域名
  • 有基础的Linux基础操作能力

开始

教程官网

这里笔者以centos8+Nginx,但实际上我的系统信息cat /etc/os-release

NAME="Alibaba Cloud Linux"
VERSION="3 (Soaring Falcon)"
ID="alinux"
ID_LIKE="rhel fedora centos anolis"
VERSION_ID="3"
PLATFORM_ID="platform:al8"
PRETTY_NAME="Alibaba Cloud Linux 3 (Soaring Falcon)"
ANSI_COLOR="0;31"
HOME_URL="https://www.aliyun.com/"

我买的是阿里云的服务器,在选择系统时看了有Alibaba Cloud Linux 3的选项,查了下在资料,基本说着兼容什么什么系统,你可以理解成,就是基于什么什么系统,然后做了一些本土化设置,以及一些优化等等,参考鸿蒙。

接下来的所有操作皆是在root用户操作下,

安装snapd

# 这一步如果报错,说明系统中已经有这个包了。可以跳过
yum install epel-release 
yum install snapd
systemctl enable --now snapd.socket
ln -s /var/lib/snapd/snap /snap

执行完过后,重启一下实例,操作之前我建议你删完全移除nginx,并删除etc/nginx此目录,

安装Nginx

# 运行以下命令,安装Nginx。
yum -y install nginx

# 运行以下命令,查看Nginx版本。
nginx -v

# 返回结果类似如下所示,表示Nginx安装成功。
# nginx version: nginx/1.20.1

# 启动 Nginx
systemctl start nginx 

# 设置开机自启 Nginx
systemctl enable nginx

修改nginx配置

vim /etc/nginx/nginx.conf

删除默认的server配置

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

填写自己的配置

server {
    server_name  wangijun.com; # 这里填写你的域名
    # 这里填写你静态网站的根目录
    root         /home/wlf/hexoblog;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

使用certbot申请SSL证书

为单域名申请SSL证书

# 安装 certbot
snap install --classic certbot

# 创建链接
ln -s /snap/bin/certbot /usr/bin/certbot

# 执行配置,中途会询问你的邮箱,如实填写即可,
certbot --nginx

# 系统上的Certbot包附带一个cron作业或systemd计时器,它将在证书到期前自动续订证书。

# 模拟测试自动续订
certbot renew --dry-run

为子域名申请SSL证书

--expand-d

告诉Certbot使用包含所有旧域和一个或多个附加新域的新证书更新现有证书。

使用该命令指定所有已有域名和一个或多个新域名

# 此例子,假设你已经设置了existing.com域名,新增加的两个域名为,example.com,newdomain.com
certbot --expand -d existing.com,example.com,newdomain.com


# 此例子,假设你已经设置了existing.com,example.com域名,新增加的一个域名为,newdomain.com
certbot --expand -d existing.com,example.com,newdomain.com

这种方式适合子域名不多的情况下,但理论上是无限的,就是命令有点长。

自动续约过后重启Nginx

执行命令

certbot renew --deploy-hook "systemctl restart nginx"

撤销证书

如果需要吊销证书,请使用子命令执行此操作。revoke

可以通过提供证书名称或提供证书路径来吊销证书 查看证书路径:certbot certificates

certbot revoke --cert-name example.com
# OR
certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem

关于如何申请免费证书让自己的网站Https
https://wangijun.com/2023/11/22/other-18/
作者
无良芳
发布于
2023年11月22日
许可协议