php - WAMP 虚拟主机不工作

标签 php mysql apache wamp virtualhost

我使用的是 wamp 2.5 版 我的 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

我尝试使用此 URL 访问我的项目:http://www.mysite.dev/ 但是我收到一个未找到服务器错误 我尝试使用 www.mysite.devhttp://mysite.dev 访问它,但仍然运气不佳!

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

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

谢谢!

最佳答案

首先,您需要从 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限制访问,以便只有运行 WAMPServer(即 Apache)的 PC 才能连接到这些站点中的任何一个。

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

Order Allow,Deny
Allow from all

Require all granted

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

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

只允许一台其他 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    

它可能会在短期内解决您的问题,但只是在等您决定要向 friend /客户/老板展示您的网站时再捕获您。如果您进入端口转发阶段,您的路由器允许世界进入您的网络,这将导致您的所有站点对世界可用。

最好更改您希望人们从 Require local 看到的用于测试/吹嘘的 ONE 站点的 ONE 虚拟主机定义。至 Require all granted并且只允许从 Internet 访问该单个站点。

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

另外,如果您更改主机文件以使 chnages 处于事件状态,则您应该重新启动或从命令窗口的命令行运行这些命令 ising 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.

ipconfig /flushdns

关于php - WAMP 虚拟主机不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26113258/

相关文章:

php - MySQL 准备语句与普通查询。 yield 和损失

php - 打开两个连接到我的 Apache 服务器?

java - org.springframework.jdbc.CannotGetJdbcConnectionException : java. sql.SQLException: 找不到适合 jdbc:mysql 的驱动程序

mysql - 如何使用 Ruby On Rails 编写 MySQL 多行条件?

django - Apache mod_wsgi django调用keras模型时如何释放占用的GPU内存?

php - .htaccess 表示 PDF URL

php - 当我从数据库加载法语文本时,数据表返回无效的 json 响应

php - 循环返回意外结果

php - 构建技能mysql db

php glob 函数不返回任何内容