SSL 证书

自动获取 Let's Encrypt 免费证书

(适用于 Linux 系统) 

安装 Certbot
sudo apt-get update
sudo apt-get install certbot python3-certbot-nginx  # Nginx 服务器
sudo apt-get install certbot python3-certbot-apache  # Apache 服务器

获取和安装证书
sudo certbot --nginx
sudo certbot --apache
admin@debian.com
Y 同意协议
Y 同意分享电子邮件
1,2,3,4 多个域名一起


配置自动更新
Let's Encrypt 证书的有效期为 90 天。为了保持证书的有效性,建议设置自动更新任务。Certbot 默认会创建一个 cron 任务来自动更新证书。
可以通过以下命令手动测试自动更新任务
sudo certbot renew --dry-run

手动获取 Let's Encrypt 免费证书(通用方法)

安装 Certbot
sudo apt-get update
sudo apt-get install certbot
sudo certbot certonly --standalone -d yourdomain.com

配置证书
获取证书后,将证书和私钥配置到你的服务器软件(如 Nginx 或 Apache)中。通常,Certbot 会将证书和私钥存放在 /etc/letsencrypt/live/yourdomain.com/ 目录下。

自动更新
设置自动更新任务以保持证书的有效性。可以通过 cron 任务来定期执行 Certbot 的更新命令:
sudo crontab -e
0 0 * * * certbot renew --quiet
以上为每天午夜执行证书的自动更新检查

配置 Nginx 或 Apaceh

自建证书

vhost.ext  域名证书附加配置信息

create_cert.sh 创建证书到 ca(中间证书 ) 以及 certs 两个目录下

CA.bat 导入证书到 windows 里的 受信任的根证书颁发机构, 也可手动 certmgr.msc 添加

vhost.ext

authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = localhost
IP.2 = 127.0.0.1
IP.3 = 192.168.1.90
DNS.4 = *.yiparts.debian
DNS.5 = admin.yiparts.debian
DNS.6 = world.yiparts.debian

IP.2 = 192.168.1.90           
表示https要访问的ip,IP.3也是ip,ssl证书说明可以自签多个ip,这是自签ip的证书

DNS.4 = demo.yiparts.debian    
表示https要访问的域名,DNS.5,DNS.6都一样是域名,ssl证书说明可以自签多个域名,这是自签域名的证书

 create_cert.sh

执行 sudo ./create_cert.sh demo.yiparts.debian

#create_cert.sh
#!/bin/bash


sudo mkdir -p ca
sudo mkdir -p certs

# 设置域名变量
# 接收参数
# 第一个参数是域名, 例 sudo ./create_cert.sh demo.yiparts.debian
DOMAIN="$1"
# 检查是否传递了参数
if [ -z "$DOMAIN" ]; then
    echo "请输入一个域名: $0 <domain>"
    exit 1
fi
# 提取基础域名部分
if [[ "$DOMAIN" == *"*."* ]]; then
    echo "自签证书泛域名会被浏览器提示不安全"
    exit 1
fi

# 证书文件名
GUID=$DOMAIN
CA="CA-$GUID"
EMAIL="admin@qg.com"

# 去 CA 目录
cd ca

# 生成 CA 私钥 需要设置密码否则会被浏览器提示不安全
sudo openssl genrsa -des3 -out "$CA.key" 2048

# 生成 CA 证书 100年
sudo openssl req -x509 -new -nodes -key "$CA.key" -sha256 -days 36500 -out "$CA.crt" -subj "/CN=$DOMAIN/O=Qg Inc/OU=It/L=Guang Zhou/ST=Guang Dong/C=CN/emailAddress=$EMAIL"

# 去 CERTS 目录
cd ..
cd certs

# 生成 SSL 私钥
sudo openssl genrsa -out "$GUID.key" 2048

# 生成 SSL 证书请求(CSR)
sudo openssl req -new -key "$GUID.key" -out "$GUID.csr" -subj "/CN=$DOMAIN/O=Qg Inc/OU=It/L=Guang Zhou/ST=Guang Dong/C=CN/emailAddress=$EMAIL"

# 使用 CA 证书签署 SSL 证书,有效期 100 年
sudo openssl x509 -req -in "$GUID.csr" -out "$GUID.crt" -days 36500 -CA "../ca/$CA.crt" -CAkey "../ca/$CA.key" -CAcreateserial -extfile "../vhost.ext"

# 查看签署的证书信息
sudo openssl x509 -in "$GUID.crt" -noout -text

