PHP実践の道(二)apache仮想ホスト構成


PHP実践の道(目次索引)
一、環境
1)オペレーティングシステム:windows xp
       2)apache2.2
二、背景
私たちがプロジェクトを開発したり、勉強したりするとき、いつも複数のプロジェクトを構築します.これによりapacheでデフォルトの作業ディレクトリはhtdocsディレクトリの下にのみ存在します.仮想ホストを構築し、単一のコンピュータを複数のサーバとして使用できるようにする必要があります.仮想ホストの構成が必要です
三、解決方案
1)複数の作業ディレクトリの要件を満たすためにのみhttpd.confでの構成モジュール、具体的な方法はネット上で探すことができますが、この方法には欠陥があり、作業ディレクトリがディスクルートディレクトリの下にない場合、解析パスに問題が発生します.
2)apache自体は仮想ホスト構成の拡張を提供し、confextraにはファイルhttpd-vhostsが表示される.conf、このファイルは仮想ホストを構成するために使用されます.仮想ホストの構成には基本的に2つの方式があり、1つはIPアドレスに基づいており、この方式にはホストに複数の独立したIPが必要である.もう1つの名前ベースの構成は、1つのIPアドレスで複数の仮想ホストを名前で構成できるより柔軟な方法です.次に、名前構成に基づく複数の仮想ホストの構成について説明します.
四、実施手順
1)仮想ホスト構成の拡張をhttpdで解放する.confファイルに次の2行が見つかりました.
# 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

次に2つのVirtualHostを構成し、1つ目を例に挙げてみましょう
ServerNameはドメイン名、つまりブラウザでアクセスするアドレスに相当します.ここではwww.cyxlgzsを構成しています.com
Directory Indexはホームページの構成で、アクセス時に特定のページを指定しない場合にアクセスするデフォルトのページです.ここではindexです.php
DocumentRootプロジェクトのディスクパス
Directoryはアクセス権限を設定しています
3)ドメイン名とIPアドレスの対応関係を設定する.
私たちの環境はwindowsの下にあるので、本機でテストするときに、この項目を構成しないと衝突します.C:/WINDOWS/system 32/drivers/etcディレクトリのhostsファイル(linuxは一般的に/etc/hosts)を見つけ、メモ帳で開き、次の2つを追加します.
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でテスト