无法为 Windows 8 计算机 (xampp) 设置虚拟主机

Posted

技术标签:

【中文标题】无法为 Windows 8 计算机 (xampp) 设置虚拟主机【英文标题】:Unable to setup virtual host for windows 8 computer (xampp) 【发布时间】:2014-01-14 19:28:30 【问题描述】:

您好,我已经尝试了许多教程几个小时,但我仍然无法让虚拟主机在 windows8 平台上运行。我正在使用 xampp 1.8.3。我的 xampp 文件夹在 c 目录中安装为 xampp2

第 1 步:我编辑了 \System32\drivers\etc\hosts 文件

127.0.0.1 xampp.local

127.0.0.1 本地主机

第 2 步:我在 httpd.config 中启用了以下几行:

Include conf/extra/httpd-vhosts.conf
LoadModule vhost_alias_module modules/mod_vhost_alias.so

第 3 步:我编辑了 \apache\conf\extra\httpd-vhosts.conf

NameVirtualHost *:80

<VirtualHost 127.0.0.1:80>
    ServerAdmin admin@localhost.com
    DocumentRoot "C:\xampp2\htdocs"
    ServerName localhost
    ServerAlias localhost
    <Directory "C:\xampp\htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost 127.0.0.1:80>
    ServerAdmin admin@fountains.com
    DocumentRoot "C:\Users\Sean\Documents\vhostprjs\foods"
    ServerName xampp.local
    ServerAlias www.xampp.local
    <Directory "C:\Users\Sean\Documents\vhostprjs\foods">
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

第 4 步:重新启动 xampp 并尝试 localhost 并且它有效。但是当我尝试 www.xampp.local\ chrome 时抱怨

哎呀!谷歌浏览器找不到 www.xampp.local

我真的不知道还能做什么。

【问题讨论】:

【参考方案1】:

哦,在 Apache 2.4.9 中让它工作对我来说真的很痛苦。我找到了很多教程,但我无法让它工作。

我的解决方案适用于 APACHE 2.4 及更高版本。我已经使用 Apache 2.4.9 对其进行了测试

您需要编辑两个文件。请在进行更改之前备份这两个文件。如果您写错任何内容,即使您卸载 xampp 然后再次安装 xampp,您的本地主机也将无法工作。

第 1 步:

编辑这个文件

C:\Windows\System32\drivers\etc\hosts

使用记事本中的“以管理员身份运行”打开此文件(非常重要)。您可以这样做

开始菜单>记事本>右键单击>以管理员身份运行>打开文件

在文件末尾添加这两行

127.0.0.1       testsite.dev
127.0.0.1       www.testsite.dev

无论你去 testsite.dev 还是 www.testsite.dev ,它现在都会尝试从你的本地机器而不是从网络访问

第 2 步:

E:\xampp\apache\conf\extra\httpd-vhosts.conf

您可以正常编辑此文件,无需以“以管理员身份运行”运行此文件 在此文件末尾添加以下行

NameVirtualHost *:80
<VirtualHost *:80> 
    DocumentRoot "E:/xampp/htdocs"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin admin@.testsite.dev
    DocumentRoot "E:/xampp/htdocs/htc"
    ServerName testsite.dev
    ServerAlias www.testsite.dev
</VirtualHost>

我在E盘设置了我的xampp,所以当你从这里复制时,请确保根据你的xampp安装进行更改。最后一部分有点有趣。我指出这个药水

<VirtualHost *:80>
    ServerAdmin admin@.testsite.dev
    DocumentRoot "E:/xampp/htdocs/htc"
    ServerName testsite.dev
    ServerAlias www.testsite.dev
</VirtualHost>

您在哪里可以找到此代码?在互联网上,您可以在很多地方找到它,但可能对您不起作用,因为该代码会与您的 Apache 版本不同。那么解决方案是什么?

在文件的末尾,您会看到已经有一些注释行向您展示了如何设置虚拟主机的演示,只需复制这些行并进行必要的更改,它就会为您工作。我附上了截图更好的理解

http://postimg.org/image/5pug9f42p/

【讨论】:

【参考方案2】:

您是否尝试过使用正斜杠而不是反斜杠?

NameVirtualHost *:80

<VirtualHost 127.0.0.1:80>
    ServerAdmin admin@localhost.com
    DocumentRoot "C:/xampp2/htdocs"
    ServerName localhost
    ServerAlias localhost
    <Directory "C:/xampp/htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost 127.0.0.1:80>
    ServerAdmin admin@fountains.com
    DocumentRoot "C:/Users/Sean/Documents/vhostprjs/foods"
    ServerName xampp.local
    ServerAlias www.xampp.local
    <Directory "C:/Users/Sean/Documents/vhostprjs/foods">
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

【讨论】:

【参考方案3】:

使用以下代码...并在您的 http-vhosts.conf 中进行相应编辑

NameVirtualHost *:80
NameVirtualHost *
  <VirtualHost *>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
</VirtualHost>


<VirtualHost *>
    DocumentRoot "C:\xampp\htdocs\your_site_directory"
    ServerName your_site_directory.dev
    <Directory "C:\xampp\htdocs\your_site_directory">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

并在您的 Windows 主机文件中使用它,不要在前面添加井号#

127.0.0.1       your_site_directory.dev 

该目录可以从任何目录链接,而不是专门从 htdocs 文件夹链接,并且服务器名称可以以 .anyname 结尾,但在使用 .coms 和 .nets 时要小心,尝试访问它们时可能会导致混淆。

但如果您仍然喜欢使用 .com 或 .net,我通常会这样做。这是解决它的方法,访问您在虚拟主机中为自己(本地)创建的 .com,这就是您从地址栏中执行此操作的方法

http//your_site_name.com

对于您的外部 .com

http://www.your_site_name.com

【讨论】:

在 Windows 8 中添加 localhost 虚拟主机解决了我的问题。【参考方案4】:

哎呀!谷歌浏览器找不到 www.xampp.local

上述错误与名称解析有关。 Chrome 找不到站点“www.xampp.local”的位置。要解决此问题,您必须将完全限定的域名添加到您的主机文件:

127.0.0.1 www.xampp.local

【讨论】:

那行不通。它仍然说哎呀!谷歌浏览器找不到 www.xampp.local 我更改了: 无法解析主机名 www.xamp.local -- 忽略! @madi 如果你ping www.xamp.local 会发生什么?另外,只是一个疯狂的猜测,因为我不熟悉 apache,但是如果您在第二个块中将 ServerName 值更改为 www.xamp.local 会发生什么? 我在 Win 8 上使用主机文件没有问题。我假设您正在以管理员权限运行文件编辑器? Chrome 可能正在使用 ip6 连接到本地计算机。我唯一会尝试的是暂时禁用 ip6。否则,我就没主意了。

以上是关于无法为 Windows 8 计算机 (xampp) 设置虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 无法在 Windows 8.1 的 Xampp 控制面板中启动

Windows 8 上的 XAMPP 和 Skype 端口冲突 [重复]

无法在 XAMPP 和 Windows XP 上安装 Xdebug

在 XAMPP (WINDOWS 8.1) 中作为脚本在 PHP 中运行 Python

无法在 xampp 版本 1.8.3 上启动 mysql

curl 无法通过 ip static 连接本地 xampp