`
cyxlgzs
  • 浏览: 89374 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

PHP实践之路(二)apache虚拟主机配置

 
阅读更多

PHP实践之路(目录索引)


一、环境

1)操作系统:windows xp
2)apache2.2

二、背景

当我们在开发项目或者在学习时,总会建立多个项目。这样在apache中默认的工作目录只有在htdocs目录下。所以我们必须建立虚拟主机,让我们的单台计算机可以当多个服务器来使用。这里我们就需要配置虚拟主机

三、解决方案

1)如果只为了达到多个工作目录的要求,可以在httpd.conf中配置<IfModule alias_module>模块,具体方式可以在网上找,但这个方式有缺陷,当工作目录不在磁盘根目录下时,解析路径将会遇到问题
2)apache本身提供了虚拟主机配置的扩展,在\conf\extra在可以看到有一个文件httpd-vhosts.conf,这个文件就是配置虚拟主机用的。而虚拟主机配置基本上有两种方式,一种是基于IP地址的,这种方式需要主机有多个独立的IP;另一种的基于名字配置,这种方式更加的灵活,一个IP地址就可以通过名字来配置多个虚拟主机。下面我们主要讲一下基于名字配置的多个虚拟主机的配置

四、实施步骤

1)释放虚拟主机配置的扩展,在httpd.conf文件中找到如下两行
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
我们仅需要去掉Include前面的“#”即可达到目的,变成如下
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

2)配置httpd-vhost.conf。配置如下
#
# Virtual Hosts
#
# 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.2/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.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# 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>
    ServerName www.cyxlgzs.com
	DirectoryIndex index.php 
    DocumentRoot "F:/MyWorkSpace/project/php/cyxlgzs"
	<Directory "F:/MyWorkSpace/project/php/cyxlgzs"> 
        Options Indexes FollowSymLinks 
        AllowOverride None 
        Order allow,deny 
        Allow from all 
	</Directory> 
</VirtualHost>

<VirtualHost *:80>
    ServerName www.first_php.com
	DirectoryIndex index.php 
    DocumentRoot "F:/MyWorkSpace/project/php/first_php"
	<Directory "F:/MyWorkSpace/project/php/first_php"> 
        Options Indexes FollowSymLinks 
        AllowOverride None 
        Order allow,deny 
        Allow from all 
	</Directory> 
</VirtualHost>

首先看到这一行
NameVirtualHost *:80

这里指定了访问的域名为*,也就是任意,然后端口是80.这里的端口和httpd.conf中的端口配置一致,也就是httpd.conf中的如下一行
Listen 80

然后接下来我们配置了两个VirtualHost,拿第一作为例子讲一下
ServerName相当于是一个域名,也就是我们在浏览器中访问的地址,这里我们配置的是www.cyxlgzs.com
DirectoryIndex是主页的配置,当访问时不指定具体页面时访问的默认网页,这里我们是index.php
DocumentRoot项目的磁盘路径
Directory配置的是一些访问的权限

3)配置域名和IP地址的对应关系。
因为我们的环境是在windows下,在本机测试时,如果不配置该项将导致冲突。我们找到C:/WINDOWS/system32/drivers/etc目录下的hosts文件(linux一般在/etc/hosts),用记事本打开,添加如下两项
127.0.0.1                     www.cyxlgzs.com
127.0.0.1                     www.first_php.com 

IP地址后面的域名和httpd-vhosts.conf文件中添加的虚拟主机相对应

4)配置完成,重庆apache服务。在浏览器中输入www.cyxlgzs.com和www.first_php.com进行测试

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics