PHP中级篇 Apache配置httpd-vhosts虚拟主机总结及注意事项[OK]

it2025-04-11  6

经常使用Apache虚拟主机进行开发和测试,但每次需要配置虚拟主机时都习惯性的ctrl+c和ctrl+v,这次由于重装系统,需要配置一个新的PHP开发环境虚拟主机,于是总结一下Apaceh配置httpd-vhosts虚拟主机使用方法和步骤,便于查找和使用。

开发环境:WAMP网址:http://www.wampserver.com/en/

实例一,Apaceh配置localhost虚拟主机步骤1,用记事本打开apache目录下httpd.conf文件(如:D:\wamp\bin\apache\apache2.2.8\httpd.conf),找到如下模块

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

去掉前面的#,这样就开启了httpd-vhosts虚拟主机文件。这时候重启wamp环境,无法打开localhost,需要在httpd-vhosts.conf配置一下。

2,用记事本打开httpd-vhosts文件,配置好localhost虚拟主机,参照httpd-vhosts文件中实例,修改成如下:

<VirtualHost *:80>ServerAdmin webmaster@dummy-host.localhostDocumentRoot "D:\wamp\www"ServerName localhostServerAlias localhostErrorLog "logs/dummy-host.localhost-error.log"CustomLog "logs/dummy-host.localhost-access.log" common</VirtualHost>

修改配置如下:DocumentRoot 修改为本地wamp环境下的www目录(如:D:\wamp\www)ServerName改为localhost

3,重启Apache,发现localhost可以正常打开,配置localhost比较简单。

实例二,Apaceh配置test.biuuu.com虚拟主机步骤

1,方法同上,复制配置代码修改如下:

<VirtualHost *:80>ServerAdmin test@biuuu.comDocumentRoot E:\WebRoot\biuuuServerName test.biuuu.comErrorLog "logs/dummy-host2.localhost-error.log"CustomLog "logs/dummy-host2.localhost-access.log" common</VirtualHost>

2,打开host文件(C:\WINDOWS\system32\drivers\etc\hosts),增加一行代码

127.0.0.1       test.biuuu.com

3,在浏览器中打开test.biuuu.com,发现如下错误403 Forbidden错误Forbidden You don't have permission to access / on this server.

分析:这主要是目录访问权限没有设置,需要设置对目录的访问权!

4,打开httpd文件,找到如下语句

<Directory />Options FollowSymLinksAllowOverride NoneOrder deny,allowDeny from all</Directory>

复制以上代码,并进行目录修改,把/替换为E:\WebRoot\biuuu,修改virtualHost代码如下

<VirtualHost *:80>ServerAdmin test@biuuu.comDocumentRoot E:\WebRoot\biuuuServerName test.biuuu.comErrorLog "logs/dummy-host2.localhost-error.log"CustomLog "logs/dummy-host2.localhost-access.log" common <Directory E:\WebRoot\biuuu>Options FollowSymLinksAllowOverride NoneOrder deny,allowDeny from all</Directory> </VirtualHost>

在浏览器中测试发现还是打不开,提示如上403 Forbidden错误,修改其中的Deny from all为allow from all

5,重启Apache,虚拟主机配置成功!

注意事项1,目录路径,如E:\WebRoot\biuuu2,访问权限,如上Deny from all修改为allow from all3,host文件,配置虚拟域名host指向4,httpd文件,打开Include conf/extra/httpd-vhosts.conf模块5,httpd-vhosts文件,配置虚拟主机

6, 还有可能是linux 的 selinux 防火墙导致这个原因,切记切记

使用Apaceh配置httpd-vhosts虚拟主机对于开发人员来说比较简单,但却非常重要,仅供参考!

参考资料:

http://httpd.apache.org/docs/2.2/vhosts/

http://httpd.apache.org/docs/2.0/vhosts/examples.html

原创文章如转载,请注明:转载自 必优博客 http://www.biuuu.com/

顾银鑫 注:如发生Fatal error: Allowed memory size of 8388608 bytes exhausted错误

修改php.ini设置memory_limit = 12M(默认8M)

或只需要在你的程序头部加入: ini_set("memory_limit","12M");

 

 

找到httpd.conf,用记事本打开httpd.conf,然后将

 

<Directory />    Options FollowSymLinks    AllowOverride None    Order deny,allow    Deny from all</Directory>

 

这里改成:

 

<Directory />    Options FollowSymLinks    AllowOverride None    Order deny,allow    Allow from all</Directory>

 

 还有一处将下面

 

# onlineoffline tag - don't removeOrder Deny,AllowDeny from all  Allow from 127.0.0.1</Directory>

 

将Deny from all  改为:Allow from all  ,然后重新启动所有服务。

 

转载于:https://www.cnblogs.com/vania/p/4354327.html

最新回复(0)