[Memo2008][EPA]tako作業メモ(中本)(080321)

apache2 のインストール (2008/03/21(金))

パスの設定

インストールされた Apache のバイナリファイル群と man へのパスを通す.

(システム用コマンドも一般ユーザ用コマンドと同じ場所にインストールさ れているので, 一般ユーザ用コマンドだけ設定を行う.)

sh, bash のパスの設定

/etc/bash.bashrc の冒頭にある

# add PATH for local installed softwares
PATH="${PATH}:<一般ユーザ用コマンドのパス>"

の <一般ユーザ用コマンドパス> に /usr/local/apache2/bin を追加する.

csh, tcsh のパスの設定

/etc/csh.cshrc の冒頭にある

# add PATH for local installed softwares
set path = ($path /usr/local/bin /usr/bin /bin <一般ユーザ用コマンドのパス>)

の <一般ユーザ用コマンドのパス> に /usr/local/apache2/bin を追加する.

zsh のパスの設定

/etc/zsh/zshenv の末尾にある

# add PATH for local installed softwares
export PATH=<一般ユーザ用コマンドのパス>:$PATH

の <一般ユーザ用コマンドのパス> に /usr/local/apache2/bin を追加する.

man 関連のパスの設定

/etc/manpath.config の適切な場所に

MANDATORY_MANPATH                          /usr/local/apache2/man
MANPATH_MAP        /usr/local/apache2/bin  /usr/local/apache2/man
MANDB_MAP          /usr/local/apache2/man  /usr/local/apache2/man

の 3 行を追加する.

設定ファイルの編集

以下の 3 つの設定ファイルを編集する.

  • httpd 用: /usr/local/apache2/conf/httpd.conf
  • 各ユーザの public_html 以下の設定用: /usr/local/apache2/conf/extra/httpd-userdir.conf
  • httpd-ssl 用: /usr/local/apache2/conf/extra/httpd-ssl.conf

/usr/local/apache2/conf/httpd.conf の編集

  • HTTP デーモンを起動するユーザを www-data にする.

    65, 66 行目を

    User www-data
    Group www-data

    に変更する.

  • サーバ管理者 (ServerAdmin) のメールアドレスを設定する.

    88 行目を

    ServerAdmin epaadmin_at_epa.scitec.kobe-u.ac.jp

    に変更する.

  • サーバ名 (ServerName) を設定

    97 行目を

    ServerName epa.scitec.kobe-u.ac.jp:80

    に変更する.

  • サーバのデフォルトルート (DefaultRoot) を /~epalab に設定

    104, 105 行目を

    #DocumentRoot "/usr/local/apache2/htdocs"
    DocumentRoot "/home/epalab/public_html"

    に変更する.

  • ディレクトリのインデックスファイルに index.html 以外に index.htm, index.htm.en, index.html.en, index.htm.ja, index.html.ja も追加.

    119 行目を

    #    Deny from all

    に変更し, 132, 133 行目を

    #<Directory "/usr/local/apache2/htdocs">
    <Directory "/home/epalab/public_html">

    に変更し, 168 行目を

    DirectoryIndex index.html index.htm index.htm.en index.html.en index.htm.ja index.html.ja

    に変更する.

  • エラーログファイルを /var/log/httpd-error.log に設定

    188 行目を

    ErrorLog "/var/log/httpd-error.log"

    に変更する.

  • アクセスログファイルを /var/log/httpd-access.log に設定

    217 行目を

    #CustomLog "logs/access_log" common

    に変更し, 223 行目を

    CustomLog /var/log/httpd-access.log combined

    に変更する.

  • CGI に関する設定を変更

    270 〜 273 行目を

    AllowOverride AuthConfig Limit
    Options ExcCGI FollowSymLinks IncludesNoExec
    # order allow,deny
    # Allow from all

    に変更する.

  • extra/httpd-userdir.conf をロード

    384 行目を

    Include conf/extra/httpd-userdir.conf

    に変更する.

  • extra/httpd-ssl.conf をロード

    402 行目を

    Include conf/extra/httpd-ssl.conf

    に変更する.

  • /~gate, および /cgi-bin に対してのアクセスは HTTPS へ移動させる (参考資料 9, 10 参照).

    413 〜 432 行目に

    #
    # Settings for gate-toroku-system
    #
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteLog "/var/log/httpd-rewrite.log"
        RewriteLogLevel 0
    
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/~gate/(.*)?$ https://%{HTTP_HOST}/~gate/$1 [L,R]
    </IfModule>
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteLog "/var/log/httpd-rewrite.log"
        RewriteLogLevel 0
    
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/cgi-bin/(.*)?$ https://%{HTTP_HOST}/cgi-bin/$1 [L,R]
    </IfModule>

    を追加する.

  • ~epalab/hiki と ~itpass/hiki に関しての設定を各ディレクトリごとの .htaccess で可能とする (AllowOverride)

    434 〜 443 行目に

    #
    # Settings for Hiki Area
    #
    <Directory /home/epalab/public_html/hiki >
        AllowOverride ALL
    </Directory>
    
    <Directory /home/itpass/public_html/hiki >
        AllowOverride ALL
    </Directory>

    を追加する.

  • /hiki, /~epalab/hiki, /~itpass/hiki に対してのアクセスは HTTPS へ移動させる. その際, URI のエンコードは行わない. (参考資料 9, 10, 11 参照).

    445 〜 470 行目に

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteLog "/var/log/httpd-rewrite.log"
        RewriteLogLevel 0
    
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/hiki/(.*)?$ https://%{HTTP_HOST}/hiki/$1 [L,R]
    </IfModule>
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteLog "/var/log/httpd-rewrite.log"
        RewriteLogLevel 0
    
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/~epalab/hiki/(.*)?$ https://%{HTTP_HOST}/~epalab/hiki/$1 [L,R,NE]
    </IfModule>
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteLog "/var/log/httpd-rewrite.log"
        RewriteLogLevel 0
    
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/~itpass/hiki/(.*)?$ https://%{HTTP_HOST}/~itpass/hiki/$1 [L,R,NE]
    </IfModule>

    を追加する.

