WSL2下用mkcert实现localhost安全证书

说明: 

环境:win10, 已装WSL2, Ubuntu18 LTS, Apache2

要求:为localhost, test.com安装证书,使之能以https://localhost, https://t1.test.com 形式访问

结果:设置后,firefox / ie能以https访问, chrome / edge 不能

Mkcert 仓库: https://github.com/FiloSottile/mkcert

步骤: 

1. 安装mkcert

1.1 Win10下安装chocolatey,因为要用它安装mkcert,管理员身份cmd

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

然后

mkcert -install

# 查看证书路径
mkcert -CAROOT
C:\Users\ben.BEN-NOTEBOOK-20\AppData\Local\mkcert

# 开证书
mkcert localhost
mkcert t1.test.com

1.2 从证书路径拷贝到apache2可以访问的地方,比如:/etc/apache2/CA/localhost

2. 配Apache2

2.1 /etc/apache2/apache2.conf

LoadModule ssl_module /usr/lib/apache2/modules/mod_ssl.so

2.2 /etc/apache2/sites-available/sites.conf

<VirtualHost *:443> 
   ServerName t1.test.com
   ServerAlias *.t1.test.com
   DocumentRoot "/mnt/d/Projects/BD/t1/src"

   #DENY FILES
     <Files ~ (/.user.ini|/.htaccess|/.git|/.svn|/.project|LICENSE|README.md)$>
       Order allow,deny
       Deny from all
    </Files>
   
   <Directory "/mnt/d/Projects/BD/t1/src">
      # # use mod_rewrite for pretty URL support
      # RewriteEngine on
      # # If a directory or a file exists, use the request directly
      # RewriteCond %{REQUEST_FILENAME} !-f
      # RewriteCond %{REQUEST_FILENAME} !-d
      # # Otherwise forward the request to index.php
      # RewriteRule . index.php

       Options Indexes FollowSymLinks MultiViews 
       AllowOverride All 
       Order allow,deny 
       Allow from all 
   </Directory> 
   
   SSLEngine on
   SSLCertificateFile "/etc/apache2/CA/localhost/t1.test.com.pem"
   SSLCertificateKeyFile "/etc/apache2/CA/localhost/t1.test.com-key.pem"
</VirtualHost>

重启 service apache2 restart 

3. 测试访问

4. 其他

实际上,也在ubuntu中装了mkcert, 但我认为起效的应该是前者,所以ubuntu下装与否取决用户,留下方法:

curl -Lo mkcert https://github.com/FiloSottile/mkcert/releases/download/v1.4.3/mkcert-v1.4.3-linux-amd64 &&
   sudo install mkcert /usr/local/bin/

参考:

Using mkcert for secure localhost development on WSL 2

kubernetes - Generating certificate for local dev environment in WSL2 - Stack Overflow

How to use HTTPS for local development

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bennybi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值