apache 2.2.9

(最終更新日時:2009-01-05 18:20:16)
オープンソースのWeb用サーバーである apache 2.x をWebサーバーとしてインストールします。
また、インストールしたWebサーバーは、名前ベースの仮想ドメイン/ホストで運用するように設定します。

ダウンロード

Apache HTTP Serverhttpd-2.2.9.tar.bz2

インストール

apache実行用の group および user を作成します。
# groupadd -g 48 apache
# useradd -u 48 -g apache -s /sbin/nologin -d /home/www apache
/usr/local/srcディレクトリでアーカイブを展開し、コンパイルおよびインストールを行います。
$ cd /usr/local/src                                      必ず一般ユーザーで行います
$ tar xjvf ダウンロード先/httpd-2.2.9.tar.bz2
$ cd httpd-2.2.9
$ ./configure \
>   --enable-ssl \                                       SSLを使用します
>   --enable-rewrite \                                   RewriteEngineを使用します
>   --enable-proxy \                                     プロキシ/ゲートウェイ機能を使用します
>   --enable-proxy-ajp \                                 プロキシ/ゲートウェイ機能でAJP13プロトコルを使用します
>   --enable-vhost-alias \                               バーチャルホストのディレクトリエイリアス機能を使用します
>   --enable-auth-digest \                               Digest認証を有効にします
>   --enable-suexec \                                    suEXECを使用します
>   --with-suexec-caller=apache \                        suEXECを実行するユーザー
>   --with-suexec-userdir=/home/www/vhosts \             suEXECがアクセス可能なユーザーディレクトリ
>   --with-suexec-docroot=/home/www \                    Apacheのドキュメントルート
>   --with-suexec-uidmin=10000 \                         suEXEC対象ユーザーの最小ユーザーID
>   --with-suexec-gidmin=10000 \                         suEXEC対象グループの最小グループID
>   --with-suexec-logfile=/var/log/httpd/suexec_log \    suEXECのログファイル
>   --with-included-apr                                  ApacheにバンドルされているAPRを使用します

(2.2.9では、configure: WARNING: Unrecognized options: の警告が出ますが、無視します。)

$ make

$ su                                                     ここからはrootで行います
# make install

Apacheサーバーディレクトリ構成

インストール後のApacheサーバーディレクトリは以下になります。(抜粋)
赤字のディレクトリは作成してください。()内はパーミッションです。
/usr/local/apache2/                                      … Apacheサーバートップディレクトリ
  ┗ conf/                                               … 定義ファイルディレクトリ
       ┣ extra/                                         … 拡張定義ファイルディレクトリ
       ┃   ┗ vhosts/ (755)                             … 仮想ドメイン/ホスト用定義ファイルディレクトリssl.crt/ (755)                                 … SSL用サーバー証明申請書ディレクトリssl.key/ (500)                                 … SSL用サーバー秘密鍵ディレクトリ

/var/log/                                                … ログディレクトリhttpd/ (755)                                        … Apacheサーバーログディレクトリvhosts/ (755)                                  … 仮想ドメイン/ホストログディレクトリsystem-act.com/ (755)                     … system-act.comドメインログディレクトリ
            ┃   ┣ www/ (755)                           … www.system-act.comログディレクトリ
            ┃   ┗ ssl/ (755)                           … ssl.system-act.comログディレクトリnekojara.com/ (755)                       … nekojara.comドメインログディレクトリwww/ (755)                           … www.nekojara.comログディレクトリ

設定

http.conf の設定を行います。
# vi /usr/local/apache2/conf/http.conf

     :
User  apache                                             apacheの実行ユーザーを変更しています
Group apache                                             apacheの実行グループを変更しています
     :
ServerAdmin Webサーバ管理者のメールアドレス
     :
#ServerName www.example.com:80
ServerName www.system-act.com                            実サーバの名称(仮想ドメイン/ホストなので仮)を指定します
     :
<IfModule dir_module>
    DirectoryIndex index.html index.php index.cgi        ディレクトリインデックスファイルを追加しています