/usr/local/apache2/conf/extra/httpd-userdir.conf の編集

  • 所有者がマッチしなくてもシンボリックリンクの公開を許可する. (SymLinksIfOwnerMatch -> FollowSymLinks).

    18 行目を

    Options MultiViews Indexes FollowSymLinks IncludesNoExec

    に変更する.

/usr/local/apache2/conf/extra/httpd-ssl.conf の編集

  • ドキュメントルート (DocumentRoot), サーバ名 (ServerName), サーバ管理者アドレス (ServerAdmin), エラーログ (ErrorLog), アクセスログ (TransferLog) を設定する.

    77 〜 81 行目を

    DocumentRoot "/home/epalab/public_html"
    ServerName epa.scitec.kobe-u.ac.jp:443
    ServerAdmin epaadmin_at_epa.scitec.kobe-u.ac.jp
    ErrorLog /var/log/httpd-ssl-error.log
    TransferLog /var/log/httpd-ssl-access.log

    に変更する.

ログローテートの設定

/etc/logrotate.conf の編集

12 行目の

#compress

compress

に変更する.

/etc/logrotate.d/apache2 の作成

/etc/logrotate.d/ に apache2 というファイルを置き, 以下の内容を書き込む.

/var/log/httpd-access.log {
    missingok
    rotate 12
    postrotate
        /usr/bin/killall -HUP httpd 2> /dev/null || true
    endscript
}

/var/log/httpd-error.log {
    missingok
    rotate 12
    postrotate
        /usr/bin/killall -HUP httpd 2> /dev/null || true
    endscript
}

/var/log/httpd-ssl-access.log {
    missingok
    rotate 12
    postrotate
        /usr/bin/killall -HUP httpd 2> /dev/null || true
    endscript
}

/var/log/httpd-ssl-error.log {
    missingok
    rotate 12
    postrotate
        /usr/bin/killall -HUP httpd 2> /dev/null || true
    endscript
}

SSL 用の証明書の作成

openssl の設定の変更

/etc/ssl/openssl.cnf の 172 行目を

nsCertType = server

に変更し, 241 行目を

nsCertType = sslCA, emailCA

に変更する.

証明書の作成

まず, /usr/local/apache2/conf に移動する.

# cd /usr/local/apache2/conf

次に, 以下のコマンドで CA 用秘密鍵 (cakey.pem) と CA 用証明書 (cacert.pem) を作成する.

# /usr/lib/ssl/misc/CA.sh -newca

ここで, 次々に問われるので以下のように入力する.

CA certificate filename (or enter to create): 何も入力せずに enter
Enter PEM pass phrase: rere

Country Name (2 letter code) [AU]: JP
State or Province Name [Some-State]: Hyogo
Locality Name (eg, city) []: Kobe
Organization Name (eg, company) []: Kobe University
Organizational Unit Name (eg, section) []: EPA lab.
Common Name: epa.scitec.kobe-u.ac.jp
Email Address: epaadmin_at_epa.scitec.kobe-u.ac.jp
A challenge password: 何も入力せずに enter
A optional company name: 何も入力せずに enter

Enter pass phrase for ./demoCA/private/./cakey.pem: rere

