使用linux自建证书(ios强制https 微信小程序强制https 本地开发环境)

前言:
  作为一个运维人员不背锅,谁背呢!

正文:

现在都要https了,很多童鞋也痛苦本地该如何搭建https环境,网上一搜 一搜一大把,但是使用就。。。。。。

今天在这里笔者就教大家一步一步使用linux搭建本地的https环境。。。因我司使用的nginx 所以接口相关的服务端配置 也只列出nginx 的配置接下来看笔者如何一步一步弄好https。(可以调用openssl命令雷同!)

假设我司有alp环境,bet环境 我司线上域名为test.com 对应的则为 test.alp test.bet相关域名
则我本地应该生成通配型的*.test.alp  *.test.bet (ps:注意一点很重要三级域名需要单独配置 如:m.a.test.alp 需要单独生成 *.a.test.alp 的通配型 或者 m.a.test.alp 购买证书的时候也是如此! 一个点多1W多块钱!! 很重要!!!本篇文章不涉及三级域名只争对二级的本地配置)

一、生成根CA的key

linux下使用如下命令:
openssl genrsa -des3 -out RootCA.key 2048

过程中会出现如下提示则为需要给根证书的key设置一个密码(我直接输入了123456):

Enter pass phrase for RootCA.key:

接下来如下提示为确认输入根证书的key密码(依然123456):
Verifying - Enter pass phrase for RootCA.key:

则生成了根证书的key:

[[email protected] key]# ll

total 4

-rw-r--r-- 1 root root 1751 Jan  5 10:27 RootCA.key

[[email protected] key]#

