如何在Xampp上启用虚拟主机以支持Laravel?

14
我正在运行Windows 7 Pro上的XAMPP。我想设置虚拟主机,这样当我使用“dev.app”作为域名时,我可以直接进入我的Laravel安装的公共文件夹。
Laravel位于F:/xampp/htdocs/dev/public。
我打开了位于F:\xamp\apache\conf\extra\https-vhosts.conf的httpd-vhosts.conf文件,并用以下内容替换了所有内容。
# 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 localhost>
    DocumentRoot "F:/xampp/htdocs/"
    ServerAdmin admin@localhost

    <Directory "F:/xampp/htdocs/">
        Options Indexes FollowSymLinks
        AllowOverride all
    </Directory>

</VirtualHost>

# Development
<VirtualHost dev.app>
    DocumentRoot "F:/xampp/htdocs/dev/public"
    ServerAdmin admin@localhost

    <Directory "F:/xampp/htdocs/dev/public">
       AllowOverride All
       Order Allow,Deny
       Allow from all
       Require all granted
    </Directory>
</VirtualHost>

然后我打开了位于C:\ Windows \ System32 \ drivers \ etc的hosts文件,并将localhost行更改为以下内容

127.0.0.1       localhost      dev.app
127.0.0.1       127.0.0.1

然而,当我在浏览器中打开dev.app时,出现了以下错误。

Unable to connect

Firefox can't establish a connection to the server at app.dev.

The site could be temporarily unavailable or too busy. Try again in a few moments.
If you are unable to load any pages, check your computer's network connection.
If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

我在这里错过了什么?

我做错了什么?

注意:在更改虚拟主机文件后,我重启了Apache。另外,我更新了Laravel配置文件夹中的app.php文件,使url值为http://dev.app

已更新:在添加了http://...后,网站能够被访问,但图片无法显示。


1
我注意到有时候你需要在本地主机上访问 http://... 才能使其正常工作。另外,在更新任何 Apache 配置文件后,确保重新启动 Web 服务器以使更改生效。 - user1669496
http:// 是关键 :) 谢谢。但现在图片无法正确加载。我只看到没有图片的网站。 - Junior
2个回答

20

hosts文件应该像这样,这样它才能在IPv4和IPv6网络上被找到

127.0.0.1  localhost dev.app
::1        localhost dev.app

如果您正在使用Apache 2.4.x,则在httpd-vhosts.conf文件中添加以下行:

NameVirtualHost *:80

Apache 2.4不再需要或允许使用此功能。

vhost文件应该像这样,您混合了Apache 2.2和2.4语法,虽然只要您已激活mod_access_compat,任何一种语法都可以,但您不应该混用它们,而且2.4语法更好。您还错过了一些其他有用的部分。

<VirtualHost *:80>
    DocumentRoot "F:/xampp/htdocs/"
    ServerAdmin admin@localhost
    ServerName localhost

    <Directory "F:/xampp/htdocs/">
       Options Indexes FollowSymLinks
       AllowOverride all
       Require local
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "F:/xampp/htdocs/dev/public"
    ServerAdmin admin@localhost
    ServerName dev.app
    ServerAlias www.dev.app

    <Directory "F:/xampp/htdocs/dev/public">
       AllowOverride All
       Options Indexes FollowSymLinks
       
       Require local
       # if you want access from other pc's on your local network
       #Require ip 192.168.1
       # Only if you want the world to see your site
       #Require all granted
    </Directory>
</VirtualHost>

这对我有用。我有Apache 2.4.x,我的“https-vhosts.conf”文件是使用Apache 2.2.x格式的。谢谢。 - Pedro Loureiro
1
httpd-vhosts.conf是文件的名称@PedroLoureiro - saber tabatabaee yazdi
@sabertabatabaeeyazdi 在答案中已经提到了文件名称,我只是加粗了一下,这样不容易被忽略。 - RiggsFolly
似乎从Chrome 63开始,所有包含.app的URL都会重定向到https。我找不到Google针对.app域名的官方帖子。只有关于.dev域名的文档 https://ma.ttias.be/chrome-force-dev-domains-https-via-preloaded-hsts/ 我将mydomain.app替换为mydomain.test,这个答案对我有效。 - Luis Rodriguez
@Sagarppanchal 这是对一个基于WINDOWS的问题的回答。因此,它没有sudo,因为它不是ubuntu!更改答案时请小心,首先阅读上下文。 - RiggsFolly
@RiggsFolly 抱歉!以后会注意的! - SagarPPanchal

0
open C:\xampp\apache\conf\httpd.conf, locate at
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf, remove # in this line,
save file,
Restart server

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接