如何在 XAMPP 的本地主机中创建有效的 SSL

How to Create Valid SSL in localhost for XAMPP

如何在 XAMPP 和 windows 中使用安全连接 (SSL)?

打开本地主机页面时出现以下错误:

connection not secure

在我的 XAMPP 安装中,我基本上对我管理的所有站点都有一个克隆。他们所有人(当然)都使用 SSL/HTTPS.

这是一步一步的指南:

在这一步中,我们将创建 SSL 并设置“site.test”网站。

1.导航到 XAMPP.

中的 Apache 目录

常规安装在 C:\xampp\apache.

2。在该页面中创建一个文件夹。

这是我们存储证书的地方。在这个例子中,我将创建“crt”文件夹。所以我们将有 C:\xampp\apache\crt

3。添加此文件。

4.编辑 cert.conf 和 运行 make-cert.bat

使用我们要使用的域更改 {{DOMAIN}} 文本,在本例中为 site.test 并保存.

双击 make-cert.bat 并在出现提示时输入域 site.test。只需输入其他问题,因为我们已经从 cert.conf.

设置了默认值

Note: I don’t know how to do text replace in .bat script, if you do, let me know in the comment how to do it and I will update make-cert.bat to automatically replace the {{DOMAIN}} with the domain input.

5.在 windows.

中安装证书

之后,您将看到 site.test 文件夹已创建。在该文件夹中,我们将有 server.crtserver.key。这是我们的 SSL 证书。

双击 server.crt 将其安装到 Windows 以便 Windows 可以信任它。

然后 select 本地计算机 作为存储位置。

然后Select“将所有证书放入以下商店”然后单击浏览和select受信任的根证书颁发机构。

单击下一步完成

现在此证书已在 Windows 中安装并受信任。接下来是如何在XAMPP.

中使用这个证书

6.在 Windows hosts

中添加站点
  • 以管理员身份打开记事本。
  • 编辑C:\Windows\System32\drivers\etc\hosts(文件没有扩展名)
  • 在新行中添加:
127.0.0.1 site.test

这会告诉windows在我们访问http://site.test时加载XAMPP 你可以试试,它会显示XAMPP仪表板页面。

7.在 XAMPP conf.

中添加站点

我们需要为此域启用 SSL 并让 XAMPP 知道我们存储 SSL 证书的位置。所以我们需要编辑 C:\xampp\apache\conf\extra\httpd-xampp.conf

并在底部添加此代码:

 ## site.test
 <VirtualHost *:80>
     DocumentRoot "C:/xampp/htdocs"
     ServerName site.test
     ServerAlias *.site.test
 </VirtualHost>
 <VirtualHost *:443>
     DocumentRoot "C:/xampp/htdocs"
     ServerName site.test
     ServerAlias *.site.test
     SSLEngine on
     SSLCertificateFile "crt/site.test/server.crt"
     SSLCertificateKeyFile "crt/site.test/server.key"
 </VirtualHost>

之后,您将需要在 XAMPP 中重新启动 Apache。很简单,打开XAMPP控制面板,停止,重新启动Apache模块即可。

Tips: In XAMPP conf, as you can see you can change the domain root directory if needed. Eg. as sub-dir in htdocs.

8.重新启动浏览器并完成!

这是加载证书所必需的。然后在你的浏览器上访问域名,你会看到绿色的锁!

希望这篇教程对你有用!

来源:https://shellcreeper.com/how-to-create-valid-ssl-in-localhost-for-xampp/