Xampp 虚拟主机不工作

Posted

技术标签:

【中文标题】Xampp 虚拟主机不工作【英文标题】:Xampp vhosts do not work 【发布时间】:2013-09-11 11:08:07 【问题描述】:

我在使用 xampp 时遇到问题。安装后,我的虚拟主机根本不想工作。 当我尝试访问http://localhost/ 时 - 效果很好。但是,如果我输入其他内容,例如 http://laravel.dev,它会给我一个错误。

主机

# Copyright (c) 1993-2009 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost


127.0.0.1       localhost
127.0.0.1       laravel.dev

虚拟主机

# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80


#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ##ServerName or ##ServerAlias in any <VirtualHost> block.
#
##<VirtualHost *:80>
    ##ServerAdmin webmaster@dummy-host.example.com
    ##DocumentRoot "C:/xampp2/htdocs/dummy-host.example.com"
    ##ServerName dummy-host.example.com
    ##ServerAlias www.dummy-host.example.com
    ##ErrorLog "logs/dummy-host.example.com-error.log"
    ##CustomLog "logs/dummy-host.example.com-access.log" common
##</VirtualHost>

##<VirtualHost *:80>
    ##ServerAdmin webmaster@dummy-host2.example.com
    ##DocumentRoot "C:/xampp2/htdocs/dummy-host2.example.com"
       ##ServerName dummy-host2.example.com
    ##ErrorLog "logs/dummy-host2.example.com-error.log"
    ##CustomLog "logs/dummy-host2.example.com-access.log" common
##</VirtualHost>


<VirtualHost *:80>
    DocumentRoot "C:/xampp2/htdocs"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "C:/xampp2/sites/laravel.dev/www"
    ServerName laravel.dev
</VirtualHost>

谢谢!

【问题讨论】:

【参考方案1】:

你到底看到了什么样的错误?

如果您没有在 Vhost 中指定正确的值,则可能会出现问题。尝试使您的第二个虚拟主机适应以下内容:

<VirtualHost *:80>
    ServerName laravel.dev
    DocumentRoot "C:/xampp2/sites/laravel.dev/www"
    SetEnv APPLICATION_ENV "development"
    <Directory "C:/xampp2/sites/laravel.dev/www">
        DirectoryIndex index.php   ## Edit this to your needs, e.g. index.html
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

另外,我在使用绝对路径时遇到了一些问题,请尝试使用相对路径(例如“sites/laravel.dev/www”)并检查是否有所不同。

【讨论】:

【参考方案2】:

当我第一次开始使用 XAMPP 时,我遇到了同样的问题。在网上很难找到答案,所以我开始尝试自己。对我有用的解决方案是清除浏览器缓存。 :) 是的,就是这么简单......

对于那些不知道的人:

hosts文件可以在C:\Windows\System32\drivers\etc目录下找到,

httpd-vhost.conf 可以在 C:\xampp\apache\conf\extra 中找到目录

附言。给你 - 我的工作配置。 (不要忘记在对这些文件进行更改后清除浏览器缓存并在 xampp 面板中重新启动 Apache 服务器)请注意,要编辑 hosts 文件,您必须以管理员身份运行编辑程序,例如 Notepad++ .如果您不这样做,您的编辑程序将无法对该文件进行任何更改。*

主机

127.0.0.1  symfony.dev
127.0.0.1  symfony

httpd-vhost.conf

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin webmaster@symfony.com
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
    ServerAlias 127.0.0.1
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@symfony.com
    DocumentRoot "C:/xampp/htdocs/www/_DoNotTouch_Symfony2/path/web"
    ServerName symfony.dev
</VirtualHost>

还有一件小事...当您在浏览器中运行虚拟服务器时,请确保您将在服务器名称之前使用 http://(最后在您第一次调用它时)。所以运行:

http://symfony.dev

之后你将只能使用

symfony.dev

(无论如何它在 Google Chrome 浏览器中的工作方式)

如果我帮助解决了您的问题,请竖起大拇指;-)。

亲切的问候

【讨论】:

以上是关于Xampp 虚拟主机不工作的主要内容,如果未能解决你的问题,请参考以下文章

Xampp for Laravel 上的虚拟主机不工作

XAMPP 的多个虚拟主机不工作

虚拟主机在 xampp 中不工作(对 localhost 使用不同的端口:7777)

Xampp 本地主机不工作

Xampp - 本地主机不工作

XAMPP v1.7.4 安装问题,本地主机不工作