Webサーバの構築

Webサーバ構築に必要なもの

構築手順

  1. ソースアーカイブの入手と展開
  2. コンパイルとインストール
  3. 設定ファイルの修正
  4. サービスデーモンの起動

apache.org HTTP SERVER PROJECT の "Stabe Release" から Unix Source と辿り、httpd-2.x.y.tar.gz へのリンクのURLを得る。

fetch httpd-2.x.y.tar.gz
tar zxpf httpd-2.x.y.tar.gz
cd httpd-2.x.y
ls
less INSTALL        (qで抜ける)

インストール

コンパイルしたあと、スーパーユーザ権限でインストールする。

./configure --prefix=/var/apache2
make
sudo make install

/var/apache2/conf ディレクトリにインストールされている 設定ファイルのうちhttpd.confを確認・修正する。ここでは

の項目を変更する。修正にはviを使う。

sudo -H -s
cd /var/apache2/conf
vi httpd.conf

[数] は省略すると1。

[]x字削除
[]dd行削除
[]iカーソル位置で挿入モードへ
[]I行頭で挿入モードへ
[]aカーソル直後で挿入モードへ
[]A行末で挿入モードへ
ESC挿入モードを抜けコマンドモードへ
ZZ保存終了
:q! [Return]編集破棄終了
:w! [Return]強制保存
:wq! [Return]強制保存終了

vi の使い方も参照。

主な設定項目

Log

アクセスログ、エラーログファイルの場所、書式を決める。

User home directories

~ユーザで該当ユーザの ~/public_html/ ディレクトリを意味する。

Virtual host

ひとつのホストで複数のWeb空間を持たせる。

WebDav

Webサーバによるファイル共有。

SSL

Secure Socket Layer - 暗号化ソケットによる伝送

変更点を差分形式で以下に示す(行頭マイナスの行を消して行頭+の行を足す)。


--- original/httpd.conf 2008-11-20 10:19:09.000000000 +0900
+++ httpd.conf  2008-11-20 10:24:24.000000000 +0900
@@ -61,8 +61,8 @@
 # It is usually good practice to create a dedicated user and group for
 # running httpd, as with most system services.
 #
-User daemon
-Group daemon
+User www
+Group www
 </IfModule>
 
 # 'Main' server configuration
@@ -82,7 +82,7 @@
 # e-mailed.  This address appears on some server-generated pages, such
 # as error documents.  e.g. admin@your-domain.com
 #
-ServerAdmin you@example.com
+ServerAdmin c108xxx@e.koeki-u.ac.jp
 
 #
 # ServerName gives the name and port that the server uses to identify itself.
@@ -91,7 +91,7 @@
 #
 # If your host doesn't have a registered DNS name, enter its IP address here.
 #
-#ServerName www.example.com:80
+ServerName qm220.is.koeki-u.ac.jp:80

 #
 # DocumentRoot: The directory out of which you will serve your
@@ -376,7 +376,7 @@
 #Include conf/extra/httpd-languages.conf

 # User home directories
-#Include conf/extra/httpd-userdir.conf
+Include conf/extra/httpd-userdir.conf

 # Real-time info on requests and configuration
 #Include conf/extra/httpd-info.conf

その他の項目

  1. CGIを使うためには httpd.conf 内の AddHandler cgi-script を有効化する。

autoconfとconfigure

ソース配布されているものには configure スクリプトが付属していることが 多い。これは、コンパイルに必要な情報を自動的に収集して、それに応じた Makefileを生成するものである。

configure スクリプトに与えるオプションで、そのソフトウェアをインストー ルするディレクトリを指定したりするなど、様々な設定を変えることができる。 どのようなオプションがあるかは configure --help で知ることができる。

./configure --help | less

--prefix=ディレクトリ オプションで インストール先ディレクトリを変更できる。その他ソフトウェア固有の 機能のON/OFFを選択するオプションが多数ある。

