0
点赞
收藏
分享

微信扫一扫

wamp配置外部访问权限

爱情锦囊 2022-11-27 阅读 78


参考自  ​​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配置外部访问权限_html


新安装的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文件:


1. <VirtualHost *:80>  
2. ServerName localhost
3. DocumentRoot c:/wamp64/www
4. <Directory "c:/wamp64/www/">
5. Options +Indexes +Includes +FollowSymLinks +MultiViews
6. AllowOverride All
7. Require local
8. </Directory>
9. </VirtualHost>

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

1. <VirtualHost *:80>  
2. ServerName localhost
3. DocumentRoot c:/wamp64/www
4. <Directory "c:/wamp64/www/">
5. Options +Indexes +Includes +FollowSymLinks +MultiViews
6. AllowOverride All
7. Require local
8. </Directory>
9.
10. <Directory "c:/wamp64/www/test">
11. Options +Indexes +Includes +FollowSymLinks +MultiViews
12. AllowOverride All
13. Require all granted
14. </Directory>
15. </VirtualHost>

这样配置后,其他主机可以通过http://IP/test来访问www/test目录,同时原www的访问权限不变。

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

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

wamp配置外部访问权限_html_02


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


1. <VirtualHost *:80>  
2. ServerName localhost
3. DocumentRoot c:/wamp64/www
4. <Directory "c:/wamp64/www/">
5. Options +Indexes +Includes +FollowSymLinks +MultiViews
6. AllowOverride All
7. Require local
8. </Directory>
9.
10. <Directory "c:/wamp64/www/test">
11. Options +Indexes +Includes +FollowSymLinks +MultiViews
12. AllowOverride All
13. Require all granted
14. </Directory>
15. </VirtualHost>
16.
17. <VirtualHost *:80>
18. ServerName test2
19. DocumentRoot d:/test2
20. <Directory "d:/test2">
21. Options +Indexes +Includes +FollowSymLinks +MultiViews
22. AllowOverride All
23. Require all granted
24. </Directory>
25. </VirtualHost>

主要注意几个配置:ServerName设置访问的域名,DocumentRoot设置目录地址,Require设置权限。

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

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

wamp配置外部访问权限_html_03


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



1. listen 10180  
2. <VirtualHost *:10180>
3. ServerName test2
4. DocumentRoot d:/test2
5. <Directory "d:/test2">
6. Options +Indexes +Includes +FollowSymLinks +MultiViews
7. AllowOverride All
8. Require all granted
9. </Directory>
10. </VirtualHost>

这样通过http://ip:10180就可以访问test2,也可以通过http://localhost:10180访问,因为localhost被映射到本机IP。

举报

相关推荐

0 条评论