php WAMP 虚拟主机不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26113258/
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-08-25 18:09:56  来源:igfitidea点击:

WAMP Virtual Host not working

phpmysqlapachewampvirtualhost

提问by Jason M

I am using a wamp version 2.5 My Apache is 2.4.9 PHP: 5.5.12 MySQL: 5.6.17

我使用的是 wamp 版本 2.5 我的 Apache 是 2.4.9 PHP:5.5.12 MySQL:5.6.17

I have these configurations:

我有这些配置:

On my httpd.conf

在我的 httpd.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

On my G:\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf

在我的 G:\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.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.
#


#
# 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 [email protected]
    DocumentRoot "g:/Apache24/docs/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 [email protected]
    DocumentRoot "g:/Apache24/docs/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>
    ServerAdmin [email protected]
    DocumentRoot "g:/wamp/www"
    ServerName localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "g:\wamp\www\mysite\public"
    ServerName mysite.dev
</VirtualHost>

On my c:\Windows\System32\Drivers\etc\hosts

在我的 c:\Windows\System32\Drivers\etc\hosts

# localhost name resolution is handled within DNS itself.
127.0.0.1   localhost
127.0.0.1   mysite.dev
#   ::1     localhost

I try to access my project using this URL: http://www.mysite.dev/BUTI am getting a Server not found errorI tried accessing it using www.mysite.dev, http://mysite.devbut still having a bad luck!

我尝试使用这个URL来访问我的项目:http://www.mysite.dev/我得到一个Server not found error我试图使用它访问www.mysite.devhttp://mysite.dev但仍然有一个糟糕的运气!

My virtual host was working before but i'm not sure why it wasn't working now. Some weird stuff going on.

我的虚拟主机以前可以工作,但我不确定为什么现在不能工作。发生了一些奇怪的事情。

I am not sure what's happening. Any ideas will be greatly appreciated!

我不确定发生了什么。任何想法将不胜感激!

Thanks!

谢谢!

回答by RiggsFolly

First you need to remove the example dummydefinitions from your vhost-httpd.conf file. They are there as examples onlyjust to get you started with the syntax, and should not remain in an active conf/extra/httpd-vhosts.confas they are pointing to non existant folders.

首先,您需要从 vhost-httpd.conf 文件中删除示例虚拟定义。它们作为示例只是为了让您开始使用语法,不应保持活动状态,conf/extra/httpd-vhosts.conf因为它们指向不存在的文件夹。

So remove these 2 definitions from the file:

所以从文件中删除这两个定义:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "g:/Apache24/docs/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 [email protected]
    DocumentRoot "g:/Apache24/docs/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>

Second Apache 2.4.x is IPV4 ( 127.0.0.1 ) and IPV6 (::1) aware so your hostsfile should look like this with definitions for both IPV4 and IPV6 versions for each site. The browser can arbitrarily use either so you need both but will probably use the IPV6 network in preference to the IPV4 if both are actually active on your PC.

第二个 Apache 2.4.x 支持 IPV4 ( 127.0.0.1 ) 和 IPV6 (::1),因此您的hosts文件应如下所示,其中包含每个站点的 IPV4 和 IPV6 版本的定义。浏览器可以任意使用其中一个,因此您需要两者,但如果两者实际上都在您的 PC 上处于活动状态,则可能会优先使用 IPV6 网络而不是 IPV4。

127.0.0.1   localhost
::1  localhost

127.0.0.1   mysite.dev
::1  mysite.dev

Now on the 2 Virtual Hosts that actually exist on your system try this as the Virtual Host definition :

现在,在您系统上实际存在的 2 个虚拟主机上,尝试将其作为虚拟主机定义:

<VirtualHost *:80>
    DocumentRoot "g:/wamp/www"
    ServerName localhost
    ServerAlias localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
    <Directory  "G:/wamp/www">
        AllowOverride All
        Options Indexes FollowSymLinks
        Require local
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "g:\wamp\www\mysite\public"
    ServerName mysite.dev
    ServerAlias www.mysite.dev
    ErrorLog "logs/mysite-error.log"
    CustomLog "logs/mysite-access.log" common
    <Directory  "G:/wamp/www/mysite/public">
        AllowOverride All
        Options Indexes FollowSymLinks
        Require local
    </Directory>
</VirtualHost>

The <Directory>....</Directory>section within the <VirtualHost>....</VirtualHost>section tells Apache which IP Addresses it is allowed to accept connections from, so using the Apache 2.4 syntax Require locallimits access so that only the PC running WAMPServer i.e. Apache can connect to any of these site.

<Directory>....</Directory>部分中的<VirtualHost>....</VirtualHost>部分告诉 Apache 允许接受来自哪些 IP 地址的连接,因此使用 Apache 2.4 语法Require local限制访问,以便只有运行 WAMPServer 的 PC(即 Apache)可以连接到这些站点中的任何一个。

Avoid mixing Apache 2.2 syntax and Apache 2.4 syntax together in the same definition. So dont use

避免在同一定义中将 Apache 2.2 语法和 Apache 2.4 语法混合在一起。所以不要使用

Order Allow,Deny
Allow from all

and

Require all granted

in the same definition. You are using Apache 2.4 so use the Apache 2.4 syntax.

在同一个定义中。您使用的是 Apache 2.4,因此请使用 Apache 2.4 语法。

If you find you want to allow other PC's inside your local network to see you site i.e. work mate or the kids etc, you can add this syntax to one or more of your Virtual Host definition.

如果您发现要允许本地网络中的其他 PC 看到您的站点,即同事或孩子等,您可以将此语法添加到一个或多个虚拟主机定义中。

Allow just a single other PC into your site

只允许一台其他 PC 进入您的站点

