wamp配置project目录和外部访问权限

来源:互联网 发布:c语言如何打开doc文件 编辑:程序博客网 时间:2024/06/08 01:36

参考自  Wamp server 403 forbidden using a public ip (works with localhost and ipv4 address)

why wamp server put online/ offline option is missing?


注:wamp及apache、mysql、php版本如下



新安装的wamp,本机通过127.0.0.1和localhost都可以正常访问,但是局域网内其他主机通过IP访问会提示403 forbidden。

一些解决方案是修改apache的httpd.conf文件,将www目录的 Require local 修改为 Require all granted,在旧版本上这样做应该可以。

但是自wamp 3开始,localhost被定义为virtual host,我们需要修改httpd-vhosts.conf文件来修改相关的行为。具体修改如下。


原始的httpd-vhosts.conf文件:

<VirtualHost *:80>ServerName localhostDocumentRoot c:/wamp64/www<Directory  "c:/wamp64/www/">Options +Indexes +Includes +FollowSymLinks +MultiViewsAllowOverride AllRequire local</Directory></VirtualHost>


修改后的httpd-vhosts.conf文件:

<VirtualHost *:80>ServerName localhostDocumentRoot c:/wamp64/www<Directory  "c:/wamp64/www/">Options +Indexes +Includes +FollowSymLinks +MultiViewsAllowOverride AllRequire local</Directory><Directory  "c:/wamp64/www/test">Options +Indexes +Includes +FollowSymLinks +MultiViewsAllowOverride AllRequire all granted</Directory></VirtualHost>
这样配置后,其他主机可以通过http://IP/test来访问www/test目录,同时原www的访问权限不变。

Require local确保只有本机可以访问,Require all granted的是所有主机均可访问。

这样添加后的test目录,在apache下被认为是一个project:



而更灵活的一种方式,是新建立一个virtual host:

<VirtualHost *:80>ServerName localhostDocumentRoot c:/wamp64/www<Directory  "c:/wamp64/www/">Options +Indexes +Includes +FollowSymLinks +MultiViewsAllowOverride AllRequire local</Directory><Directory  "c:/wamp64/www/test">Options +Indexes +Includes +FollowSymLinks +MultiViewsAllowOverride AllRequire all granted</Directory></VirtualHost><VirtualHost *:80>ServerName test2DocumentRoot d:/test2<Directory  "d:/test2">Options +Indexes +Includes +FollowSymLinks +MultiViewsAllowOverride AllRequire all granted</Directory></VirtualHost>
主要注意几个配置:ServerName设置访问的域名,DocumentRoot设置目录地址,Require设置权限。

访问test2时,就可以像访问localhost一样,直接http://test2访问。这里需要注意,在hosts文件中加入test2到本机IP的映射,否则直接通过http://test2是无法访问的。

这样配置之后,test2就会出现在virtual host下面,与localhost是同等级的:



另外需要注意一点,因为localhost和test2都指向了*:80,所以当直接访问http://IP的时候,按照顺序从上向下匹配,首先匹配到的是localhost。所以这里如果通过http://ip来访问test2是访问不到的,这时候可以通过修改端口号来实现:

listen 10180<VirtualHost *:10180>ServerName test2DocumentRoot d:/test2<Directory  "d:/test2">Options +Indexes +Includes +FollowSymLinks +MultiViewsAllowOverride AllRequire all granted</Directory></VirtualHost>
这样通过http://ip:10180就可以访问test2,也可以通过http://localhost:10180访问,因为localhost被映射到本机IP。

1 0