CultusReport

Web, PHP, smarty, PEAR, e-commerce, or other internet technologies

Apache2のvhost設定@Ubuntu14.04

Ubuntuでのvhost設定メモ

今回は、以下2サイトを1サーバーに構築します
example.com
test.com

なお、今回の投稿は以下のサイトを参考にしてます。
(圧倒的に下記サイトのほうが詳しいです。)
How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS | DigitalOcean

バーチャルホスト用のディレクトリを作成

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/test.com/public_html

ディレクトリ権限を変更

sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/test.com/public_html


インストール時点でアクセス可能になっているかもしれませんが、
念のため/var/wwwのアクセス権も追加しておきます。

sudo chmod -R 755 /var/www

テストサイトの作成

sudo vim /var/www/example.com/public_html/index.html
<html>
  <head>
    <title>Welcome to Example.com!</title>
  </head>
  <body>
    <h1>Success!  The example.com virtual host is working!</h1>
  </body>
</html>
sudo vim /var/www/test.com/public_html/index.html
<html>
  <head>
    <title>Welcome to Test.com!</title>
  </head>
  <body>
    <h1>Success!  The test.com virtual host is working!</h1>
  </body>
</html>

バーチャルホストの設定

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test.com.conf
<VirtualHost *:80>
    ServerAdmin admin@test.com
    ServerName test.com
    ServerAlias www.test.com
    DocumentRoot /var/www/test.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

confファイルに文法ミスがないかを確認するためには、以下のコマンドを利用します

/usr/sbin/apache2ctl configtest

新バーチャルホストの実行

先ほど設定したファイルを有効にします

sudo a2ensite example.com.conf
sudo a2ensite test.com.conf

confファイルを変更したので、Apacheを再起動します

sudo service apache2 restart

hostsファイルの編集

hostsファイルを編集し、名前解決ができるようにします

sudo vim /etc/hosts

hostsファイルに以下の設定を追記します

127.0.0.1   example.com
127.0.0.1   test.com

バーチャルホストの確認

ドメインによって参照先ファイルが異なることを確認します
下記サイトにブラウザからアクセスしてみて、サイトごとにメッセージが異なることを確認します

http://example.com/index.html
http://test.com/index.html

その他Apacheの設定

セキュリティリスクを低減するため、実行ユーザをapacheにしておきます

ユーザーグループ"apache"とユーザー"apache"を作成します。

groupadd apache
useradd -g apache apache

実行ユーザーの設定ファイルを編集します

sudo vim /etc/apache2/envvars


変更前

# Since there is no sane way to get the parsed apache2 config in scripts, some
# settings are defined via environment variables and then used in apache2ctl,
# /etc/init.d/apache2, /etc/logrotate.d/apache2, etc.
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

変更後

# Since there is no sane way to get the parsed apache2 config in scripts, some
# settings are defined via environment variables and then used in apache2ctl,
# /etc/init.d/apache2, /etc/logrotate.d/apache2, etc.
export APACHE_RUN_USER=apache
export APACHE_RUN_GROUP=apache

その他confファイルの設定

セキュリティリスクを低減するため、confファイルを変更しておきます

sudo vim /etc/apache2/conf-enabled/security.conf 

変更前

#ServerTokens Minimal
ServerTokens OS
#ServerTokens Full
...
#ServerSignature Off
ServerSignature On

変更後

#ServerTokens Minimal
ServerTokens Prod
#ServerTokens Full
...
#ServerSignature Off
ServerSignature Off

変更後は、改めてApacheを再起動しましょう

sudo service apache2 restart