VirtualBox3.2.10 と Ubuntu10.10 で開発環境を構築する-②Apache設定編

テスト用のサイトをNameVirtualHostで2つ作り、windowsからアクセスしてみます。
(以下では 「sudo su -」でrootで作業しています)

まず、現状の設定は以下のようになっています。
・設定ファイル(/etc/apache2/apache2.conf)の229行目あたり

# Include the virtual host configurations:
 Include sites-enabled/

でsites-enabled/以下のファイルも設定ファイルとして読み込んでいる。
・sites-enabledではリンクファイルによりsites-available/defaultを参照している。

/etc/apache2/sites-enabled# ll
合計 0
lrwxrwxrwx 1 root root 26 2010-11-03 15:11 000-default -> ../sites-available/default

このdefaultの中に、現在動いているサイトの設定が書かれています。
defaultの中身は

/etc/apache2/sites-available# head -15 default
<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

/etc/apache2/sites-available#

このようになっていて、 /var/www がドキュメントルートになってますので、
ここに置いたファイルがブラウザから見れることになります。

/var/www# head index.html
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
/var/www#

動作確認の時にブラウザで見た「It works!」というのはここに書かれていたのでした。

これをこのようにしたいと思います。
・/etc/apache2/vhosts に設定ファイルを置く。
・/www/{環境名}/htdocs をドキュメントルートとする

まずは今公開中のサイトを閉じます。(設定ファイルから消します)

/etc/apache2# cp apache2.conf apache2.conf_20101120  # バックアップ
/etc/apache2# vi apache2.conf
 (略)
/etc/apache2# diff apache2.conf_20101120 apache2.conf
230c230
< Include sites-enabled/  # 以前はこうだったインクルードディレクトリを・・・
---
> Include vhosts/  # こう変更しました。
/etc/apache2#

次に、新しく作るサイトの設定ファイルを書きます
(gakaxy, sisyamoの2サイトを作り、それぞれ
http://galaxy.localhosthttp://sisyamo.localhost
でブラウザからアクセスする想定です)

/etc/apache2# mkdir vhosts
/etc/apache2# cd vhosts
/etc/apache2/vhosts# vi vhosts.conf
 (略)
/etc/apache2/vhosts# cat vhosts.conf
<VirtualHost *:80>
  ServerName galaxy.localhost
  DocumentRoot /www/galaxy/htdocs
  LogLevel  warn
  LogFormat "%h %l %u %t \"%r\" %>s %b" common
  ErrorLog  /var/log/apache2/galaxy/error.log
  CustomLog /var/log/apache2/galaxy/access.log common
</VirtualHost>

<VirtualHost *:80>
  ServerName sisyamo.localhost
  DocumentRoot /www/sisyamo/htdocs
  LogLevel  warn
  LogFormat "%h %l %u %t \"%r\" %>s %b" common
  ErrorLog  /var/log/apache2/sisyamo/error.log
  CustomLog /var/log/apache2/sisyamo/access.log common
</VirtualHost>
/etc/apache2/vhosts#

ログファイル用ディレクトリを準備します。

/etc/apache2/vhosts# cd /var/log/apache2
/var/log/apache2# mkdir galaxy
/var/log/apache2# mkdir sisyamo

ログローテーションの設定をします。
以下のように /etc/logrotate.d/apache2 を変更しました。

/etc/logrotate.d# cat apache2
/var/log/apache2/galaxy/*.log /var/log/apache2/sisyamo/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                /etc/init.d/apache2 reload > /dev/null
        endscript
}
/etc/logrotate.d#

ドキュメントルートにファイルを置きます。

/etc/logrotate.d# cd /
/# mkdir www
/www# mkdir galaxy
/www# mkdir sisyamo
/www# cd galaxy
/www/galaxy# mkdir htdocs
/www/galaxy# cd htdocs
/www/galaxy/htdocs# echo "<html>\n<body>\nthis is galaxy\n</body>\n<html>" > index.html
/www/galaxy/htdocs# cd ../../sisyamo
/www/sisyamo# mkdir htdocs
/www/sisyamo# cd htdocs
/www/sisyamo/htdocs# echo "<html>\n<body>\nthis is sisyamo\n</body>\n<html>" > index.html
/www/sisyamo/htdocs#

apacheを再起動して、設定を有効にします。

/www/sisyamo/htdocs# apache2ctl configtest  # 設定ファイルの文法チェック
Syntax OK
/www/sisyamo/htdocs#
/www/sisyamo/htdocs# /etc/init.d/apache2 restart
 * Restarting web server apache2
 ... waiting    ...done.
/www/sisyamo/htdocs#

これで公開するファイルが用意できましたが、まだブラウザからアクセスすることはできません。
なぜなら、"galaxy.localhost"のアクセス先(Ubuntu)のIPがDNSサーバに登録されていないからです。
そこで、windowsのローカルのhostsファイルに設定を書きます。
windows7ではc:\windows\system32\drivers\etc にあります。
これに以下の2行を追加します。
192.168.56.101 galaxy.localhoset
192.168.56.101 sisyamo.localhoset

これで、ブラウザから
http://galaxy.localhost/http://sisyamo.logalhost/
にアクセスしてみてください。
それぞれ「this is galaxy」「this is sisyamo」というのが見えたらOKです!