说明:
环境: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