如何在 Xampp 上为 Laravel 启用虚拟主机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31951931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:05:29  来源:igfitidea点击:

How to enable Virtual Host on Xampp for Laravel?

apachelaravelxampphttpd.confvhosts

提问by Junior

I have XAMPP running on Windows 7 Pro. I am trying to setup a Virtual Host so that when I use "dev.app" as a domain I get directly to my public folder of laravel installation.

我在 Windows 7 Pro 上运行 XAMPP。我正在尝试设置一个虚拟主机,以便当我使用“dev.app”作为域时,我可以直接进入 laravel 安装的公共文件夹。

Laravel is located at F:/xampp/htdocs/dev/public

Laravel 位于 F:/xampp/htdocs/dev/public

I opened the httpd-vhosts.conffile located at F:\xamp\apache\conf\extra\https-vhosts.conf

我打开了httpd-vhosts.conf位于F:\xamp\apache\conf\extra\https-vhosts.conf

and replaced everything with this

并用这个替换了所有内容

# 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>

then I opened my hosts file located at C:\Windows\System32\drivers\etcand added changed the localhost line to look like this

然后我打开了位于 的主机文件C:\Windows\System32\drivers\etc并添加了更改 localhost 行看起来像这样

127.0.0.1       localhost      dev.app
127.0.0.1       127.0.0.1

However, when I go to dev.app in my browser I get this error

但是,当我在浏览器中访问 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.

无法连接

Firefox 无法与 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.

What am I missing here? What did I do wrong?

我在这里缺少什么?我做错了什么?

Note:I restarted Apache after I change the vhosts file. Also, I updated the app.php file in the config folder of laravel to have http://dev.appvalue in the url.

注意:我在更改 vhosts 文件后重新启动了 Apache。此外,我更新了 laravel 配置文件夹中的 app.php 文件,使其http://dev.app在 url 中具有值。

UPDATEDafter adding http://.... the site resolve but the images are not showing.

添加 http://.. 后更新。网站解析但图像未显示。

回答by RiggsFolly

The hosts file should look like this so that it can be found on the IPV4 and IPV6 networks

主机文件应如下所示,以便可以在 IPV4 和 IPV6 网络上找到它

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

If you are using Apache 2.4.x this line in httpd-vhosts.conf

如果您使用的是 Apache 2.4.x,请在 httpd-vhosts.conf 中添加这一行

NameVirtualHost *:80

is no longer required or allowed for Apache 2.4.

Apache 2.4 不再需要或允许。

The vhost file should look like this, you mixed Apache 2.2 and 2.4 syntax and while either is allowed as long as you have mod_access_compatactivated, you should not mix them and the 2.4 syntax is better. You also missed a few other useful bits and pieces

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>

回答by Murad

Use laragon server instead of XAMPP. One of useful features of Laragon is Auto Virutal Hosts. learn more about Pretty URLs with laragon here

使用 laragon 服务器而不是 XAMPP。Laragon 的一项有用功能是 Auto Virutal Hosts。在此处了解有关 laragon 的漂亮 URL 的更多信息