• letsencrypt + centsos7.9 + docker + express 搭建https环境


    参考官网教程网址 (https://certbot.eff.org/instructions?ws=other&os=ubuntubionic)

    letsencrypt + centsos7.9 + docker + express 搭建https环境

    1. 安装 snapd (https://snapcraft.io/docs/installing-snap-on-centos), 用于安装certbot
      我的系统是centos7
      使用一下命令
    sudo yum install epel-release
    
    sudo yum install snapd
    
    sudo systemctl enable --now snapd.socket
    
    sudo ln -s /var/lib/snapd/snap /snap
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 更新snapd
    sudo snap install core
    
    sudo snap refresh core
    
    • 1
    • 2
    • 3
    1. 删除之前安装的certbot-auto
    sudo yum remove certbot
    
    • 1
    1. 安装Certbot
    sudo snap install --classic certbot
    
    • 1
    1. 链接certbot 命令

    使用软链接

    sudo ln -s /snap/bin/certbot /usr/bin/certbot
    
    • 1
    1. 生成证书

    交互式:

    sudo certbot certonly --webroot
    
    • 1

    或者
    sudo certbot certonly --webroot -w 静态文件目录 -d 域名

    静态文件目录 也就是 前端打包生成dist文件后放在后端服务的目录, 确保能够通过80端口访问这个目录, letsencrypt 会在里面新建一个写了特定字符串的文件,比对外网访问结果,来验证域名是否是属于你的

    express是 /public

    生成证书key:
    Certificate is saved at: /etc/letsencrypt/live/域名/fullchain.pem
    Key is saved at: /etc/letsencrypt/live/域名/privkey.pem

    1. 使用生成证书
    const express = require('express')
    const http = require('http')
    const https = require('https')
    const fs = require('fs')
    const app = express()
    
    app.get('/ping', (req, res) => {
      console.log('[long] get !!: ', req.protocol)
      res.send('pong')
    })
    
    app.get('/', (req, res) => {
      console.log('[long] get !!: ', req.protocol)
      res.send('test  !')
    })
    
    const key = fs.readFileSync('./cert/privkey.pem', 'utf8')
    const cert = fs.readFileSync('./cert/fullchain.pem', 'utf8')
    const options = {key, cert}
    
    http.createServer(app).listen(88, () => {
      console.log('\n\n\n started !')
    })
    
    https.createServer(options, app).listen(446, () => {
      console.log('https started !')
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    1. 自动更新证书

    有效期为90天,官方已经自带更新功能

    sudo certbot renew --dry-run
    运行后显示没有错误即可

    1. 确认你的网站已经支持https,访问后网址左边有个小锁的图标

    2. 证书管理(参考 https://eff-certbot.readthedocs.io/en/stable/using.html#where-certs )

    所有生成的密钥和颁发的证书都可以在 /etc/letsencrypt/live/$domain中找到

    执行

    sudo chmod 0755 /etc/letsencrypt/{live,archive}
    
    • 1

    privkey.pem 证书的私钥需要永远保密

    1. 证书自动更新脚本
      参考 https://eff-certbot.readthedocs.io/en/stable/using.html#re-creating-and-updating-existing-certificates

    先更新证书:

    sudo certbot certonly --webroot -w /data/overseaFileServerData/app/node/ssl/ -d xx.com
    
    • 1

    编写shell更新脚本,设置服务器启停

    echo '执行renewCert.sh 脚本'
    sudo cp -f /etc/letsencrypt/live/xx.com/fullchain.pem /data/xx/app/node/cert/fullchain.pem
    sudo cp -f /etc/letsencrypt/live/xx.com/privkey.pem /data/xx/app/node/cert/privkey.pem
    cd /home/centos/xx/deploy/docker
    sudo docker-compose restart
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 安装certbot(https://certbot.eff.org/instructions?ws=other&os=ubuntubionic)
    2. 拷贝项目中的 renewCert.sh 文件到~/snap/目录,renewCert.sh为更新成功后执行的脚本
    3. 添加更新成功运行时钩子:
    sudo sh -c 'printf "#!/bin/sh\nsh /home/huangshuxin/snap/renewCert.sh\n" > /etc/letsencrypt/renewal-hooks/deploy/afterRenew.sh'
    sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/afterRenew.sh
    
    • 1
    • 2
    1. 测试: sudo certbot renew --force-renewal
    2. 进入网站,点击网址处的加密标志,查看颁发日期是否是新的。

    如果你想手动更新https证书

    sudo certbot renew --force-renewal --deploy-hook "sh /home/huangshuxin/snap/renewCert.sh"
    
    • 1
  • 相关阅读:
    2024华为OD机试真题-攀登者1-C++(C卷D卷)
    分片机制在redis中的实践
    【信号处理】非线性信号处理(Python代码实现)
    零基础成为网络工程师经验分享,附完整学习路线
    maven configuration
    【JavaScript预解析】
    mybatis-puls常用全量配置以及开启sql日志(application.properties方式)
    央企招聘:正式编制!八险三金!各项福利!中国邮政招人啦!
    Netty笔记
    Jetpack DataBinding使用--Jetpack系列
  • 原文地址:https://blog.csdn.net/change_fate/article/details/127620199