라이프로그


Apache のバーチャルホストの設定 IT

Apache のバーチャルホストの設定は、httpd.conf に直接書く場合もありますが、他のファイルに分けて書いた方がメンテナンスは容易です。Apache 2.2 では、httpd/extra/httpd-vhosts.conf に書くようになっています。CentOS など Red Hat 系では /extra/ ディレクトリはなく、conf.d/*.conf が自動的に読み込まれますので、/etc/httpd/conf.d/httpd-vhosts.conf というファイルを作るのがよいと思います。

CentOS 5での例ですが、まず /etc/httpd/conf/httpd.conf を編集します。

NameVirtualHost *:80

上記を有効にします。バーチャルホストはドメインごとに以下のように設定します。

# www.example1.org<VirtualHost *:80>    DocumentRoot /var/www/vhosts/example1    ServerName www.example1.org    ServerAlias example1.org    ErrorLog logs/example1.org-error_log    CustomLog logs/example1.org-access_log combined</VirtualHost>

http://www.example1.org/ にアクセスしたときに /var/www/vhosts/example1/ ディレクトリのファイルを表示する例です。エイリアスとして www なしの http://example1.org/ でも同じ設定を使うようになっています。また、ログも分けて出力するようにしています。

バーチャルホストの設定がたくさんあるときは、それぞれのサイトごとにファイルを分けた方が良い場合もあります。(特に複数の管理者で触るような場合)まず httpd.conf で

Include conf.d/vhosts/*.conf

のようにして、/etc/httpd/conf.d/vhosts/*.conf ファイルを読み込むように設定します。ドメインごとに example1.org.conf や example2.net.conf などのファイルを作っていきます。

<VirtualHost *:80>    DocumentRoot /var/www/vhosts/example2    ServerName example2.net    ServerAlias www.example2.net    AddDefaultCharset euc-jp    <Directory "/var/www/vhosts/example2">      Options Includes ExecCGI FollowSymLinks MultiViews    php_value mbstring.language Japanese    php_value mbstring.internal_encoding EUC-JP    php_value mbstring.http_input auto    php_value mbstring.http_output EUC-JP</VirtualHost>

上記は、サーバ全体の文字コード指定は UTF-8 だが、example2.net だけは EUC-JP で稼動させたいときの設定です。HTML だけでなく PHP の内部コードと出力文字コードも EUC-JP にしています。

設定を有効にするには、Apahce をリロードします。

# service httpd relaod
http://futuremix.org/2009/11/apache-virtual-hosts-conf

1 2 3 4 5 6 7 8 9 10 다음