二、生成CA的证书crt(Common Name填RootCA 其他的都不填写 这个证书是需要安卓手机 浏览器 苹果手机 mac 都需要去安装的 ps:苹果手机需要使用url的形式或者邮件的形式才能安装 很重要!!电脑版安装的时候需要将证书添加到受信任的根证书中  手机 ios 不需要 注意!!

linux下使用如下命令:

openssl req -new -x509 -sha256 -days 3650 -key RootCA.key -out RootCA.crt

然后提示输入密码(刚刚设置的123456):

Enter pass phrase for RootCA.key:

如下提示直接回车:
Country Name (2 letter code) [XX]:

如下提示直接回车:

State or Province Name (full name) []:

如下提示直接回车:

Locality Name (eg, city) [Default City]:

如下提示直接回车:

Organization Name (eg, company) [Default Company Ltd]:

如下提示直接回车:

Organizational Unit Name (eg, section) []:

如下提示填RootCA即可:

Common Name (eg, your name or your server‘s hostname) []:RootCA

如下提示直接回车:

Email Address []:

将生成RootCA.key 现在可以将这个key发给测试的同事 开发的同事 安装此证书(ps:苹果手机需要使用url的形式或者邮件的形式才能安装 很重要!!

[[email protected] key]# ll

total 8

-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt

-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key

[[email protected] key]#

三、生成*.test.alp的证书(包含key 和 crt)

1.生成*.test.alp证书的key

openssl genrsa -des3 -out testalp.key 2048

如下提示输入key的密码(我输入的依然为123456):

Enter pass phrase for testalp.key:

如下提示再次确认密码(输入123456):
Verifying - Enter pass phrase for testalp.key:

及生产一个含有密码的*.test.alp证书的key

[[email protected] key]# ll

total 12

-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt

-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key

-rw-r--r-- 1 root root 1743 Jan  5 10:48 testalp.key

[[email protected] key]#

2.刚刚生成的是带密码的 现在导出一个无密码的*.test.alp证书的key(nginx使用的就是现在生产的这个无密码的*.test.alp证书的key)

1.如下命令通过key生成一个无密码的key:
openssl rsa -in testalp.key -out testalp_nopass.key

如下提示输入密码(输入123456):
Enter pass phrase for testalp.key:

即生产了testalp_nopass.key:

[[email protected] key]# ll

total 16

-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt

-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key

-rw-r--r-- 1 root root 1743 Jan  5 10:48 testalp.key

-rw-r--r-- 1 root root 1675 Jan  5 11:00 testalp_nopass.key

[[email protected] key]#

四、根据*.test.alp的key生产证书签名的请求文件(这个请求文件等会需要 使用RootCA.key签名 这里使用 testalp_nopass.key去生成 ps:这里一定要注意一个问题 Common Name 一定要写:*.test.alp

如下命令生产请求文件testalp_nopass.csr:
openssl req -new -sha256 -days 3650 -key testalp_nopass.key -out testalp_nopass.csr

如下提示直接回车:

Country Name (2 letter code) [XX]:

如下提示直接回车:

State or Province Name (full name) []:

如下提示直接回车:

Locality Name (eg, city) [Default City]:

如下提示直接回车:

Organization Name (eg, company) [Default Company Ltd]:

如下提示直接回车:

Organizational Unit Name (eg, section) []:

如下输入*.test.alp 很重要!!

Common Name (eg, your name or your server‘s hostname) []:*.test.alp

如下提示直接回车:

Email Address []:

如下提示直接回车:

Please enter the following ‘extra‘ attributes

to be sent with your certificate request

A challenge password []:

如下提示直接回车:

An optional company name []:

则生成了名字为*.test.alp的请求文件testalp_nopass.csr:

[[email protected] key]# ll

total 20

-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt

-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key

-rw-r--r-- 1 root root 1743 Jan  5 10:48 testalp.key

-rw-r--r-- 1 root root  980 Jan  5 11:06 testalp_nopass.csr

-rw-r--r-- 1 root root 1675 Jan  5 11:00 testalp_nopass.key

[[email protected] key]#

五、最后一步通过之前生成的RootCA.key 和RootCA.crt签名testalp_nopass.csr并生成testalp_nopass.crt

如下命令:
openssl ca -md sha256 -in testalp_nopass.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out testalp_nopass.crt -cert RootCA.crt -keyfile RootCA.key

如下提示需要输入RootCa.key的密码(我的为之前配置的123456):
Enter pass phrase for RootCA.key:

很高兴的报错了:

/etc/pki/CA/index.txt: No such file or directory

unable to open ‘/etc/pki/CA/index.txt‘

139938927691592:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen(‘/etc/pki/CA/index.txt‘,‘r‘)

139938927691592:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:

[[email protected] key]#

这也是我刻意用新机器发生的错误 该错误的解决办法:

如下命令:

touch /etc/pki/CA/index.txt

然后继续执行:

openssl ca -in testalp_nopass.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out testalp_nopass.crt -cert RootCA.crt -keyfile RootCA.key

如下提示需要输入RootCa.key的密码(我的为之前配置的123456):
Enter pass phrase for RootCA.key:

很高兴又报错了:

Using configuration from /etc/pki/tls/openssl.cnf

Enter pass phrase for RootCA.key:

/etc/pki/CA/serial: No such file or directory

error while loading serial number

139989781997384:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen(‘/etc/pki/CA/serial‘,‘r‘)

139989781997384:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:

[[email protected] key]#

该错误的解决办法:

如下命令:

touch /etc/pki/CA/serial

echo "00" > /etc/pki/CA/serial

然后继续执行:

openssl ca -in testalp_nopass.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out testalp_nopass.crt -cert RootCA.crt -keyfile RootCA.key

如下提示需要输入RootCa.key的密码(我的为之前配置的123456):
Enter pass phrase for RootCA.key:

恭喜恭喜三连错:

Check that the request matches the signature

Signature ok

The mandatory stateOrProvinceName field was missing

[[email protected] key]#

该错误的解决办法:

把/etc/pki/tls/openssl.cnf中大约86 87的行

请使用vi把如下内容

stateOrProvinceName= match

organizationName= match

修改成

stateOrProvinceName= optional

organizationName= optional

在保存就可以了。

然后继续执行:

openssl ca -in testalp_nopass.csr -config /etc/pki/tls/openssl.cnf -days 3650 -out testalp_nopass.crt -cert RootCA.crt -keyfile RootCA.key

如下提示需要输入RootCa.key的密码(我的为之前配置的123456):
Enter pass phrase for RootCA.key:

如下提示输入y:

Sign the certificate? [y/n]:y

如下提示输入y:

1 out of 1 certificate requests certified, commit? [y/n]y

则签名成功:

[[email protected] key]# ll

total 28

-rw-r--r-- 1 root root 1265 Jan  5 10:42 RootCA.crt

-rw-r--r-- 1 root root 1751 Jan  5 10:33 RootCA.key

-rw-r--r-- 1 root root 1743 Jan  5 10:48 testalp.key

-rw-r--r-- 1 root root 4373 Jan  5 11:26 testalp_nopass.crt

-rw-r--r-- 1 root root  980 Jan  5 11:06 testalp_nopass.csr

-rw-r--r-- 1 root root 1675 Jan  5 11:00 testalp_nopass.key

[[email protected] key]#

到这里对如下文件进行说明:

nginx只需要testalp_nopass.crt 以及testalp_nopass.key即可

浏览器 手机端只需要安装 RootCA.crt 即可 且这个RootCA.crt是不需要到最后弄完了才可以使用,这个crt安装了之后后续该电脑 该手机设备 不需要再次更新安装!!一次安装永久有效 也不会影响后续对*.test.bet的签发(ps:苹果手机需要使用url的形式或者邮件的形式才能安装 很重要!! 电脑版安装的时候需要将证书添加到受信任的根证书中  手机 ios 不需要 注意!!

*.test.bet的签发和生产也如上*.test.alp一样即可

这里我给大家演示下效果.

nginx下的配置:

我在nginx 的conf目录下创建了一个key的文件夹存放 testalp_nopass.crt 和testalp_nopass.key:

[[email protected] key]# ll

total 12

-rw-r--r-- 1 root root 4373 Jan  5 11:26 testalp_nopass.crt

-rw-r--r-- 1 root root 1675 Jan  5 11:00 testalp_nopass.key

[[email protected] key]# pwd

/usr/local/etc/nginx/conf/key

[[email protected] key]#

配置nginx.conf增加如下配置(因只做测试 直接在nginx.conf直接配置 且项目为写的html的测试页):

server

{

listen 443;

ssl on;

ssl_certificate key/testalp_nopass.crt;

ssl_certificate_key key/testalp_nopass.key;

index index.html;

root  /data/test/;

}

主要配置为:

listen 443;

ssl on;

ssl_certificate key/testalp_nopass.crt;

ssl_certificate_key key/testalp_nopass.key;

访问一下试试会发现https了 但是貌似不对:

上说说找不到颁发者需要这里我需要先安装生产Root.crt(电脑版安装的时候需要将证书添加到受信任的根证书中  手机 ios 不需要 注意!!):

若没有添加到信任机构中则会是这样的:

所以切记电脑端需要添加到受信任的根证书颁发机构!

但是就算你配置了你会发现 还是不行 :

尽管说证书没有问题。原因在于域名!没错!是域名 这个也就是为什么这里提出来 是因为要反证之前为什么说要配置*.test.alp的重要:

nginx增加配置和电脑host解析test.test.alp之后的结果:

nginx增加配置:

server

{

listen 443;

server_name test.test.alp;

ssl on;

ssl_certificate key/testalp_nopass.crt;

ssl_certificate_key key/testalp_nopass.key;

index index.html;

root  /data/test/;

}

主要增加:

server_name test.test.alp;

配置hosts之后访问一切正常!

到最后说一点就是如果你需要重新配置需要清空/etc/pki/CA/index.txt这个文件

如下命令:
echo > /etc/pki/CA/index.txt 
即可

笔者只写了具体怎么做,但是使用到的openssl的参数还需要自行百度一下!

时间: 2025-01-01 21:37:23

使用linux自建证书(ios强制https 微信小程序强制https 本地开发环境)的相关文章

解决微信小程序配置https不成功问题

拿到一个微信小程序的项目,需要配置https安全链接(为什么必须使用https不再赘述),预想这个已经很成熟的流程,应该不会有太大问题,结果还真是出乎意料,竟然掉进一个大坑,折腾好久. 申请证书配置的详细流程不再写了,大概就是去腾讯云或者阿里云申请一个ssl证书,按照自己的网站架构下载相应的证书,导入到自己的服务器,导入方法下载时有详细教程. 这里需要注意几点: 1. htpps默认使用443端口,请确认443未被占用 2.在防火墙中添加新的入站规则,允许443端口被访问.(我用的阿里云服务器E

微信小程序免费SSL证书https、TLS版本问题的解决方案

微信小程序与第三方服务器通讯的域名5个必要条件 1.一个已备案的域名,不是localhost.也不是127.0.0.1,域名不能加端口 2.加ssl证书,也就是https://~~~ 4.HTTPS 服务器的 TLS 版本支持1.2及以下版本,一般就是1.0.1.1.1.2要同时支持这三个版本,一个也不能少,要不然就会出现下面这种情况 5.微信小程序后台加上合法域名设置(一个月内改的次数是有限的,且行且珍惜)如下图 以上5个条件,必需全满足,要不然你手机预览的时候就会发现通讯不了~~~ 另外说一

微信小程序 免费SSL证书https、TLS版本问题的解决办法

这篇文章主要介绍了微信小程序 免费SSL证书https.TLS版本问题的解决办法的相关资料,需要的朋友可以参考下 微信小程序 免费SSL证书https.TLS版本问题的解决办法 微信小程序与第三方服务器通讯的域名5个必要条件 1.一个已备案的域名,不是localhost.也不是127.0.0.1,域名不能加端口 2.加ssl证书,也就是https://~~~ 4.HTTPS 服务器的 TLS 版本支持1.2及以下版本,一般就是1.0.1.1.1.2要同时支持这三个版本,一个也不能少,要不然就会出

微信小程序http转https

微信小程序http转https?微信小程序开发后台https域名绑定和免费的https证书申请详解 微信小程序在11月3号发布了,这是一个全新的生态,没有赶上微信公众号红利的开发者,运营者可别错过这趟车了. 但是微信的后台需要全https,之前我还不相信,后台注册了后进后台才发现,服务器配置如下图 从后台的服务器配置可以看出 (1)微信小程序后台只支持https,如果公司后台还没支持https的,赶紧要升级了 (2)API请求,文件上传,socket 文件上传和下载,必须是在后台配置的,否则微信

微信小程序:nginx 设置HTTPS

微信小程序需要HTTPS服务. 程序是NGINX做前段代理,现将NGINX设置HTTPS做个总结 微信小程序需要HTTPS服务,并且有相关要求: 开发通过https://www.qcloud.com/product/ssl#userDefined10 检测SSL,显示之前配置的https未能通过.主要是算法和域名没通过. 具体操作命令如下 1 先生成key,并设置密码 openssl genrsa -des3 -out server_new.key 2048 2 通过key 再生成csr ope

为微信小程序开发做准备,在Centos 6.8下利用letsencrypt.sh脚本为nginx 配置免费https证书

原文链接: http://phpecshop.blog.51cto.com/6296699/1891737 最近在做微信小程序商城开发的时候,阅读官方api文档发现要求https,的路程. wx.request(OBJECT) wx.request发起的是 HTTPS 请求. 于是开始了在Centos 6.8下利用letsencrypt.sh脚本为nginx 配置免费https(Let's Encrypt SSL证书) 1.下载letsencrypt.sh # wget https://raw.

天河微信小程序入门:阿里云tomcat免费配置https

天河君在第一时间通过了微信小程序验证,开启了我的微信小程序之旅.因为天河君之前是一名后端狗,对前端不是很了解,所以几乎可以认为是从零开始学做微信小程序.也希望有志在微信小程序方向做点事情的朋友能够和我一起,共同进步,以致千里.上一篇是天河君在通过验证之后,兴奋的立即上手,在手机上跑了一回微信小程序.(天河微信小程序入门<一>)冷静下来后,天河发现,之前的很多demo在手机上都用不了,因为公测后得到了appId,结果导致无法与后台通讯,appId和后台的服务器域名是绑定的.没有后台的小程序是不完

为苹果ATS和微信小程序搭建 Nginx + HTTPS 服务

昨天测试开发微信小程序,才发现微信也要求用HTTPS加密数据,想来是由于之前苹果的ATS审核政策的缘故吧,微信想在苹果上开放小程序必然也只能要求开发者必须使用HTTPS了,于是在服务器上测试安装Nginx+HTTPS服务. 安装 HTTPS 最麻烦的问题应该就是获取证书了,证书感觉种类也挺复杂的,有好几种,单域.泛域.多域...还有个种标准乱七八糟的感觉,而且收费很高,还是每年买的. 现在各个云服务商也都有提供各种基础功能的免费证书,但似乎很多只对单域免费,这里的单域是指每个二级域名都算是一个域

小程序配置https

问题: 1. 微信开发者工具中,通过不检验域名可以访问,手机预览访问不了的-- 添加配置https的域名访问 申请https证书: 阿里云购买免费证书:控制台-安全(云盾)-CA证书服务-购买-一个域名&Symantec-购买 nginx配置https: #ci server=======start server { listen 80; #listen 443 ssl; server_name ci.mag998.com; root /mnt/html/ci; index index.php