Require local
Require ip 192.168.1.100

or 2 other PC's

或 2 台其他 PC

Require local
Require ip 192.168.1.100, 192.168.1.101

Or to anyone on your local network just use the first 3 of the 4 quartiles of the ip address.

或者对于本地网络上的任何人,只需使用 IP 地址的 4 个四分位数中的前 3 个。

Require ip 192.168.1

Also avoid using the syntax that allows access from anywhere i.e.

还要避免使用允许从任何地方访问的语法,即

Require all granted  <--Apache 2.4 syntax

or 

Order Allow,Deny     <-- Apache 2.2 syntax
Allow from all    

It may solve your issues in the short term, but is just waiting to catch you sometime later when you decide you want to show your site to a friend/client/boss. If you get to the stage of Port Forwarding you router so that the world is allowed into your network that would cause ALL OF YOUR SITESto become available to the world.

它可能会在短期内解决您的问题,但只是在您决定要向朋友/客户/老板展示您的网站时等待稍后赶上您。如果您进入端口转发阶段,您的路由器将允许世界进入您的网络,从而使您的所有站点都可以使用。

Better to change the ONE Virtual Host Definition for the ONE site you want people to see for testing/bragging from Require localto Require all grantedand only allow that single site to be access from the internet.

最好更改您希望人们看到的 ONE 站点的 ONE Virtual Host Definition 以进行测试/吹嘘Require localRequire all granted并且只允许从 Internet 访问该单个站点。

Once you have made all these changes remember to restart Apache.

完成所有这些更改后,请记住重新启动 Apache。

Also if you change the hosts file to make the chnages active you should either reboot or run these command from the command line of a command windows started ising the Runs as Administratoroption.

此外,如果您更改主机文件以激活 chnages,您应该重新启动或从命令窗口的命令行运行这些命令Runs as Administrator

net stop dnscache
net start dnscache

If you are using Windows 10 the above dns commands no longer work, you should do this instead.

如果您使用的是 Windows 10,上述 dns 命令不再有效,您应该改为执行此操作。

ipconfig /flushdns

回答by Maziyar Mk

Due to Google acquiring .dev gTLD, having .dev development sites is no longer easily possible, best way to mitigate is to just rename your development domain into .local or something that you prefer.

由于 Google 收购了 .dev gTLD,因此拥有 .dev 开发站点不再容易,最好的缓解方法是将您的开发域重命名为 .local 或您喜欢的名称。

What happens in the background is that the local DNS server redirects the browser to 127.0.53.53 (open cmd > nslookup yourdomain.dev) in order to inform end-users of .dev gTLD being acquired. Since the .dev domain in hosts file is set to 127.0.0.1 it shows connection refused.

后台发生的事情是本地 DNS 服务器将浏览器重定向到 127.0.53.53(打开 cmd > nslookup yourdomain.dev),以通知最终用户 .dev gTLD 被获取。由于主机文件中的 .dev 域设置为 127.0.0.1,它显示连接被拒绝。

You can change 127.0.0.1 to 127.0.53.53 in the hosts file and see that the browser error changes from ERR_CONNECTION_REFUSED to ERR_ICANN_NAME_COLLISION.

可以将hosts文件中的127.0.0.1改为127.0.53.53,看到浏览器错误从ERR_CONNECTION_REFUSED改为ERR_ICANN_NAME_COLLISION。

回答by HichemSeeSharp

I am coming very late to this question, I did all what was mentioned by @RiggsFolly but one thing I changed made it to work instantly. I changed .devto .testas .dev is reserved. hope this helps

我对这个问题来的很晚,我做了@RiggsFolly 提到的所有事情,但我改变的一件事使它立即起作用。我换.dev.test为.dev被保留。希望这可以帮助

回答by Mahesh Hegde

Following is working for me

以下对我有用

<VirtualHost *:80>
    DocumentRoot "G:\project\test.dev"
    ServerAdmin [email protected]
    ServerName test.dev
    ErrorLog "logs/test.dev-error.log"
    CustomLog "logs/test.dev-access.log" common
    <Directory "G:\project\test.dev">
       AllowOverride All
       Options Indexes FollowSymLinks
       Require local
    </Directory>
</VirtualHost>

回答by D. Chatzimichailidis

I fix the same problem by uncomment some lines in httpd.conf in Apache folder.

我通过取消注释 Apache 文件夹中 httpd.conf 中的一些行来解决同样的问题。

Uncomment lines below:

取消注释以下行:

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

Save file and Restart your Apache and it will work. Many thanks to this guy: https://john-dugan.com/wamp-vhost-setup/

保存文件并重新启动您的 Apache,它将起作用。非常感谢这个人:https: //john-dugan.com/wamp-vhost-setup/

回答by Kiryl Plyashkevich

Check out this modules uncommented in httpd.conf

查看 httpd.conf 中未注释的模块

  • proxy_module
  • proxy_http_module
  • 代理模块
  • proxy_http_module

回答by Jangya satapathy

try this on you apache httpd.config file:

在你的 apache httpd.config 文件上试试这个:

    <VirtualHost *:80>
      ServerName mysite.dev
      DocumentRoot "g:\wamp\www\mysite\public"
     SetEnv APPLICATION_ENV "development"

     <Directory "g:\wamp\www\mysite\public">
        DirectoryIndex index.php
        Options All Includes Indexes
        Options All Indexes FollowSymLinks 
        Order allow,deny
        Allow from all
        Require all granted
     </Directory>
  </VirtualHost>

restart your wamp server and put url like mysite.dev/ on you browser. hope it will help you. thank you.

重新启动您的 wamp 服务器并将像 mysite.dev/ 这样的 url 放在您的浏览器上。希望它会帮助你。谢谢你。