manekineko倉金家ホームページ

趣味の部屋/インターネットサーバー/サーバー構築メモ その2

httpサーバーapache2

2010年7月17日(土) 2010年7月28日(水)更新
ホームページを表示するためのhttpサーバーを設定します。


基本設定1
/etc/httpd/conf.d に追加機能やモジュールの設定ファイルがあります。
必要のないものはファイル名を xxxx.conf-KILL とでもしておきます。
こうすれば読み込まれません。(READMEにそう書いてある。)
manual.conf-KILL … 日本語のがないんだもん。
proxy_ajp.conf-KILL … proxy機能は不要。
squid.conf-KILL … 個人的な使用ではあまりメリットがあるとは思えない。
といった具合。
manualなど公開する必要もありませんし、実行にmoduleが必要なサービスについては少しはリソースの無駄を低減できるでしょう。

基本設定2
/etc/httpd/conf/httpd.conf を編集します。
そのままでも動かないことはないようですが、好みで。
おやじさんのサイト http://www.aconus.com/~oyaji/centos/apache_centos.htm を参考にさせて頂きました。いつもお世話になっております。

(変更したところのみ記載)
KeepAlive On
# 使わないModuleは無効化(設定1ともからめて)
......
#LoadModule authn_anon_module modules/mod_authn_anon.so
......
#LoadModule ldap_module modules/mod_ldap.so
#LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
......
#LoadModule status_module modules/mod_status.so
......
#LoadModule userdir_module modules/mod_userdir.so
......
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
......
DirectoryIndex index.html index.htm index.html.var
… index.htm を追加。
HostnameLookups On … ログに記載のため。少し遅くなるけど。
LogLevel notice … 設定が完了したらwarnに戻してよい。
LanguagePriority ja en ca cs da de .... … jaを先頭に移動。
#AddDefaultCharset UTF-8
… すでにEUC-JP, Shift-JIS等混在しているので各ページで指定することに。
<IfModule mod_negotiation.c>
<IfModule mod_include.c>
<Directory "/var/www/error">
......
LanguagePriority ja en es de fr … jaを先頭に。
......
</Directory>
# 以下すべて無効になっていたのをすべて有効に変更
ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
ErrorDocument 410 /error/HTTP_GONE.html.var
ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var
</IfModule>
</IfModule>
.....
そして最後に
Include conf/vhosts.conf
を書きます。
#apachectl configtest で設定にまちがいがないか確認。
Syntax OK とくればへんな設定ミスはなさそう。
これをやるくせをつけておけばいきなり再起動できずにあわてることはなくなります。(スペルミスがよくあるんだわ。)

サイトの設定
/home ディレクトリにウェブコンテンツを入れる場所をつくります。
私の場合は /home/http としました。
中にとりあえず index.html ファイルをつくり、Hello! とでも書いておきます。

自前のサイト設定はわかりやすいようにhttpd.confとは別ファイルにします。
/etc/httpd/conf/vhosts.confをつくり、以下のようなことを記述。
ServerName jiji.iroribata.net …DNSに設定したサーバーのホスト名。127.0.0.1とかIPアドレスでもいいみたい。
NameVirtualHost *:80
<Directory /home/http>
  Options FollowSymLinks
  Order allow,deny
  Allow frm all
</Directory>
<VirtualHost *:80>
  ServerName iroribata.net
  DocumentRoot /home/http
  ServerAdmin donbei@iroribata.net
  ErrorLog logs/error_log
  CustomLog logs/access_log common
</VirtualHost>
保存してhttpdを再起動し、ブラウザで http://localhost/ を開いて index.html の内容が表示されればOK...なのですが、やっぱり SELinux さんにおこられました。表示ディレクトリを /home/http にしたのがいけないようです。

SELinux
ホームページのファイルは時々刻々変化し、都度いちいち設定はしていられませんので、
SELinux Management(SELinux Administration) で
Boolean ⇒ HTTPD Service の
Disable SELinux protection for http suexec
Disable SELinux protection for httpd daemon
にチェックし、httpdサーバーのアクセスは無条件に許可するようにしてコンピュータを再起動。(Relabelまではやる必要はないようです。)
今度は問題なく表示されました。

暗号化通信SSLの設定
別に暗号化しなくてはならない内容など何もないし、すでにSSLモジュール mod_ssl は標準で組み込まれていますし、仮の証明書も入ってそのままSSLを受け付ける状態になってはいるようです。
単にやってみたかったというだけの話。