# 验证 SSL 证书
sudo openssl verify -CAfile "../ca/$CA.crt" "$GUID.crt"

echo "所有操作已完成。"

CA.bat  

@echo off

chcp 65001 > nul

for %%f in (CA\*.crt) do (
    echo 导入 %%f 证书
    C:\Windows\System32\certutil -store Root | findstr /C:"%%~nf" > nul
    if errorlevel 1 (
        C:\Windows\System32\certutil -addstore -f "Root" "%%f"
    ) else (
        echo 证书 %%f 已经导入,跳过
    )
)
echo  
echo 证书已成功导入到受信任的根证书颁发机构
pause

Nginx 配置

server {
    listen 443 ssl;

    ssl_certificate /home/qg/ssl/certs/demo.yiparts.debian.crt;  
    ssl_certificate_key /home/qg/ssl/certs/demo.yiparts.debian.key;  
  
    ssl_session_timeout 5m;
    ssl_session_cache shared:SSL:50m;
    ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;    

    server_name demo.yiparts.debian;

    root /home/wwwroot/project/yiparts;  #网站根目录

    index index.php index.html index.htm;

    # Message Websocket 的 SSL 代理服务
    # php messageWorker.php -d start 守护进程方式启动 Websocket, 其它 start,restart,stop, ps -aux | grep messageWoker
    # 测试 https://demo.yiparts.debian/wss 是否正常
    # 使用域名连接 wss://demo.yiparts.debian/wss
    # 如果还是连接失败, 检查域名白名单 cfg('websocket>whiteList')
    location /wss
    {
        proxy_pass http://127.0.0.1:8181; # swoole 或 workerman 提供的 Websocket 监听地址:端口
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr; # 透传真实客户端ip, $_SERVER['HTTP_X_REAL_IP']
    }

    # 静态文件
    location ~ ^/(static|files|cache)/ {
        try_files $uri =404;  # /xxx/开头的网址直接寻找对应的文件输出,不管文件是否存在
    }

    # 这些放入上面的 /xxx/目录下
    #location ~* \.(htm|js|ts|css|jpg|jpeg|png|gif|ico|svg)$ {
    #    #ttf|eot|woff|woff2|zip|gz|pdf|xml|txt|tiff|xls|xlsx|doc|docx|ppt|pptx|cad|mp3|mp4 
    #    try_files $uri =404;  # 以 .xxx 结尾的直接寻找对应的文件输出,不管文件是否存在
    #}

    # XMVC 入口文件 index.php
    location / {
        try_files $uri $uri/ /index.php?$query_string; # 除前面的直接输出规则外所有的请求交给 index.php
    }
    
    # PHP 代理
    fastcgi_read_timeout 600s; # 开发环境下增加 FastCGI 超时时间, 用于 xdebug 调试用
    location ~ \.php$ 
    {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

        # 开发环境下  禁用 OPcache
        fastcgi_param PHP_ADMIN_VALUE "opcache.enable=0";

        # 开发环境下 添加开发者头信息
        fastcgi_param X_DEVELOP "1";
    }
}

相关推荐

  1. SSL 证书

    2024-07-09 20:14:07       25 阅读
  2. openssl生成ssl证书

    2024-07-09 20:14:07       51 阅读
  3. Golang ssl 证书 验证

    2024-07-09 20:14:07       55 阅读
  4. Tomcat配置ssl证书

    2024-07-09 20:14:07       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 20:14:07       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 20:14:07       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 20:14:07       42 阅读
  4. Python语言-面向对象

    2024-07-09 20:14:07       53 阅读

热门阅读

  1. HP打印机Er报错 (重新开始或恢复按钮 ↓)

    2024-07-09 20:14:07       18 阅读
  2. php简单实现利用飞书群里机器人推送消息的方法

    2024-07-09 20:14:07       22 阅读
  3. 终于弄明白了什么是EI!

    2024-07-09 20:14:07       20 阅读
  4. 期货量化交易:探索金融投资的新领域

    2024-07-09 20:14:07       26 阅读
  5. 探索金融数据API:现代投资的关键工具

    2024-07-09 20:14:07       24 阅读
  6. uniApp 封装VUEX

    2024-07-09 20:14:07       18 阅读
  7. H5与小程序:两者有何不同?

    2024-07-09 20:14:07       19 阅读
  8. 论文引用h指数

    2024-07-09 20:14:07       27 阅读
  9. 强化学习(Reinforcement Learning,简称RL)

    2024-07-09 20:14:07       26 阅读