この作業によって, 以下のようなディレクトリとファイルが作成される.

/usr/local/apache2/conf/
                    |
                    └ demoCA [ 各種証明書等のルートディレクトリ ]
                        |
                        ├ cacert.pem [ CA 用証明書 ]
                        ├ careq.pem
                        |
                        ├ certs [ 証明書等のディレクトリ (バックアップに利用) ]
                        |
                        ├ crl [ 破棄証明書一覧用のディレクトリ ]
                        |
                        ├ index.txt     [ クライアント証明書用 DB ]
                        ├ index.txt.attr
                        ├ index.txt.old
                        |
                        ├ newcerts [ クライアント証明書 (sireal 追番) のディレクトリ ]
                        |    |
                        |    ├ xxxxx..pem [ クライアント証明書 ]
                        |    |     :
                        |    └ xxxxx..pem [ クライアント証明書 ]
                        |
                        ├ private [ CA 用の秘密鍵用ディレクトリ ]
                        |   |
                        |   └ cakey.pem [ CA 用の秘密鍵 ]
                        |
                        ├ serial [ クライアント証明書用シリアル ]
                        └ serial.old

秘密鍵を他人に見られてはいけないので, パーミションを以下のように変更しておく.

# chmod 600 ./demoCA/private/cakey.pem
# chmod 700 ./demoCA/private

このようにして作成した鍵を Apache で使用すると, Apacheの起動時に毎回パスフレーズを聞かれる. 自動起動させるために, 以下のコマンドで鍵からパスフレーズを削除する. (もちろんセキュリティ上は毎回パスフレーズを入力する方が望ましい)

# openssl rsa -in ./demoCA/private/cakey.pem -out ./demoCA/private/cakey.pem
Enter pass phrase for ./demoCA/private/./cakey.pem: rere

次に, 以下のコマンドでサーバ証明書 (server.crt) を作成する.

# openssl x509 -in ./demoCA/cacert.pem -out ./demoCA/server.crt

次に, CA 証明書 (cacert.pem) をブラウザに登録するために, 以下のコマンドで DER フォーマットで記述された証明書 (ca.der) を作成する.

# openssl x509 -inform pem -in ./demoCA/cacert.pem -outform der -out ./demoCA/ca.der

これらの作業により, demoCA/ 以下に server.crt と ca.der が生成される. これらのファイルをカレントディレクトリである /usr/local/apache2/conf にコピーする.

# cp ./demoCA/server.crt .
# cp ./demoCA/ca.der .

次に, CA 用秘密鍵 (cakey.pem) をコピーしてサーバ用秘密鍵 (server.key) を作成する.

# cp ./demoCA/private/cakey.pem ./server.key

これにて完了.

動作チェック

apache は /usr/local/apache2/bin/apachectl で起動/停止する. 以下のコマンドで apache を起動する.

# /usr/local/apache2/bin/apachectl -k start

停止するには以下のようにすると良い.

# /usr/local/apache2/bin/apachectl -k graceful-stop

ちなみに, 現時点 (2008/03/27) では, /usr/local/apache2/conf/httpd.conf の DocumentRoot が /home/epalab/public_html となっているが, そんなディレクトリはまだ存在しないので, Apache の起動はできない.

そこで, /home/chikuwa2/public_html/index.html なるファイルを作り, /usr/local/apache2/conf/httpd.conf の中身を書き換えてやると, Apache が起動でき, ブラウザで

http://133.30.109.21 (tako の IP アドレス)

を開くとちゃんと見れた.

apache2 のインストール (2008/03/28(金))

起動スクリプトを用意する

サーバの起動時に自動的に Apache を起動させるため, apache2 の起動スクリプト を用意する.

まず, Debian の apache パッケージに含まれる /etc/init.d/apache を元に起動スクリプトを apache2 用に書き換えたファイル (apache2) を入手する.

# wget http://epa.scitec.kobe-u.ac.jp/~itpass/epa/apache/apache2

このファイルを /etc/init.d/apache2 にコピーし, 以下のようにパーミションを変更する.

# cp apache2 /etc/init.d/
# chown root:root /etc/init.d/apache2
# chmod 755 /etc/init.d/apache2

次に, ランレベルごとに apache2 の動作を設定する.

# update-rc.d apache2 defaults

そしてサーバを再起動し, 起動時に apache が起動することを確認する.

# shutdown -r now

ブラウザで

http://tako.epa.scitec.kobe-u.ac.jp

を開くとちゃんと見れた.

Last modified:2008/09/25 16:06:25
Keyword(s):
References:[[EPA2008]2008年度前期サーバ構築ログ]