Webサーバの設定

Ubuntu Serverインストール時にLAMPを選択しているので、すでにApache2が起動しているはず。ブラウザから『http://192.168.1.xxx』を叩いてみる。『Index of』という/var/wwwのディレクトリ一覧が出れば、ちゃんと動いている。

Apache2の設定

Apache1のときは、httpd.confでなんでもかんでも設定してたような記憶があるんだけど、Apache2は設定ファイルがいろいろ分かれているみたい。

/etc/apache2/
 ・apache2.conf   ← 基本設定。これは変えないお約束?!
 ・ports.conf    ← ポート設定
 ・sites-available/ ← 仮想ホストの設定ファイルを置く
 ・mods-available/  ← モジュールの設定ファイルを置く
 ・conf.d/      ← 細かい設定はここに置く

共通のディレクトリ設定

$ sudo nano /etc/apache2/conf.d/directory_settings

        Options All
        Options -Indexes
        AllowOverride All
        Order allow,deny
        allow from all

こうしておくと、仮想ホストやユーザー別の設定でいちいちって書かなくていいらしい。

サイトの設定

$ cd /etc/apache2/sites-available/
$ sudo cp default my_site
$ sudo nano my_site
$  :(設定後)
$ sudo a2dissite default  ← defaultを無効に
$ sudo a2ensite my_site   ← my_siteを有効に

『/etc/apache2/sites-available/my_site』はこんな(↓)感じ。(SPAM避けにドメイン名は若干変えてあります)

NameVirtualHost *:80

        DocumentRoot /home/www/2.5
        ServerName 2.5.satake77.net
        ServerAdmin xxx@satake77.com
        ServerSignature On

        LogLevel warn
        ErrorLog /home/log/apache2/25error.log
        CustomLog /home/log/apache2/25access.log combined

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        Alias /doc/ "/usr/share/doc/"



        DocumentRoot /home/www/vote
        ServerName vote.satake77.net
        ServerAdmin xxx@satake77.com
        ServerSignature On

        LogLevel warn
        ErrorLog /home/log/apache2/voteerror.log
        CustomLog /home/log/apache2/voteaccess.log combined

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        Alias /doc/ "/usr/share/doc/"

この例はこんなの(↓)を実現するパターン。
http://2.5.satake77.net』 →『/home/www/2.5/index.html』
http://vote.satake77.net』→『/home/www/vote/index.html』

DNSレコードの設定

これにあわせたFC2ドメインの設定はこちら↓。

DNSレコード情報
 ホスト1 www  / 219.117.201.xxx / A / 10
 ホスト2 @    / 219.117.201.xxx / A / 10
 ホスト3 2.5  / 219.117.201.xxx / A / 10
 ホスト4 vote / 219.117.201.xxx / A / 10

『www』と『@』(ホスト名なし)はで先に書かれた 2.5の方へ振り分けられる。

設定を変えたとき

$ sudo /etc/init.d/apache2 reload  ← 再読込み
$ sudo /etc/init.d/apache2 restart  ← 再起動

普通は再読込みでいいんじゃないかな。

エラーが出たとき

なにも変更してないのに、再起動すると次のようなエラーが出た。

apache2: Could not determine the server's fully qualified domain name,
using 127.0.1.1 for ServerName

ネットをググルと『ServerName 127.0.1.1』を『apache2.conf』に追加しとけというアドバイスが。『apache2.conf』はいじりたくないので『httpd.conf』にこの1行だけ追加してエラーが出なくなった。

参考

こちら(→Ubuntu/apache2 - TOBY SOFT wiki)のサイトに大変お世話になりました。感謝です。
こちら(→http://httpd.apache.org/docs/2.2/ja/vhosts/examples.html)でバーチャルホストの設定を参考にしました。