</IfModule>
     :
ErrorLog /var/log/httpd/error_log                        apacheサーバー用のエラーログファイルを変更しています
     :
    LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%!414r\" %>s %b" common     wormによるログの増大対策をします#CustomLog logs/access_log common                    commonフォーマットのログファイルは使用しません
     :
    #CustomLog logs/access_log combined

    SetEnvIf Remote_Addr 192.168.                     nolog
                                                         ローカルネット内からのアクセスを記録しないようにしています
    SetEnvIfNoCase Request_URI "\.(css|js)$"          nolog
                                                         .css/.jsファイルへのアクセスを記録しないようにしています
    SetEnvIfNoCase Request_URI "\.(gif|jpg|png|bmp)$" nolog
                                                         画像ファイルへのアクセスを記録しないようにしています
     :
# Server-pool management (MPM specific)
#Include conf/extra/httpd-mpm.conf
Include conf/extra/httpd-mpm.conf                        MPM定義を変更します
     :
# Language settings
#Include conf/extra/httpd-languages.conf
Include conf/extra/httpd-languages.conf                  言語設定を変更します
     :
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
Include conf/extra/httpd-vhosts.conf                     仮想ドメイン/ホストの設定を行います
     :
# Various default settings
#Include conf/extra/httpd-default.conf
Include conf/extra/httpd-default.conf                    その他の既定設定を変更します
httpd-mpm.conf の設定を行います。
# vi /usr/local/apache2/conf/extra/httpd-mpm.conf

     :
<IfModule !mpm_netware_module>
    PidFile "/var/run/httpd.pid"                         Pidファイルの場所を変更しています
</IfModule>
     :
<IfModule !mpm_winnt_module>
<IfModule !mpm_netware_module>
LockFile "/var/lock/subsys/httpd"                        Lockファイルの場所を変更しています
</IfModule>
</IfModule>
     :

httpd-languages.conf の設定を行います。
# vi /usr/local/apache2/conf/extra/httpd-languages.conf

     :
# DefaultLanguage nl
DefaultLanguage ja                                       デフォルトの言語を日本語にしますAddDefaultCharset UTF-8                                  デフォルトの文字をUTF-8にします

AddCharset us-ascii.ascii .us-ascii
     :

httpd-vhosts.conf の設定を行います。
# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

     :
# configuration.
Include conf/extra/vhosts-common.conf                    仮想ホスト共通の定義を別ファイルで行っています
     :
NameVirtualHost 192.168.11.11:80                         名前ベースの仮想ホストにしています
     :
<VirtualHost 192.168.11.11:80>
    ServerAdmin 当該仮想ホストの管理者メールアドレス
    DocumentRoot "/home/www/vhosts/system-act.com/www/public_html"
                                                         VirtualDocumentRootで生成することもできますが、
                                                         その場合、PHPの$_SERVER['DOCUMENT_ROOT']が
                                                         設定されないようですので、VirtualDocumentRootを使用せず、
                                                         各ホストのDocumentRootを使用します
    ServerName www.system-act.com                        仮想ホストのサーバ名を指定します
    #ServerAlias www.dummy-host.example.com              仮想ホストの別名は使用しません
    ErrorLog "/var/log/httpd/vhosts/system-act.com/www/error_log"
                                                         エラーログファイルを変更しています
    CustomLog "/var/log/httpd/vhosts/system-act.com/www/access_log" combined env=!nolog
                                                         アクセスログファイルを変更しています
                                                         統計情報取得のためcombinedフォーマットで記録します
                                                         nologに設定した情報以外を記録します
    Include conf/extra/vhosts/www.system-act.com.conf    ディレクトリ設定情報等は別ファイルにしています
</VirtualHost>

<VirtualHost 192.168.11.11:80>
    ServerAdmin 当該仮想ホストの管理者メールアドレス
    DocumentRoot "/home/www/vhosts/nekojara.com/www/public_html"
    ServerName www.nekojara.com
    ErrorLog "/var/log/httpd/vhosts/nekojara.com/www/error_log"
    CustomLog "/var/log/httpd/vhosts/nekojara.com/www/access_log" combined env=!nolog
    Include conf/extra/vhosts/www.nekojara.com.conf