ひとつのソフトウェアは、別のソフトウェアに由来するヘッダファイルと ライブラリに依存することが多い。ヘッダファイルはCソースのコンパイル時に 参照され、ライブラリファイルはリンク時に参照される。どのディレクトリから ヘッダファイルを探すか、ライブラリを探すか、はgccに与えるオプションで指 示できる。configure実行時にgccがそれらのオプションを設定するように操作 することができる。

Apacheの起動

Apacheの起動は $PREFIX/bin/apachectl スクリプトを介して 行なう。

/var/apache2/bin/apachectl help

で使い方が出る。通常は、

apachectl start起動
apachectl restart再起動
apachectl stop停止

を使う。起動の確認はpsコマンドでプロセス一覧を見て行なう。

ps auxw

起動したら手元のブラウザ(firefox)で

http://qmXXX.is.koeki-u.ac.jp

を開いて確認する。

Userdirの変更と確認

httpd.confで有効化した

Include conf/extra/httpd-userdir.conf

を確認し、これを変更してみる。

cd /var/apache2/conf/extra
vi httpd-userdir.conf

デフォルトの

UserDir public_html

public_html を好きなディレクトリ名に変えてみる。
例:

UserDir webdir

変更をApacheに反映させるにはapachectlを用いる。

/var/apache2/bin/apachectl restart

一般ユーザに戻り、~/webdir を作り、その中に index.html を作成し、ブラウザでアクセスしてみる。

VirtualHostの構築

(この項目はDNS設定完了後に行なう)

ここでは,独自ドメインが foo.is.koeki-u.ac.jp で, そのドメイン内に

の2つの仮想ホストを構築する例を示す。 2つのドメイン名はDNSに登録済みである必要がある。

httpd.confの修正

サーバ上で /var/apache2/conf に移動し, httpd.conf ファイルを開く。

cd /var/apache2/conf
sudo vi httpd.conf

"vhosts" というパターンで 検索してみつかった行のコメント記号(#)を外す。

#Include conf/extra/httpd-vhosts.conf
     ↓
Include conf/extra/httpd-vhosts.conf

続いて,extra/httpd-vhosts.conf を編集し,

<VirtualHost *:80>
    ServerAdmin 管理者(自分の)メイルアドレス
    DocumentRoot "rootドキュメントのディレクトリ名(フルパス)"
    ServerName サーバ名
    ServerAlias その他のサーバ名 # もしあれば,無ければコメントアウト
    ErrorLog "logs/エラーログのファイル名_log"
    CustomLog "logs/アクセスログのファイル名_log" common
</VirtualHost>

となるように修正する。以下は,www.foo.is.koeki-u.ac.jp と fes.is.koeki-u.ac.jp の2ドメインの設定を記述した例である。

<VirtualHost *:80>
    ServerAdmin ta01002@e.koeki-u.ac.jp
    DocumentRoot "/var/home/yuuji/webspace"
    ServerName www.foo.is.koeki-u.ac.jp
    # ServerAlias 別名サーバ
    ErrorLog "logs/www-error_log"
    CustomLog "logs/www-access_log" common
</VirtualHost>

<Directory /var/home/yuuji/webspace>
 Options Indexes
 Order allow, deny
 Allow from all
</Directory>

<VirtualHost *:80>
    ServerAdmin ta01002@e.koeki-u.ac.jp
    DocumentRoot "/var/home/yuuji/fes-web"
    ServerName fes.foo.is.koeki-u.ac.jp
    ErrorLog "logs/fes-error_log"
    CustomLog "logs/fes-access_log" common
</VirtualHost>

<Directory /var/home/yuuji/fes-web>
 Options Indexes
 Order allow, deny
 Allow from all
</Directory>

クォートや綴り間違いのないことを確認したら保存終了し, apacheを再起動する。

sudo /var/apache2/bin/apachectl restart

エラーなしで再起動できたら,2つのドメイン用のコンテンツを準備する。 上記の設定例では,ホームディレクトリ内の webspace/fes-web/ ディレクトリにそれぞれ独立した index.html を配備することになる。

準備できたら手元のブラウザから

2つのドメイン(実際は自分で設定したドメイン)をURL指定して開き, 中味を確認する。

yuuji@koeki-u.ac.jp