首页 > Web开发 > 详细

生成https证书

时间:2021-04-01 23:31:34      阅读:30      评论:0      收藏:0      [点我收藏+]

代码如下:

#!/bin/bash

hostname=192.168.157.128
  
rm -rf /etc/pki/CA &>/dev/null
mkdir -p /etc/pki/CA/private && cd /etc/pki/CA
yum -y install expect &>/dev/null

#CA生成一对密钥
(umask 077;openssl genrsa -out private/cakey.pem 2048)

#提取公钥
openssl rsa -in private/cakey.pem -pubout

#生成自签署证书
expect << EOF
     set timeout 60
     spawn openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
     expect "letter code"         {send "cn\r"}
     expect "full name"           {send "HB\r"}
     expect "city"                {send "WH\r"}
     expect "company"             {send "runtime\r"}
     expect "section"             {send "teach\r"}
     expect "hostname"            {send "${hostname}\r"}
     expect "Email"               {send "xx@xx.com\r"}
     expect "#"
EOF

#读出cacert.pem证书的内容
openssl x509 -text -in cacert.pem
mkdir certs newcerts crl
touch index.txt && echo 01 > serial

#安装httpd
yum -y remove httpd &>/dev/null
yum -y install httpd &>/dev/null
systemctl enable --now httpd &>/dev/null

#httpd服务器生成密钥
cd /etc/httpd && mkdir ssl && cd ssl
(umask 077;openssl genrsa -out httpd.key 2048)

#生成证书签署请求
expect << EOF
     set timeout 60
     spawn openssl req -new -key httpd.key -days 365 -out httpd.csr 
     expect "letter code"         {send "cn\r"}
     expect "full name"           {send "HB\r"}
     expect "city"                {send "WH\r"}
     expect "company"             {send "runtime\r"}
     expect "section"             {send "teach\r"}
     expect "hostname"            {send "${hostname}\r"}
     expect "Email"               {send "1@2.com\r"}
     expect "password"            {send "\r"}
     expect "company name"        {send "\r"}
     expect "#"
EOF

#CA签署客户端提交上来的证书
expect << EOF
     set timeout 60
     spawn openssl ca -in ./httpd.csr -out httpd.crt -days 365
     expect "certificate"        {send "y\r"}
     expect "commit"             {send "y\r"}
     expect "#"
EOF

#修改配置文件
yum -y remove mod_ssl &>/dev/null
yum -y install mod_ssl &>/dev/null
sed -i "s/#DocumentRoot/DocumentRoot/g" /etc/httpd/conf.d/ssl.conf
sed -i "s/#ServerName www.example.com:443/ServerName ${hostname}:443/g" /etc/httpd/conf.d/ssl.conf
sed -i "s#/etc/pki/tls/certs/localhost.crt#/etc/httpd/ssl/httpd.crt#g" /etc/httpd/conf.d/ssl.conf
sed -i "s#/etc/pki/tls/private/localhost.key#/etc/httpd/ssl/httpd.key#g" /etc/httpd/conf.d/ssl.conf

#重启httpd
systemctl restart httpd &>/dev/null
ss -antl
Data Base Updated
State        Recv-Q        Send-Q                 Local Address:Port                 Peer Address:Port        
LISTEN       0             128                          0.0.0.0:22                        0.0.0.0:*           
LISTEN       0             128                             [::]:22                           [::]:*           
LISTEN       0             128                                *:443                             *:*           
LISTEN       0             128                                *:80                              *:*  

 

生成https证书

原文:https://www.cnblogs.com/lichouluoyu/p/14607735.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!