欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

借助 Let’s Encrypt 免费开启 HTTPS

程序员文章站 2022-06-22 18:05:00
首先下载证书管理工具, 当前只提供了 Unix 版本 wget https://dl.eff.org/certbot-auto chmod a+x certbot-auto cert...

首先下载证书管理工具, 当前只提供了 Unix 版本

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

certbot-auto 用到了 python, 在运行 certbot-auto 之前, 需要设置一下 python 的源代理, 要不然会被墙, 这里是个巨坑.

mkdir -p ~/.pip
vim ~/.pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

运行 certbot-auto 生成证书

./certbot-auto certonly --webroot -w /var/www -d domain1 -d  domain2

关于 webroot : https://certbot.eff.org/docs/using.html

配置 Nginx, 开启 443

server {
    server_name domain1 domain2;
    listen 443;
    ssl on;

    ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/domain/chain.pem;
    ...

全面启用 HTTPS, 将 HTTP 重定向到 HTTPS

server {
        server_name domain1 domin2;
        listen 80;
        return 301 https://$host$request_uri;
}

测试重新生成证书

./certbot-auto renew --dry-run

正式环境设置定时生成证书 (minute hour day month week)

# 每周一凌晨 02:30
crontab -e
30 2 * * 1 /root/certbot/certbot-auto renew >> /var/log/certbot.log & echo certbo last renew at `date` >> /var/log/certbot.log