生成ssl自签证书

 1、创建根私钥、证书和证书请求文件

 

openssl genrsa -out private.key 2048
openssl req -new -key private.key -out server.csr
openssl x509 -req -days 3650 -in server.csr -signkey private.key -out server.crt
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

2、nginx配置参考

        listen       443 ssl default;
        server_name  172.30.195.217 xc-pro.iflytek.work;

        ssl_certificate  /idp/public/nginx/conf/ssl/dev/server.crt;
        ssl_certificate_key  /idp/public/nginx/conf/ssl/dev/private.key;
        ssl_session_timeout  5m;

2、参考日志:

[root@nginx ssl]# mkdir -p /usr/local/nginx/ssl
[root@nginx ssl]# cd /usr/local/nginx/ssl
total 0
[root@nginx ssl]# openssl genrsa -out private.key 2048
Generating RSA private key, 2048 bit long modulus
...................+++
.........................................................................................................................................+++
e is 65537 (0x10001)
[root@nginx ssl]# openssl req -new -key private.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:zh
State or Province Name (full name) []:anhui
Locality Name (eg, city) [Default City]:hefei
Organization Name (eg, company) [Default Company Ltd]:idp
Organizational Unit Name (eg, section) []:yth
Common Name (eg, your name or your server's hostname) []:xc-pro.iflytek.work
Email Address []:623243335@qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:Cfpl@1wl
An optional company name []:Cfpl@1wl
[root@nginx ssl]# openssl x509 -req -days 3650 -in server.csr -signkey private.key -out server.crt
Signature ok
subject=/C=zh/ST=anhui/L=hefei/O=idp/OU=yth/CN=xc-pro.iflytek.work/emailAddress=623243335@qq.com
Getting Private key
[root@nginx ssl]# ll
total 12
-rw-r----- 1 root root 1675 Aug 28 16:04 private.key
-rw-r----- 1 root root 1294 Aug 28 16:06 server.crt
-rw-r----- 1 root root 1119 Aug 28 16:05 server.csr
[root@nginx ssl]# 
[root@nginx ssl]# 
[root@nginx ssl]# cd ../../sites/
[root@nginx sites]# ll
total 8
-rw-r----- 1 root root 1253 Aug 28 15:43 443.conf_bak
-rwxr-xr-x 1 root root 3734 Aug 28 14:57 80.conf
[root@nginx sites]# 
[root@nginx sites]# vim 443.conf_bak 
172.30.195.217_2023082816:08:38.log,[root@nginx sites]# ll
total 8
-rw-r----- 1 root root 1249 Aug 28 16:07 443.conf_bak
-rwxr-xr-x 1 root root 3734 Aug 28 14:57 80.conf
[root@nginx sites]# mv 443.conf_bak 443.conf
[root@nginx sites]# chmod 755 443.conf 
[root@nginx sites]# ll
total 8
-rwxr-xr-x 1 root root 1249 Aug 28 16:07 443.conf
-rwxr-xr-x 1 root root 3734 Aug 28 14:57 80.conf
[root@nginx sites]# 
[root@nginx sites]# 
[root@nginx sbin]# cd ../conf/sites/
[root@nginx sites]# vim 443.conf 

[root@nginx sites]# cd ../../sbin/
[root@nginx sbin]# ./nginx -t
nginx: the configuration file /idp/public/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /idp/public/nginx/conf/nginx.conf test is successful
[root@nginx sbin]# ./nginx -s reload

### 如何在CentOS 7.x 上生成自签名 SSL 证书 #### 准备工作 为了生成自签名 SSL 证书,首先需要安装 OpenSSL 工具包。如果没有安装该工具包,则可以通过以下命令完成安装: ```bash sudo yum install openssl -y ``` 此操作会确保系统具备必要的工具来创建和管理加密密钥以及证书请求文件[^1]。 #### 创建私有密钥与证书签署请求 (CSR) 执行下面的命令可以生成一个 RSA 私有密钥并同时制作一份证书签署请求(CSR)。这里我们指定 `-newkey rsa:2048` 参数用于定义新的 RSA 密钥长度为 2048 位,并将其保存到 `server.key` 文件中;而 CSR 将被存储至 `server.csr` 中。 ```bash openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr ``` #### 构建自签名证书 利用之前产生的 CSR 和对应的私钥,现在能够构建有效期长达十年(即 3650 天)的自签名 X.509 数字证书。以下是具体实现方式: ```bash openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt ``` 上述指令中的选项解释如下: - `-req`: 表明输入的是一个 PKCS#10 请求; - `-days 3650`: 设置证书的有效期限为 3650 日; - `-in server.csr`: 输入待处理的 CSR 文件路径名; - `-signkey server.key`: 使用本地已有的私钥对当前申请进行数字签名认证; - `-out server.crt`: 输出最终形成的 DER 编码形式的公钥证书位置。 #### 转换证书格式 (.crt 或 .pem 后缀) 有时可能还需要将 `.cer` 扩展名为基础的标准 ASCII 文本编码转成更常见的 Base64 PEM 结构化表示方法或者直接重命名为带有 `.crt`/`.pem` 的扩展名称以便于识别用途差异。可通过下述语句达成目标: ```bash openssl x509 -inform PEM -in server.cer -out server.pem ``` 或者是按照需求调整输出文件类型[^2]。 通过以上步骤即可成功地在一个基于 CentOS 7.x 平台之上建立起一套完整的自签名 SSL/TLS 加密通信机制所需的基础材料集合——包括但不限于服务器端使用的非对称算法私钥及其关联公开验证凭证副本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值