</VirtualHost>
     :

httpd-default.conf の設定を行います。
# vi /usr/local/apache2/conf/extra/httpd-default.conf

     :
ServerTokens Prod                                        HTTPレスポンスは製品名のみ表示します
     :
ServerSignature Off                                      エラー時にバージョンを非表示にします
各仮想ホスト共通設定ファイル vhosts-common.conf を作成します。
# vi /usr/local/apache2/conf/extra/vhosts-common.conf

# (悪質なロボット等をアクセス拒否用環境変数に設定します)
SetEnvIf User-Agent "^Pockey-GetHTML"        badaccess
SetEnvIf User-Agent "^Yeti"                  badaccess   現在 iptables で拒否しています
SetEnvIf User-Agent "^NP_Trackback/2\.0\.3$" badaccess   正規の NP_TrackBack ではありません

<Directory    "/">
  Order           allow,deny
  Allow      from all
  Deny       from env=badaccess
</Directory>

# (RewriteEngineの共通設定を行います)
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteLog    /var/log/httpd/rewrite.log               ログファイルを指定します

  # (当サイトのグローバルIPアドレスでのアクセスを強制的にリダイレクトします)
  RewriteCond   %{HTTP_HOST} 203\.189\.48\.125
  RewriteRule   ^/(.*)$ http://www.system-act.com/$1 [R=301,L]
</IfModule>

仮想ホスト www.system-act.com の設定ファイル www.system-act.com.conf を作成します。
# vi /usr/local/apache2/conf/extra/vhosts/www.system-act.com.conf

# (仮想ホスト毎にRewriteLogへの制御が可能なようにしています)
<IfModule mod_rewrite.c>
  RewriteEngine   On
  RewriteOptions  inherit
  RewriteLogLevel 0
</IfModule>

Alias /css       "/home/www/vhosts/system-act.com/www/css"
Alias /images    "/home/www/vhosts/system-act.com/www/images"

仮想ホスト www.nekojara.com の設定ファイル www.nekojara.com.conf を作成します。
# vi /usr/local/apache2/conf/extra/vhosts/www.nekojara.com.conf

<IfModule mod_rewrite.c>
  RewriteEngine   On
  RewriteOptions  inherit
  RewriteLogLevel 0
</IfModule>

Alias /css       "/home/www/vhosts/nekojara.com/www/css"
Alias /images    "/home/www/vhosts/nekojara.com/www/images"

ScriptAlias /cgi-bin "/home/www/vhosts/nekojara.com/www/cgi-bin"

# (nekojara.com各ユーザーのディレクトリ構成を設定しています)
AliasMatch ^/([^/]+)/css/(.*)    /home/www/vhosts/nekojara.com/www/$1/css/$2
AliasMatch ^/([^/]+)/images/(.*) /home/www/vhosts/nekojara.com/www/$1/images/$2
AliasMatch ^/([^/]+)/(.*)        /home/www/vhosts/nekojara.com/www/$1/public_html/$2

SSL設定

OpenSSL運用 を参考にサーバー証明書とサーバー秘密鍵を生成し、Webサーバーに設定します。
# mkdir     /usr/local/apache2/conf/ssl.crt
# chmod 500 /usr/local/apache2/conf/ssl.crt
# mkdir     /usr/local/apache2/conf/ssl.key
# chmod 755 /usr/local/apache2/conf/ssl.key

     :
(サーバー証明書とサーバー秘密鍵を生成)
     :

# cp /home/ssl/server/web/server.crt /usr/local/apache2/conf/ssl.crt/
# cp /home/ssl/server/web/server.key /usr/local/apache2/conf/ssl.key/
http.conf の設定を行います。
# vi /usr/local/apache2/conf/http.conf

     :
