【localhost加SSL】nginx +ssl + php + localhost

参考文章(该文章本人尝试不成功,但以此为基础成功了)

博主:ccw1078
连接:https://www.jianshu.com/p/2e77699ca53a

1.安装 OpenSSL

由于 Windows 不像 Mac OS 和 Linux 自带 openssl,需要另外下载安装;
下载链接:点击下载
本人软件安装在 D:\Program Files (x86)\GnuWin32

2.新建配置文件

使用记事本或者其他编辑器,新建文件,命名为 localhost.cnf (注:后缀为 cnf,不是 txt)
文件内容设置如下并保存:

[dn]
CN=localhost

[req]
distinguished_name = dn

[EXT]
subjectAltName=DNS:localhost
keyUsage=digitalSignature
extendedKeyUsage=serverAuth

本人软件保存为在 D:\localhost.cnf

3.生成证书

注:假设 OpenSSL 的安装位置为 ‘D:\Program Files (x86)\GnuWin32’,
打开文件'D:\Program Files (x86)\GnuWin32\bin\openssl.exe'
然后执行命令

req -x509 -out localhost.crt -keyout localhost.key -newkey rsa:2048 -nodes -sha256  -subj '//skip=yes/CN=localhost' -extensions EXT -config d:\localhost.cnf

下图就成功,crt和key文件在D:\Program Files (x86)\GnuWin32\bin中
在这里插入图片描述
在这里插入图片描述

4.安装证书

本人使用nginx为例

① 打开vhost.conf(不建议直接在nginx.conf中更改)

直接复制下面代码保存

server {
        listen       443;
        server_name  localhost localhost;
        # 本地网站地址
        root   "C:\\Users\\Administrator\\Desktop\\tp6\\public";
        ssl                  on;
        # crt证书地址(自己放在哪里写哪里)
        ssl_certificate "D:/phpStudy/PHPTutorial/key/localhost.crt";
         # key证书地址(自己放在哪里写哪里)
        ssl_certificate_key "D:/phpStudy/PHPTutorial/key/localhost.key";
        ssl_session_timeout  5m;
        
        # thinkPHP标准配置
		location / {
			index index.php;
			if (!-e $request_filename){
				rewrite  ^(.*)$  /index.php?s=$1  last;   break;
			}
		}
		# php解析必须的,否则全部当文件下载
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}
②重启nginx
③成功(不安全是正常的,但不影响使用特别是easywechat这类需要ssl的)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值