付属のシェルスクリプトを使う方法、makefileを使う方法、直接opensslコマンドを打つ方法などいろいろやりかたがあるようですが、今回はCAというシェルスクリプトを使うやりかたでやってみます。

SSL証明書関係のディレクトリ作成。
/var/www/ssl
(ディフォルトのままでもいいけどわかりにくいので。)

/etc/pki/tls/openssl.cnf を編集
[ CA_default ]
dir = /var/www/ssl       # Where everything is kept
default_days    = 9125   # how long to certify for
default_crl_days= 300    # how long before next CRL
...期限延長をあまり欲張ってやるとかえって過去の日時になってしまう。
/etc/pki/tls/misc/CA を /var/www/ssl にコピーし、書き換えます。
DAYS="-days 9125" # 25 years(こちらで指定すればopnssl.confのほうはいらないのかも)
CADAYS="-days 9125" # 25 years
CATOP=./myCA

#cd /var/www/ssl して、
# ./CA -newca
CA certificate filename (or enter to create)

Making CA certificate ...
Generating a 1024 bit RSA private key
.......................++++++
..............++++++
writing new private key to './myCA/private/./cakey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
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) [GB]:JP …以下それらしく
State or Province Name (full name) [Berkshire]:Kanagawa
Locality Name (eg, city) [Newbury]:Yugawara-Town
Organization Name (eg, company) [My Company Ltd]:iroribata.net
Organizational Unit Name (eg, section) []:Iroriban
Common Name (eg, your name or your server's hostname) []:jiji.iroribata.net
Email Address []:donbei@iroribata.net

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: …空で
An optional company name []: …空で
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for ./myCA/private/./cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 0 (0x0)
Validity
Not Before: Jul 28 12:20:43 2010 GMT
Not After : Jul 22 12:20:43 2035 GMT
Subject:
countryName = JP
stateOrProvinceName = Kanagawa
organizationName = iroribata.net
organizationalUnitName = Iroriban
commonName = jiji.iroribata.net
emailAddress = donbei@iroribata.net
X509v3 extensions:
X509v3 Basic Constraints:
CA:TRUE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
17:2E:DC:C4:E9:E6:AB:B1:99:00:7B:1C:E5:FF:87:47:35:E9:43:9E
X509v3 Authority Key Identifier:
keyid:17:2E:DC:C4:E9:E6:AB:B1:99:00:7B:1C:E5:FF:87:47:35:E9:43:9E

Certificate is to be certified until Jul 22 12:20:43 2035 GMT (9125 days)

Write out database with 1 new entries
Data Base Updated
/var/www/ssl/myCA/private に秘密鍵ファイル cakey.pem が、
/var/www/ssl/myCA に認証ファイル cacert.pem ができます。

起動時いちいち入力しなくてよいように秘密鍵ファイルのパスワードを取り除いておきます。
#cd /var/www/ssl/myCA/private
#openssl rsa -in cakey.pem -out cakey.pem
やはり暗号化サイトともなると別管理しなくてはならないような気がして、SSL用のホームディレクトリ /home/https をつくります。
まずはてきとうなことを書いた index.html を置いておきます。

以上の設定で /etc/httpd/conf.d/ssl.conf を編集。変更点のみ。
<VirtualHost _default_:443>の前後。
......
<Directory /home/https>
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
<VirtualHost _default_:443>
DocumentRoot /home/https
ServerName jiji.iroribata.net:443
ServerAdmin root@localhost
ErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel error … warnだとちゃんとしたCAかといった警告が出る。
......
SSLCertificateFile /var/www/ssl/myCA/cacert.pem
SSLCertificateKeyFile /var/www/ssl/myCA/private/cakey.pem
......

いつものようにチェックし、httpdを再起動、表示してみます。
#apachectl configtest
Syntax OK
#service httpd restart
httpdを停止中:     [OK]
httpdを停止中:     [OK]
https://... でSSLアクセスをしてみると、やっぱり証明書が信用できないという警告が出ます。しかも言い方が昔よりきつくなっています。
単に暗号化通信だけしたいんだけど、なんで認証まで一緒にからませるのかな。
お金払ってまで認証してもらうつもりもないし、あまり利用価値はないな。
単にやってみたというだけでおしまい。