# Secure (SSL/TLS) connections
#Include conf/extra/httpd-ssl.conf
Include conf/extra/httpd-ssl.conf                        SSL設定を有効にします
httpd-ssl.conf の設定を行います。
# vi /usr/local/apache2/conf/extra/httpd-ssl.conf

     :
<VirtualHost _default_:443>

#   General setup for the virtual host
DocumentRoot "/home/www/vhosts/system-act.com/ssl/public_html"
ServerName ssl.system-act.com                            仮想SSLホストのサーバ名を指定します
ServerAdmin 当該仮想SSLホストの管理者メールアドレス
ErrorLog "/var/log/httpd/vhosts/system-act.com/ssl/error_log"
                                                         エラーログファイルを変更しています
TransferLog "/var/log/httpd/vhosts/system-act.com/ssl/access_log"
                                                         アクセスログファイルを変更しています
     :
CustomLog "/var/log/httpd/vhosts/system-act.com/ssl/ssl_request_log" \
                                                         SSLリクエストログファイルを変更しています
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

Include conf/extra/vhosts/ssl.system-act.com.conf        ディレクトリ情報等は別ファイルにしています
</VirtualHost>

仮想ホスト ssl.system-act.com の設定ファイル ssl.system-act.com.conf を作成します。
# vi /usr/local/apache2/conf/extra/vhosts/ssl.system-act.com.conf

<IfModule mod_rewrite.c>
  RewriteEngine   On
  RewriteOptions  inherit
  RewriteLogLevel 0
</IfModule>

# (/css と /images は www.system-act.com と共有します)
Alias /css       "/home/www/vhosts/system-act.com/www/css"
Alias /images    "/home/www/vhosts/system-act.com/www/images"

起動

自動起動設定を行います。
# vi /etc/rc.d/init.d/httpd

#!/bin/sh
#
# Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#        HTML files and CGI.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /usr/local/apache2/conf/httpd.conf

# Source function library.
. /etc/rc.d/init.d/functions

# Check that networking is up.
if [ -f /etc/sysconfig/network ]; then
  . /etc/sysconfig/network
fi
[ ${NETWORKING} = "no" ] && exit 0

if [ -f /etc/sysconfig/httpd ]; then
  . /etc/sysconfig/httpd
fi

apachectl=/usr/local/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
prog="httpd"
RETVAL=0

# Seeemantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
  echo -n $"Starting $prog: "
  daemon $httpd $OPTIONS
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
  return $RETVAL
}

stop() {
  echo -n $"Stopping $prog: "
  killproc $httpd
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd /var/run/httpd.pid
}

reload() {
  echo -n $"Reloading $prog: "
  check13 || exit 1
  killproc $httpd -HUP
  RETVAL=$?
  echo
}

# See how we were called.
case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  status)
        status $httpd
  RETVAL=$?
  ;;
  restart)
  stop
  start
  ;;
  condrestart)
  if [ -f /var/run/httpd.pid ] ; then
    stop
    start
  fi
  ;;
  reload)
        reload
  ;;
  graceful|help|configtest|fullstatus)
  $apachectl $@
  RETVAL=$?
  ;;
  *)
  echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|
        graceful|help|configtest}"
  exit 1
esac

exit $RETVAL

# chmod 755 /etc/rc.d/init.d/httpd
# chkconfig httpd on
httpdを起動します。
# service httpd start
ファイアーウォール iptables の設定を追加します。
# vi /usr/local/sbin/firewall.sh
# --- (INPUTチェイン) ---# HTTP/HTTPS接続を許可
iptables -A INPUT -i $EXTIF -p tcp --dport  80 -j CHK-SYNFLOOD
iptables -A INPUT -i $EXTIF -p tcp --dport 443 -j CHK-SYNFLOOD# --- (OUTPUTチェイン) ---# HTTP/HTTPS接続を許可
iptables -A OUTPUT -o $EXTIF -p tcp --sport  80 -j ACCEPT
iptables -A OUTPUT -o $EXTIF -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
# firewall.sh                                            設定を反映させます
a System House to build an Accounting system by the Computer Technology