1つのWebサーバで複数のWebアプリを構築する(VirtualHostとポート番号)

Pocket

ここでは、1つのWebサーバ(Apache2.2)上で、複数のWebアプリケーションを稼働させる設定例を掲載しています。なお、このページで掲載しているWebアプリは Zend Framework1 を使って作成してあります。

スポンサーリンク

VirtualHost の 設定(Apache2.2)

複数の Zend Framework アプリケーションを1つの WEB サーバー上で動作させたい場合には、 Apache のバーチャルホストの設定を利用する方法が簡単です。異なるポート番号でそれぞれのサイトを運営する VirtualHost の 設定例(Apache2.2)は次の通りです。

http.conf

# 接続を受け付けるポートを複数指定する
Listen 80
Listen 8080

# rewrite モジュールをロードする
LoadModule rewrite_module modules/mod_rewrite.so

(省略)

# 拡張 conf ファイルを読み込むようにコメントをはずす
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

extra/httpd-vhosts.conf

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

#
# 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>
    ServerAdmin  webmaster1
    DocumentRoot "/path/to/DocumentRoot1"
    ServerName   192.168.1.1:80
    ServerAlias  www.dummy-host.example.com
    ErrorLog     "logs/error_log_1"
    CustomLog    "logs/access_log_2" common

    # 環境変数を設定する場合(for zend framework)
    # .htaccess で設定しても良い
    SetEnv APPLICATION_ENV "development"

    # .htaccess の読み込みを有効にする(for zend framework)
    <Directory /path/to/DocumentRoot2>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:8080>
    ServerAdmin  webmaster2
    DocumentRoot "/path/to/DocumentRoot2"
    ServerName   192.168.1.1:8080
    ErrorLog     "logs/error_log_1"
    CustomLog    "logs/access_log_2" common

    SetEnv APPLICATION_ENV "development"

    <Directory /path/to/DocumentRoot2>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

.htaccess の設定

Zend Framework アプリケーションごとにそれぞれ .htaccess ファイルを配置します。下記では、環境変数(APPLICATION_ENV)の設定と、公開ディレクトリに存在するファイル以外へのリクエストはすべて index.php に集約するためのリライト設定を行っています。

SetEnv APPLICATION_ENV development

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

サイト(アプリケーション)ごとに ZendFramework のバージョンを変更する

異なるバージョンの ZendFramework を使用するには、index.php でそれぞれのバージョンのフレームワークのディレクトリパスを読み込むように変更します。

index.php

// 以下のディレクトリパスをそれぞれ変更する
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));
参考
スポンサーリンク


Pocket

Leave a Comment

Your email address will not be published. Required fields are marked *