WAMP 虚拟主机不工作 [英] WAMP Virtual Host not working

查看:43
本文介绍了WAMP 虚拟主机不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有这些配置:

在我的 httpd.conf

# 虚拟主机包括 conf/extra/httpd-vhosts.conf

在我的G:\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf

# 个虚拟主机## 所需模块:mod_log_config# 如果你想在你的计算机上维护多个域/主机名# machine 你可以为它们设置 VirtualHost 容器.大多数配置# 只使用基于名称的虚拟主机,所以服务器不需要担心#IP地址.这由以下指令中的星号指示.## 请参阅文档在# <URL:http://httpd.apache.org/docs/2.4/vhosts/># 在您尝试设置虚拟主机之前了解更多详细信息.## 您可以使用命令行选项-S"来验证您的虚拟主机# 配置.### 虚拟主机示例:# 几乎任何 Apache 指令都可以进入 VirtualHost 容器.# 第一个 VirtualHost 部分用于所有不使用的请求# 匹配任何 <VirtualHost> 中的 ServerName 或 ServerAlias;堵塞.#<虚拟主机 *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot "g:/Apache24/docs/dummy-host.example.com"服务器名称 dummy-host.example.comServerAlias www.dummy-host.example.com错误日志日志/虚拟主机.example.com-error.log"CustomLog "logs/dummy-host.example.com-access.log" common</虚拟主机><虚拟主机 *:80>ServerAdmin webmaster@dummy-host2.example.comDocumentRoot "g:/Apache24/docs/dummy-host2.example.com"服务器名称 dummy-host2.example.com错误日志日志/虚拟主机2.example.com-error.log"CustomLog "logs/dummy-host2.example.com-access.log" common</虚拟主机><虚拟主机 *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot "g:/wamp/www"服务器名称本地主机错误日志日志/本地主机-error.log"CustomLog "logs/localhost-access.log" 通用</虚拟主机><虚拟主机 *:80>DocumentRoot "g:\wamp\www\mysite\public"服务器名称 mysite.dev</虚拟主机>

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

# localhost 名称解析在 DNS 本身内处理.127.0.0.1 本地主机127.0.0.1 mysite.dev# ::1 本地主机

我尝试使用以下 URL 访问我的项目:http://www.mysite.dev/ 但是我收到一个 Server not found 错误 我尝试使用 www.mysite.dev , http://mysite.dev 访问它,但仍然运气不好!

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

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

谢谢!

解决方案

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

因此从文件中删除这两个定义:

ServerAdmin webmaster@dummy-host.example.comDocumentRoot "g:/Apache24/docs/dummy-host.example.com"服务器名称 dummy-host.example.comServerAlias www.dummy-host.example.com错误日志日志/虚拟主机.example.com-error.log"CustomLog "logs/dummy-host.example.com-access.log" common</虚拟主机><虚拟主机 *:80>ServerAdmin webmaster@dummy-host2.example.comDocumentRoot "g:/Apache24/docs/dummy-host2.example.com"服务器名称 dummy-host2.example.com错误日志日志/虚拟主机2.example.com-error.log"CustomLog "logs/dummy-host2.example.com-access.log" common</虚拟主机>

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

127.0.0.1 本地主机::1 本地主机127.0.0.1 mysite.dev::1 mysite.dev

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

DocumentRoot "g:/wamp/www"服务器名称本地主机服务器别名本地主机错误日志日志/本地主机-error.log"CustomLog "logs/localhost-access.log" 通用<目录G:/wamp/www">允许覆盖所有期权索引 FollowSymLinks需要本地</目录></虚拟主机><虚拟主机 *:80>DocumentRoot "g:\wamp\www\mysite\public"服务器名称 mysite.devServerAlias www.mysite.dev错误日志日志/mysite-error.log"CustomLog "logs/mysite-access.log" 常见<目录G:/wamp/www/mysite/public">允许覆盖所有期权索引 FollowSymLinks需要本地</目录></虚拟主机>

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

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

订单允许,拒绝所有人都允许

要求所有授予

在相同的定义中.您使用的是 Apache 2.4,因此请使用 Apache 2.4 语法.

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

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

需要本地需要 ip 192.168.1.100

或其他 2 台电脑

需要本地需要 ip 192.168.1.100, 192.168.1.101

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

需要ip 192.168.1

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

要求所有授予的 <--Apache 2.4 语法或者Order Allow,Deny <-- Apache 2.2 语法所有人都允许

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

最好将您希望人们在测试/吹嘘中看到的 ONE 站点的 ONE Virtual Host Definition 从 Require local 更改为 Require all allowed 并且只允许单个可从 Internet 访问的站点.

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

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

net stop dnscache网络启动 dnscache

<块引用>

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

ipconfig/flushdns

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

I have these configurations:

On my httpd.conf

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

On my 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>

On my 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/ BUT I am getting a Server not found error I tried accessing it using www.mysite.dev , http://mysite.dev but still having a bad luck!

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!

解决方案

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

So remove these 2 definitions from the file:

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

Second Apache 2.4.x is IPV4 ( 127.0.0.1 ) and IPV6 (::1) aware so your hosts file 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.

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 :

<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 local limits access so that only the PC running WAMPServer i.e. Apache can connect to any of these site.

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

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.

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.

Allow just a single other PC into your site

Require local
Require ip 192.168.1.100

or 2 other PC's

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.

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 SITES to 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 local to Require all granted and only allow that single site to be access from the internet.

Once you have made all these changes remember to restart 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 Administrator option.

net stop dnscache
net start dnscache

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

ipconfig /flushdns

这篇关于WAMP 虚拟主机不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