{{category OpenBSD,Apache HTTPd,nolink}}OpenBSDのサーバ httpd の設定 (2010-07-08 Apache 1.3.29 at OpenBSD 4.7 ベースに)
!!!webサーバ(httpd)の設定
'''/var/www/conf/httpd.conf''' に設定する。
*関連 man → [httpd(8)|http://www.openbsd.org/cgi-bin/man.cgi?query=httpd&sektion=8&arch=i386]
*関連 → [Apache HTTP Server Version 1.3|http://httpd.apache.org/docs/1.3/]
!!全般の設定
記述例がないディレクティブは、デフォルトの設定のままの項目。
[ServerType|http://httpd.apache.org/docs/1.3/mod/core.html#servertype] は、どのようにサーバを動かすかを指定する。
ServerTokens ProductOnly
[ServerTokens|http://httpd.apache.org/docs/1.3/mod/core.html#servertokens] は、クライアントに返すレスポンスヘッダに、サーバやモジュールの情報を含めるかを指定する。ProductOnly だと「Apache」のみが表示され、未指定かFullだとモジュールとバージョン情報も表示される。
ServerRoot "/var/www"
[ServerRoot|http://httpd.apache.org/docs/1.3/mod/core.html#serverroot] は、サーバの起点となるディレクトリを設定します。デフォルトではこのパスに chroot されて起動する。
[ResourceConfig|http://httpd.apache.org/docs/1.3/mod/core.html#resourceconfig] と [AccessConfig|http://httpd.apache.org/docs/1.3/mod/core.html#accessconfig] は、追加のディレクティブを記述したファイルを指定する。
[Timeout|http://httpd.apache.org/docs/1.3/mod/core.html#timeout] [KeepAliveTimeout|http://httpd.apache.org/docs/1.3/mod/core.html#keepalivetimeout] は、待ち時間の設定をする。
[KeepAlive|http://httpd.apache.org/docs/1.3/mod/core.html#keepalive] と [MaxKeepAliveRequests|http://httpd.apache.org/docs/1.3/mod/core.html#maxkeepaliverequests] は、複数のリクエストを同じ接続で接続するようにする。大量の画像を含むページでは遅延時間が短縮される(場合もある)。
[MinSpareServers|http://httpd.apache.org/docs/1.3/mod/core.html#minspareservers] [MaxSpareServers|http://httpd.apache.org/docs/1.3/mod/core.html#maxspareservers] [StartServers|http://httpd.apache.org/docs/1.3/mod/core.html#startservers] [MaxClients|http://httpd.apache.org/docs/1.3/mod/core.html#maxclients] は、子プロセス数の指定で、アイドル状態の最小数と最大数、起動時の作成数、最大で作成される数を指定する。
MaxRequestsPerChild 200
[MaxRequestsPerChild|http://httpd.apache.org/docs/1.3/mod/core.html#maxrequestsperchild] は、個々の子プロセスが処理するリクエスト数を指定する。
//MaxCPUPerChild MaxDATAPerChild MaxNOFILEPerChild MaxRSSPerChild MaxSTACKPerChild
[Include|http://httpd.apache.org/docs/1.3/mod/core.html#include] は、他の設定ファイルをインクルードする。
[LoadModule|http://httpd.apache.org/docs/1.3/mod/mod_so.html#loadmodule] は、指定したモジュールを動的に読み込み。
[ExtendedStatus|http://httpd.apache.org/docs/1.3/mod/mod_status.html#extendedstatus] は、拡張ステータス情報を保存する。
[Port|http://httpd.apache.org/docs/1.3/mod/core.html#port] および [Listen|http://httpd.apache.org/docs/1.3/mod/core.html#listen] は、Apache が listen するポートとアドレスを指定する。また、Port は SERVER_PORT 環境変数に設定される。 [BindAddress|http://httpd.apache.org/docs/1.3/mod/core.html#bindaddress] は、非推奨なので Listen を使用する。
[User|http://httpd.apache.org/docs/1.3/mod/core.html#user] [Group|http://httpd.apache.org/docs/1.3/mod/core.html#group] は、Apache が動くユーザとグループを指定する。
ServerAdmin webmaster@example.com
[ServerAdmin|http://httpd.apache.org/docs/1.3/mod/core.html#serveradmin] は、エラーなどで表示される管理者のメールアドレスを記載する。
ServerName www.example.com
[ServerName|http://httpd.apache.org/docs/1.3/mod/core.html#servername] は、サーバのホスト名をFQDNで指定する。
DocumentRoot "/var/www/htdocs"
[DocumentRoot|http://httpd.apache.org/docs/1.3/mod/core.html#documentroot] は、ドキュメントがあるルートのディレクトリを指定する。デフォルトでは ServerRoot に chroot されるので、その配下になるようにする必要がある。。
!!HTTPレスポンスヘッダの変更
レスポンスヘッダを変更するため、[mod_headers|http://httpd.apache.org/docs/1.3/mod/mod_headers.html] をロードするように設定する。コメントアウトされているので"#"を外せばよい。
LoadModule headers_module /usr/lib/apache/modules/mod_headers.so
Header set X-Frame-Options "SAMEORIGIN"
Header set X-Content-Type-Options "nosniff"
// 1分でわかる「X-ナントカ」HTTPレスポンスヘッダ (葉っぱ日記)
// http://d.hatena.ne.jp/hasegawayosuke/20110107/p1
!X-Frame-Options
コンテンツを他のフレーム内(FRAMEとIFRAMEタグ)で表示可能かどうかを指定する。'''DENY''' を指定するとフレームに埋め込んでコンテンツを表示することが出来なくなる。'''SAMEORIGIN''' を指定すると同じドメインの時のみフレームに埋め込んでコンテンツを表示することができる。
IE8 以降、Firefox 3.6.9以降、Chrome 4.1.249.1042以降、Safari 4.0以降、Opera 10.50以降 の各ブラウザで対応している。クリックジャック対策に有効。
!X-Content-Type-Options
'''nosniff''' を指定して IE8 以降においてコンテンツの内容からファイルタイプを推測する動作を抑制する。
IEで未知のコンテンツタイプの場合、コンテンツ内容で判断しようとするため、非HTMLがHTMLとして扱われXSSが発生する場合がある。これを防御するのに有効。
!!!用途別の設定
!!chrooted httpd
OpenBSDでは、デフォルトで ServerRoot に chroot して起動するようになっている。
!chroot の無効化
起動時に '''-u''' オプションを与えて起動すると chroot されない。
マシンの起動と同時に起動する場合は、/etc/rc.conf.local に記述を追加する。
httpd_flags="-u"
!!suEXEC の設定
suEXEC 機能を利用することで、CGIやSSIの実行を Apache を実行しているユーザではなく、スプリクトの所有者で実行することが出来る。
suEXEC 機能はデフォルトで無効になっており、また、動かすためには正しく設定する必要がある。スプリクトを動かすためには、スクリプトやディレクトリが存在し、他のユーザが書き込めるようになっていない。正当な一般ユーザ・グループで実行される(オーナがrootや一定のid以上で、setuid, setgid されていない)ようになっている必要がある。
*関連 man → [suexec(8)|http://www.openbsd.org/cgi-bin/man.cgi?query=suexec&sektion=8&arch=i386]
*関連 → Apache 1.3 [suEXEC サポート|http://httpd.apache.org/docs/1.3/suexec.html]
!suEXEC の有効化
OpenBSDでは、suEXEC は組み込まれているが、有効にはなっていない。以下のコマンドで確認する。
# httpd -l
Compiled-in modules:
http_core.c
:: (中略)
mod_ssl.c
suexec: disabled; invalid wrapper /usr/sbin/suexec
上記の状態では、suEXEC には対応しているが有効になっていない。ので、root で実行できるように setuid する。
# ls -l /usr/sbin/suexec
-r-xr-xr-x 1 root bin 12068 Sep 23 17:02 /usr/sbin/suexec
# chmod u+s /usr/sbin/suexec
# ls -l /usr/sbin/suexec
-r-sr-xr-x 1 root bin 12068 Sep 23 17:02 /usr/sbin/suexec
再起動すれば、有効になる。ちなみに「apachectl restart」や「kill HUP」などの単純な再起動では不十分。
# httpd -l
Compiled-in modules:
http_core.c
:: (中略)
mod_ssl.c
suexec: enabled; valid wrapper /usr/sbin/suexec
!suEXEC の無効化
suEXEC を無効にする場合は、setuid root しないか、suexec ファイルを削除(または移動)してから再起動する。
# chmod u-s /usr/sbin/suexec
!suEXEC の確認
以下の内容のファイルを suEXEC されるユーザディレクトリなどに置き、実行されるように設定してブラウザから呼び出す。実行されるユーザが表示される。
#!/usr/bin/perl
print "Content-type: text/plain\n\n";
print `whoami`;
「500 Internal Server Error」となる場合、httpd のログ(/var/www/logs/error_log) に「Premature end of script headers」が出力される。また、suexec のログ(/var/log/suexec_log) に詳しい内容が出力される。
suEXEC がどのように設定されているかは、suexec -V コマンドで確認できる
# /usr/sbin/suexec -V
-D DOC_ROOT="/var/www/htdocs"
-D GID_MIN=1000
-D HTTPD_USER="www"
-D LOG_EXEC="/var/log/suexec_log"
-D SAFE_PATH="/usr/bin:/bin:/usr/local/bin"
-D UID_MIN=1000
-D USERDIR_SUFFIX="public_html"
!!SSL の設定
Apache 2.0 からは、Apache の一部として配布されるが、1.3 は別プロジェクトとなる。OpenBSDでは、mod_ssl は組み込まれているがドキュメントは、[mod_ssl|http://www.modssl.org/] を参照する。
!秘密鍵、証明書の作成
秘密鍵 /etc/ssl/private/server.key を作成 (RSA 1024 bit)
openssl genrsa -out /etc/ssl/private/server.key 1024
証明書要求 /etc/ssl/server.csr を作成
openssl req -new -key /etc/ssl/private/server.key -out /etc/ssl/server.csr
秘密鍵と証明書要求から自己署名証明書 /etc/ssl/server.crt を作成 (4年間(1461日間)有効)
openssl req -in /etc/ssl/server.csr -key /etc/ssl/private/server.key -x509 -days 1461 -out /etc/ssl/server.crt
※認証局に署名してしてもらう場合は、証明書要求を置くって、署名された証明書を /etc/ssl/server.crt に保存する。
# openssl genrsa -out /etc/ssl/private/server.key 1024
Generating RSA private key, 1024 bit long modulus
..............++++++
.......................++++++
e is 65537 (0x10001)
# openssl req -new -key /etc/ssl/private/server.key -out /etc/ssl/server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) []:
Organization Name (eg, company) []:
Organizational Unit Name (eg, section) []:
Common Name (eg, fully qualified host name) []:apis.jpn.ph
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
# openssl req -in /etc/ssl/server.csr -key /etc/ssl/private/server.key -x509 -days 1461 -out /etc/ssl/server.crt
# ls -lF /etc/ssl/server.csr /etc/ssl/server.crt /etc/ssl/private/server.key
-rw-r--r-- 1 root wheel 887 Oct 12 23:19 /etc/ssl/private/server.key
-rw-r--r-- 1 root wheel 810 Oct 12 23:20 /etc/ssl/server.crt
-rw-r--r-- 1 root wheel 631 Oct 12 23:19 /etc/ssl/server.csr
#
!サーバの設定
設定ファイルの httpd.conf に ディレクティブがあるので適切に変更する。
DocumentRoot ServerName ServerAdmin ErrorLog TransferLog あたりの設定は、SSLの時に別の設定をしないのであれば、特に設定しなくてもよい。
「SSLEngine on」としSSLを有効にする。SSLCipherSuite で許可する暗号方式を指定する(デフォルトでは40bitや56bitも許可されてるので、可能なら強度の高い方法に制限した方がいいかな)。
SSLCertificateFile で秘密鍵、SSLCertificateKeyFile で証明書を指定する。また、中間証明書を利用する場合は SSLCertificateChainFile で中間証明書を指定する。
# SSL Engine Switch:
SSLEngine on
# SSL Cipher Suite:
SSLCipherSuite ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP
# Server Certificate:
SSLCertificateFile /etc/ssl/server.crt
# Server Private Key:
SSLCertificateKeyFile /etc/ssl/private/server.key
# Server CA Certificates:
#SSLCertificateChainFile /var/www/conf/ssl.crt/ca.crt
# Per-Server Logging:
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
!サーバの起動
コマンドラインから起動する場合は、次のようにするとSSLが有効になる。
# apachectl sslstart
マシンの起動と同時に起動する場合は、/etc/rc.conf.local に記述を追加する。
httpd_flags="-DSSL"