配置php虚拟目录
----------
摘要
----------
本文极为简要的介绍了如何在windows平台下安装apache2.2.x并配置虚拟目录. 以作为快速安装的参考. 详细的设置最好的文档是官方的说明文档见下方的链接, 在安装配置的时候仔细的查阅文档是很好的解决办法.
1. apache http server version 2.2 英文文档
2. apache http server version 2.2 中文文档
----------
准备条件
----------
到 http://httpd.apache.org/ 下载相应版本的apache. (注意: 2.0与2.2版本的apache对应的module版本页不同, 互相不兼容, 因此需要根据应用程序来下载相应版本的apache). 如果不需要使用ssl, 则要下载 apache 2.x.x-win32-x86-no_ssl.msi, 如果需要使用, 则需要下载apache 2.x.x-win32-x86-openssl-x.x.x.msi
----------
安装
----------
双击安装程序, 它将引导你完成整个安装过程。请确保你输入正确的服务器url(如果你的服务器没有dns名称,你可以直接输入ip地址)。建议将apache安装成 for all users,on port 80,as a service apache将自动安装一个系统服务以自动启动。注意:如果你已经有一个iis或者其它的程序使用80端口,安装可能会失败。如果是这样,你可以到程序目录下的apache group\apache2\conf目录中找到文件httpd.conf,将其中的listen 80改成一个不用的端口,比如listen 81。然后重新运行这个安装程序,这次应该不会再有问题了。
安装结束之后在浏览器中键入 http://loccalhost,如果呈现了一个已经配置好的网站,那么apache就安装成功了。 如果你将apache安装成一个服务,它将以本地system帐户来运行。为apache创建一个单独的用户来运行它将会更安全一些。
打开apache的安装目录, 找到文件 /conf/httpd.conf, 打开它进行配置的编辑. 配置文件中对于各项都有比较详细的描述, 在配置结束之后可以运行开始菜单中apache安装文件夹中的test configuration工具来检验配置文件是否正确. 如果配置不正确会给出简要的说明. 如果在配置结束之后通过了test configuration工具, 但是仍然不能够顺利运行apache, 可以到[apache安装目录]/logs/ 这个目录中查看记录, 以找出问题所在.
----------
配置 httpd.conf文件
----------
# 设置文档主目录, 后面的就是磁盘的目录
documentroot d:/programs/apache2.2/htdocs
# 添加虚拟目录, 下面的设置在浏览器中输入 http://localhost/webpath/ 的时候机会访问服务器的d:/programs/webpath目录
alias /webpath/ d:/programs/webpath
# 当/webpath 后面不加/的话, 在浏览器中可以输入 http://localhost/webpath apache会自动的在后面添加/
alias /webpath d:/programs/webpath
# url 重定向
redirect permanent /foo/ http://www.example.com/bar/
# 设定虚拟主机: 请参考 http://lamp.linux.gov.cn/apache/apachemenu/vhosts/index.html
?
在win下安装apache配置虚拟目录和unin下基本是一样的
就是修改httpd.conf
1:单个ip对应单个玉米
例如:www.phpunion.com对应192.168.1.1
www.feelone.net对应192.168.1.2
在httpd.conf中加入如下指令
#对应玉米所指向的ip
documentroot c:/web/php #www.phpunion.com要指向的目录,注意一定要有
servername www.phpunion.com #所要绑的玉米
?
#对应玉米所指向的ip
documentroot c:/web/feel #www.feelone.net要指向的目录,注意一定要有
servername www.feelone.net #所要绑的玉米
剩下的可以继续绑!
?
2:单个ip对应多个玉米
?
namevirtualhost 192.168.1.1 #记得一定要有着句啊!
#对应玉米所指向的ip
documentroot c:/web/php #www.phpunion.com要指向的目录,注意一定要有
servername www.phpunion.com #所要绑的玉米
#对应玉米所指向的ip
documentroot c:/web/feel #www.feelone.net要指向的目录,注意一定要有
servername www.feelone.net #所要绑的玉米
这样就绑上对应的目录了,如果没有namevirtualhost 192.168.1.1 这个指令的话,所有的玉米就都指到php目录了.
然后重新启动apache,就可以看到所指的玉米对应相应的虚拟目录!
?
example:
serveradmin webmaster@host.example.com
documentroot /www/docs/host.example.com
servername host.example.com
errorlog logs/host.example.com-error_log
transferlog logs/host.example.com-access_log
?
?
?
?
?
今天网站建设技术部的php程序员教了我一个在本地实现虚拟一个域名来访问本机电脑的数据的方法,首先说说我本地的环境是:win2003+xampp。其实能实现效果就行,直接用xampp的最好方法就是省事,不用一个一个软件安装!然后下面一步一步实现虚拟一个www.123.com来访问本地网站程序。
1.关闭xampp的apache,然后找到c:\xampp\apache\conf\extra\httpd-vhosts.conf文件:
在##namevirtualhost *:80 下面加入以下代码,其中的ip为本地电脑的ip,可以进入cmd使用ipconfig查询到本机ip
namevirtualhost 192.168.1.101:80
然后把以下代码复制一份
##
##serveradmin postmaster@dummy-host2.localhost
##documentroot c:/xampp/htdocs/dummy-host2.localhost
##servername dummy-host2.localhost
##serveralias www.dummy-host2.localhost
##errorlog logs/dummy-host2.localhost-error.log
##customlog logs/dummy-host2.localhost-access.log combined
##
修改成以下代码
##serveradmin postmaster@dummy-host2.localhost
documentroot e:/php/ecshop2.72
servername www.123.com
serveralias www.123.com
##errorlog logs/dummy-host2.localhost-error.log
##customlog logs/dummy-host2.localhost-access.log combined
其中documentroot 是本机网站制作程序的路径,servername及serveralias 就是要虚拟的域名网址,这个随便你高兴了
2.找到c:\xampp\apache\conf\httpd.conf文件,找到以下代码:
documentroot c:/xampp/htdocs
把上面的两个网站目录的路径修改成上面设置的路径
documentroot e:/php
因为设置了虚拟的域名网址,所以路径不用再加上网站的文件夹名称了
3.然后打开c:\windows\system32\drivers\etc\hosts 文件,
在127.0.0.1 localhost下添加你要添加的虚拟域名网址
127.0.0.1 localhost
192.168.1.101 www.123.com
呵呵,修改这三个文件之后就ok了,现在可以重新启动xampp的apache了,然后在ie里输入www.123.com就能访问本地的网站程序了,这样测试的好处是能把本机虚拟成服务器,有些东东很方便就能调试!当然,用localhost也是还可以访问了,这个功能用不到的话也没必要这么搞,我是因为有时用到magento程序,使用域名会更好,不会有一些莫名其妙的问题。当然了,如果你有好几个网站的话,每次都要修改一下c:\xampp \apache\conf\extra\httpd-vhosts.conf对应的documentroot e:/php/ecshop2.72,因为我的网页设计的php程序都是放在e盘下的php文件夹,然后每个网站就一个文件夹,这样每次只需要修改一个文件就ok了,看个人爱好了,共享出来让大家玩玩,嘿嘿!
?
?
详细配置:http://httpd.apache.org/docs/2.2/vhosts/
中文配置:http://www.zzxj.net/web/manual/apachemenu/rewrite/rewrite_guide.html
?
?
?
?
apache虚拟主机的配置
?
虚拟主机的配置
?
基于ip地址的虚拟主机配置
listen 80
documentroot /www/example1
servername www.example1.com
documentroot /www/example2
servername www.example2.org
?
?
?
基于ip和多端口的虚拟主机配置
listen 172.20.30.40:80
listen 172.20.30.40:8080
listen 172.20.30.50:80
listen 172.20.30.50:8080
?
documentroot /www/example1-80
servername www.example1.com
documentroot /www/example1-8080
servername www.example1.com
documentroot /www/example2-80
servername www.example1.org
documentroot /www/example2-8080
servername www.example2.org
?
单个ip地址的服务器上基于域名的虚拟主机配置:
# ensure that apache listens on port 80
listen 80
# listen for virtual host requests on all ip addresses
namevirtualhost *:80
documentroot /www/example1
servername www.example1.com
serveralias example1.com. *.example1.com
# other directives here
documentroot /www/example2
servername www.example2.org
# other directives here
?
在多个ip地址的服务器上配置基于域名的虚拟主机:
listen 80
# this is the “main” server running on 172.20.30.40
servername server.domain.com
documentroot /www/mainserver
# this is the other address
namevirtualhost 172.20.30.50
documentroot /www/example1
servername www.example1.com
# other directives here …
documentroot /www/example2
servername www.example2.org
# other directives here …
?
在不同的端口上运行不同的站点:
基于多端口的服务器上配置基于域名的虚拟主机。
listen 80
listen 8080
namevirtualhost 172.20.30.40:80
namevirtualhost 172.20.30.40:8080
servername www.example1.com
documentroot /www/domain-80
servername www.example1.com
documentroot /www/domain-8080
servername www.example2.org
documentroot /www/otherdomain-80
servername www.example2.org
documentroot /www/otherdomain-8080
?
基于域名和基于ip的混合虚拟主机的配置:
listen 80
namevirtualhost 172.20.30.40
documentroot /www/example1
servername www.example1.com
documentroot /www/example2
servername www.example2.org
documentroot /www/example3
servername www.example3.net
?
?
?
自己的设置
?
hosts
?
?
?
127.0.0.1 localhost
127.0.0.1 fantsi
?
?
?
httpd.conf
?
?
?
alias /fantsi/ e:/xampp/htdocs/fantsi
namevirtualhost 127.0.0.1
documentroot e:/xampp/htdocs
servername localhost
serveralias localhost
documentroot e:/xampp/htdocs/fantsi
servername fantsi
serveralias fantsi