一、Centos7.9下使用openssl生成自签名SSL证书过程记录
一般在CentOS7上openssl已经默认安装。可使用openssl version检查安装的openssl。openssl version OpenSSL 1.0.2 . 安装了OpenSSL的话,接下来开始按步骤生成SSL证书。
第一步:生成私钥
新建/etc/ssl/certs/www.xx.com目录,https也可以和IP地址关联的,为了分类管理,这里建议将目录名和域名或者IP地址一致。进入目录执行:
openssl genrsa -des3 -out server.key 2048
执行会提示输入,此时输入一个4位以上的密码并重复输入即完成生成自签名的SSL证书和私钥。
第二步:生成CSR(证书签名请求)
openssl req -new -key server.key -out server.csr -subj "/C=CN/ST=Shandong/L=Qingdao/O=Compa/OU=test/CN=192.168.162.22"
subj参数中C是Country(国家)、ST=是省(State or Province)、L=是城市(Location or City)、
O是(组织或企业Organization)、OU是(部门Organization Unit)、CN是(域名或IP:Common Name)
在执行上面这一步时有可能碰到报错:
Can't load /root/.rnd into RNG
139870213177792:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:88:Filename=/root/.rnd
对应的解决办法是进入/root目录执行命令:
cd /root && openssl rand -writerand .rnd
第三步:去除私钥中的密码
在第1步创建私钥时设定了密码。而这个密码会带来一个副作用,那就是在每次启动Web服务器时,都会要求输入密码,这显然非常不方便。要删除私钥中的密码,操作如下:
openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:
writing RSA key
第四步:生成自签名SSL证书
# -days 证书有效期-天
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=C = CN, ST = Shandong, L = Qingdao, O = Compa, OU = test, CN = 192.168.162.22
Getting Private key
X.509证书包含三个文件:key,csr,crt。X.509是密码学一个标准,规范了公开秘钥认证、证书吊销列表、授权凭证、凭证路径验证算法等。我们需要用到的证书文件为:server.crt 和 server.key。
#. 三个文件的意义如下:
key是服务器上的私钥文件,用于对发送给客户端数据的加密,以及对从客户端接收到数据的解密
csr是证书签名请求文件,用于提交给证书颁发机构(CA)对证书签名
crt是由证书颁发机构(CA)签名后的证书,或者是开发者自签名的证书,包含证书持有人的信息,持有人的公钥,以及签署者的签名等信息
然后把上面的证书文件放到nginx配置文件的certs文件夹中,注意在docker中使用ssl证书需要记住把证书文件路径放对,即要放到容器中的nginx可以访问的地址中,不然启动nginx就会报错nginx: [emerg] cannot load certificate。使用docker部署https,只需要针对映射到的容器IP进行ssl签名。比如由一个外网IP映射到一个内网服务器IP的443端口,只需要针对这个内网服务器IP进行签名即可。
二、certbot-auto更新https证书时出现更新失败urlopen error timed out导致证书过期
之前已经使用免费的certbot-auto 做了本站的https证书,原网址: 可以在本站中搜索相关内容找到文章,更新证书的后台服务一直在跑着没怎么看,今天访问https页面时提示证书错误(查看提示证书已过期),就上来看一下,发现更新出了问题。
因为请求的域名 raw.githubusercontent.com 失败,导致证书更新失败,先暂指一下host解决。过程记录如下:
[root@007 ~]# /opt/modules/https/certbot-auto renew
Upgrading certbot-auto 1.0.0 to 1.8.0...
Couldn't download https://raw.githubusercontent.com/certbot/certbot/v1.8.0/letsencrypt-auto-source/letsencrypt-auto. <urlopen error timed out>
[root@007 ~]# vim /etc/hosts
1 127.0.0.1 localhost
5 199.232.4.133 raw.githubusercontent.com
[root@007 ~]# /opt/modules/https/certbot-auto renew
Bootstrapping dependencies for RedHat-based OSes... (you can skip this with --no-bootstrap)
yum is /usr/bin/yum
yum is hashed (/usr/bin/yum)
Loaded plugins: security
Setting up Install Process
Package gcc-4.4.7-23.el6.x86_64 already installed and latest version
Package augeas-libs-1.0.0-10.el6.x86_64 already installed and latest version
Package openssl-1.0.1e-58.el6_10.x86_64 already installed and latest version
Package openssl-devel-1.0.1e-58.el6_10.x86_64 already installed and latest version
Package libffi-devel-3.0.5-3.2.el6.x86_64 already installed and latest version
Package redhat-rpm-config-9.0.3-51.el6.centos.noarch already installed and latest version
Package ca-certificates-2020.2.41-65.1.el6_10.noarch already installed and latest version
Package python-devel-2.6.6-68.el6_10.x86_64 already installed and latest version
Package python-virtualenv-12.0.7-1.el6.noarch already installed and latest version
Package python-tools-2.6.6-68.el6_10.x86_64 already installed and latest version
Package python-pip-7.1.0-2.el6.noarch already installed and latest version
Nothing to do
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/007.cn.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
NGINX configured with OpenSSL alternatives is not officially supported by Certbot.
Plugins selected: Authenticator nginx, Installer nginx
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for 007.cn
http-01 challenge for www.007.cn
Waiting for verification...
Cleaning up challenges
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/007.cn/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/007.cn/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#不用更新certbot-auto 软件本身,直接更新证书的方式
[root@007 ~]# /opt/modules/https/certbot-auto renew --no-self-upgrade
另外找到certbot-auto有个选项:--no-self-upgrade。certbot-auto默认始终尝试更新到最新版,但对已经稳定的应用而言,可以不用更新,因此可以使用此选项直接更新证书,而不用更新certbot-auto导致其它意外问题。