VirtualHostの設定

VirtualHost とはなんぞ

1つのサーバに複数のドメインを管理する場合に使用
参照:Apache Web サーバ - ご先祖様はきっと農民。

VirtualHost の設定

例)dev.example.com というドメインをバーチャルホストで管理する場合
ドキュメントルートは /var/www/dev.example.com/public_html/

作業の流れ
  1. ディレクトリを作成
  2. バーチャルホストの設定

ディレクトリを作成

  • /var/www/ ディレクトリ以下に以下のファイルを作成

mkdir の -p オプションは存在しない親ディレクトリを作成する場合に使用。

mkdir -p /var/www/dev.example.com/public_html
  • 権限を変更
chown -R riceplanting:riceplanting /var/www/dev.example.com/public_html

編集するファイル

/etc/httpd/conf.d/dev.example.com.conf
  • /etc/httpd/conf.d/ ディレクトリ以下に dev.example.com.conf を新規作成
vim /etc/httpd/conf.d/dev.example.conf
<VirtualHost *:80>
ServerName dev.example.com
DocumentRoot "/var/www/dev.example.com/public_html"
DirectoryIndex index.html index.php
ErrorLog /var/log/httpd/dev.example.com_error_log
CustomLog /var/log/httpd/dev.example.com_access_log combined
AddDefaultCharset UTF-8
<Directory "/var/www/dev.example.com/public_html">
AllowOverride All
</Directory>
</VirtualHost>
/etc/httpd/conf/httpd.conf

VirtualHost の有効化

  • NameVirtualHost のコメントアウトを外す
NameVirtualHost *:80