{{item}}
{{item.title}}
{{items.productName}}
{{items.price}}/年
{{item.title}}
部警SSL证书可实现网站HTTPS加密保护及身份的可信认证,防止传输数据的泄露或算改,提高网站可信度和品牌形象,利于SEO排名,为企业带来更多访问量,这也是网络安全法及PCI合规性的必备要求
前往SSL证书自签名SSL证书是在缺乏官方证书颁发机构(CA)签名的情况下,由用户自行生成并签名的证书。尽管它不被浏览器默认信任,但其生成流程简单、成本为零,非常适合开发测试、内部系统或非公开网络环境。本文将详细介绍如何使用OpenSSL工具生成自签名SSL证书,包括单域名、多域名及包含子域名的证书配置,并提供部署验证的全流程指导。
OpenSSL是生成SSL证书的核心工具,需先确保其已安装在系统中:
(1)Windows 系统:
(2)Linux系统:
多数Linux发行版预装OpenSSL,若未安装,通过包管理器安装:
1 # Ubuntu/Debian
2 sudo apt-get install openssl -y
3
4 # CentOS/RHEL
5 sudo yum install openssl -y
6
7 # 验证安装
8 openssl version
(3)macOS 系统:
自带OpenSSL,可通过Homebrew更新至最新版本:
1 brew install openssl
2 openssl version
在生成证书前,需理解以下关键概念:
自签名证书的生成流程可简化为:生成私钥 → 创建CSR→ 用私钥签名CSR生成证书。
适用于仅需保护单个域名(如 example.com)的场景,步骤如下:
私钥是证书的核心,建议使用 2048 位或 4096 位 RSA 算法(4096 位安全性更高):
1 # 生成 2048 位 RSA 私钥,文件名为 server.key,不加密(开发环境用)
2 openssl genrsa -out server.key 2048
3
4 # 若需加密私钥(生产环境推荐),添加-des3参数(需设置密码,每次使用私钥时需输入)
5 openssl genrsa -des3 -out server.key 2048
执行后会生成 server.key 文件,注意:
通过私钥生成CSR,需填写证书相关信息(部分字段可留空,直接按回车跳过):
1 OpenSSLreq -new -key server.key -out server.csr
执行后会提示输入以下信息:
1 Country Name (2 letter code) [AU]:CN # 国家代码,中国为CN
2 State or Province Name (full name) [Some-State]:Beijing # 省份
3 Locality Name (eg, city) []:Beijing # 城市
4 Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany # 组织/公司名称
5 Organizational Unit Name (eg, section) []:IT # 部门名称
6 Common Name (e.g. server FQDN or YOUR name) []:example.com # 核心:证书保护的域名,必须正确
7 Email Address []:admin@example.com # 管理员邮箱
8 A challenge password []: # 可选,证书请求密码,一般留空
9 An optional company name []: # 可选,额外公司名称
关键注意: Common Name 必须填写证书要保护的域名(如 example.com),否则浏览器会提示 “证书与域名不匹配”。
用私钥对CSR进行签名,生成有效期为 365 天的证书(可修改 -days 参数调整有效期):
1 OpenSSLx509 -req -days 365 -in server.CSR-signkey server.key -out server.crt
参数说明:
执行后生成 server.crt 文件,即自签名证书,包含公钥和域名等信息。
若需保护多个域名(如 example.com、example.net)或子域名(如 api.example.com),需在证书中添加SAN字段,步骤如下:
手动创建一个 openssl.cnf 配置文件,指定 SAN 域名列表:
1 [req]
2 default_bits = 2048
3 prompt = no
4 default_md = sha256
5 distinguished_name = dn
6 x509_extensions = v3_ca
7
8 [dn]
9 C = CN
10 ST = Beijing
11 L = Beijing
12 O = MyCompany
13 OU = IT
14 CN = example.com # 主域名
15
16 [v3_ca]
17 subjectAltName = @alt_names
18 keyUsage = digitalSignature, keyEncipherment
19 extendedKeyUsage = serverAuth
20 basicConstraints =CA:FALSE # 非CA证书,仅用于服务器认证
21
22 [alt_names]
23 DNS.1 = example.com # 主域名
24 DNS.2 = www.example.com # 子域名1
25 DNS.3 = api.example.com # 子域名2
26 DNS.4 = example.net # 其他主域名
配置说明:
直接通过私钥和配置文件生成证书(无需单独生成CSR):
1 # 生成私钥(若已生成可跳过)
2 openssl genrsa -out multi-domain.key 2048
3
4 # 基于配置文件生成包含SAN的自签名证书,有效期365天
5 openssl req -new -x509 -days 365 -key multi-domain.key -out multi-domain.crt -configOpenSSL.cnf
执行后生成 multi-domain.crt 证书,可同时保护 example.com、api.example.com等多个域名。
生成证书后,需验证其有效性及包含的信息:
1 openssl rsa -noout -text -in server.key
输出会显示私钥的算法(RSA)、位数(2048)等信息,确认无误。
1 openssl req -noout -text -in server.csr
检查 Subject 字段中的 Common Name 是否正确, X509v3 Subject Alternative Name 是否包含所需域名(多域名证书)。
1 openssl x509 -noout -text -in server.crt
重点检查:
生成的证书可部署到 Web 服务器(如 Nginx、Apache)、应用程序(如 Node.js)等场景:
1 server {
2 listen 443 ssl;
3 server_name example.com; # 与证书中的域名一致
4
5 ssl_certificate /etc/nginx/ssl/server.crt; # 证书路径
6 ssl_certificate_key /etc/nginx/ssl/server.key; # 私钥路径
7
8 # 可选:优化 SSL 配置
9 ssl_protocols TLSv1.2 TLSv1.3;
10 ssl_ciphers HIGH:!aNULL:!MD5;
11 }
1 sudo nginx -t # 检查配置是否有误
2 sudo systemctl restart nginx
1 <VirtualHost *:443>
2 ServerName example.com
3 SSLEngine on
4 SSLCertificateFile /etc/httpd/ssl/server.crt
5 SSLCertificateKeyFile /etc/httpd/ssl/server.key
6 </VirtualHost>
1 sudo systemctl restart httpd
用浏览器访问 https://example.com 时,会提示 “您的连接不是私密连接”(因自签名证书不被信任):
(1)开发环境:可手动信任证书(以 Chrome 为例):
(2)生产环境:自签名证书不适合公开服务,需购买CA签名的证书(如Let's Encrypt免费证书)。
使用OpenSSL生成自签名SSL证书是开发者必备技能,通过本文的步骤,可快速生成单域名、多域名及包含子域名的证书,满足开发测试和内部系统的加密需求。需注意,自签名证书仅适用于非公开场景,公开服务仍需依赖CA签名证书以确保用户信任。掌握证书生成流程后,可进一步学习证书吊销、更新及自动化管理(如通过 Certbot 自动续期Let's Encrypt证书),构建更安全的网络环境。
Dogssl.cn拥有20年网络安全服务经验,提供构涵盖国际CA机构Sectigo、Digicert、GeoTrust、GlobalSign,以及国内CA机构CFCA、沃通、vTrus、上海CA等数十个SSL证书品牌。全程技术支持及免费部署服务,如您有SSL证书需求,欢迎联系!