WAMP虚拟主机无法正常工作

时间:2014-09-30 05:17:49

标签: php mysql apache wamp virtualhost

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

我有这些配置:

在我的httpd.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.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 webmaster@dummy-host.example.com
    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 webmaster@dummy-host2.example.com
    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 webmaster@dummy-host.example.com
    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>

在我的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

我尝试使用以下网址访问我的项目:http://www.mysite.dev/ 但是我收到Server not found error我尝试使用www.mysite.dev http://mysite.dev访问它但是运气不好!

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

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

谢谢!

6 个答案:

答案 0 :(得分:16)

首先,您需要从vhost-httpd.conf文件中删除示例 dummy 定义。它们仅作为示例仅用于开始使用语法,并且不应保留在活动conf/extra/httpd-vhosts.conf中,因为它们指向不存在的文件夹。

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

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    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 webmaster@dummy-host2.example.com
    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>

第二个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

现在,在系统上实际存在的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>

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

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

Order Allow,Deny
Allow from all

Require all granted

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

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

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

Require local
Require ip 192.168.1.100

或其他2个PC

Require local
Require ip 192.168.1.100, 192.168.1.101

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

Require ip 192.168.1

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

Require all granted  <--Apache 2.4 syntax

or 

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

它可以在短期内解决您的问题,但只是在您决定要向朋友/客户/老板展示您的网站时等待。如果您进入Port Forwarding的路由器,那么路由器将允许您的网络进入您的网络,这将导致所有您的网站向全世界提供。

最好更改您希望人们从Require localRequire all granted进行测试/吹牛的ONE网站的ONE Virtual Host Definition,并且只允许从互联网访问该网站。< / p>

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

此外,如果您更改hosts文件以使chnages处于活动状态,则应该从命令窗口的命令行重启或运行这些命令,这些命令是Runs as Administrator选项启动的。

net stop dnscache
net start dnscache

答案 1 :(得分:5)

由于Google收购了.dev gTLD,因此不再容易使用.dev开发网站,最好的方法是将您的开发域名重命名为.local或您喜欢的内容。

后台发生的情况是本地DNS服务器将浏览器重定向到127.0.53.53(打开cmd&gt; nslookup yourdomain.dev),以便通知最终用户正在获取.dev gTLD。由于hosts文件中的.dev域设置为127.0.0.1,因此显示连接被拒绝。

您可以在hosts文件中将127.0.0.1更改为127.0.53.53,并查看浏览器错误从ERR_CONNECTION_REFUSED更改为ERR_ICANN_NAME_COLLISION。

答案 2 :(得分:2)

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

取消注释以下行:

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

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

答案 3 :(得分:0)

在httpd.conf

中查看此模块未注释
  • proxy_module
  • proxy_http_module

答案 4 :(得分:0)

对于这个问题,我来得太晚了,我做了@RiggsFolly提到的所有事情,但是我改变了一件事情,使它可以立即工作。我将ERROR更改为WARNING,因为保留了.dev。希望对您有帮助

答案 5 :(得分:-1)

在你的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>

重新启动你的wamp服务器并在你的浏览器上输入类似mysite.dev/的url。 希望它会对你有所帮助。 谢谢。

相关问题