#!/bin/bash # 定义变量 DOMAIN="youip,domain" # 这里填写你的IP地址,但记住这通常不是最佳实践 DAYS=3650 # 证书有效期天数 CERT_DIR="/etc/ssl/mycerts" # 证书存储目录 CERT_FILE="$CERT_DIR/server.crt" KEY_FILE="$CERT_DIR/server.key" # 创建证书目录(如果不存在) mkdir -p "$CERT_DIR" # 生成私钥 openssl genpkey -algorithm RSA -out "$KEY_FILE" -pkeyopt rsa_keygen_bits:2048 # 生成证书签名请求(CSR) openssl req -new -key "$KEY_FILE" -out "$CERT_DIR/server.csr" -subj "/CN=$DOMAIN" # 自签名证书 openssl x509 -req -days $DAYS -in "$CERT_DIR/server.csr" -signkey "$KEY_FILE" -out "$CERT_FILE" # 清理CSR文件(可选) rm "$CERT_DIR/server.csr" echo "证书和私钥已生成在$CERT_DIR目录下"
以上为shell脚本,linux系统中安装openssl后可以进行操作生成
nginx配置
server { listen 443 ssl; server_name 192.168.1.170; #当前服务器ip地址 ssl_certificate /etc/nginx/ServerCA.crt; ssl_certificate_key /etc/nginx/ServerCA.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; location / { root /usr/share/nginx/html; index index.html; } }