| apache2.x&mod_sslインストール(openssl) |
今回のインストールではapacheのインストールと
mod_sslによるssl化でhttpsのアクセスを行えるようにします。
| www:x:508:503:apache_user:/home/www:/bin/false |
| 指定無しの場合 | --enable-mods=mostの場合 | --enable-mods=allの場合 | 上記のconfigureの場合 |
| core.c mod_access.c mod_auth.c mod_include.c mod_log_config.c mod_env.c mod_setenvif.c prefork.c http_core.c mod_mime.c mod_status.c mod_autoindex.c mod_asis.c mod_cgi.c mod_negotiation.c mod_dir.c mod_imap.c mod_actions.c mod_userdir.c mod_alias.c mod_so.c |
core.c prefork.c http_core.c mod_so.c |
core.c mod_access.c mod_auth.c mod_include.c mod_log_config.c mod_env.c mod_setenvif.c prefork.c http_core.c mod_mime.c mod_status.c mod_autoindex.c mod_asis.c mod_cgi.c mod_negotiation.c mod_dir.c mod_imap.c mod_actions.c mod_userdir.c mod_alias.c mod_so.c |
core.c mod_access.c mod_auth.c mod_include.c mod_log_config.c mod_env.c mod_setenvif.c mod_ssl.c worker.c http_core.c mod_mime.c mod_dav.c mod_status.c mod_autoindex.c mod_asis.c mod_cgid.c mod_dav_fs.c mod_negotiation.c mod_dir.c mod_imap.c mod_actions.c mod_userdir.c mod_alias.c mod_rewrite.c mod_so.c |
-randオプションに/usr/local/apache2/conf/httpd.confを指定していますが
ある程度容量のあるテキストを指定します。
--SSL化をするための作業--
●プライベートCA作成のための準備作業
# cd /usr/local/ssl
# cp -r /work/openssl/openssl-0.9.7/apps/demoCA /usr/local/ssl/CA
# chmod 700 CA
# cd CA
# mv cacert.pem cacert.pem.org
# cd private/
# mv cakey.pem cakey.pem.org
# cp /usr/local/ssl/openssl.cnf /usr/local/ssl/CA/
# cd /usr/local/ssl/CA/
# vi openssl.cnf
diffの結果参照
[root@home5 CA]# diff openssl.cnf ../openssl.cnf
37c37
< dir = /usr/local/ssl/CA # Where everything
is kept
---
> dir = ./demoCA # Where everything is
kept
●プライベートCAキーの作成
# cd /usr/local/ssl/CA/private/
# openssl genrsa -des3 -out cakey.pem 1024
Generating RSA private key, 1024 bit long
modulus
............++++++
.....++++++
e is 65537 (0x10001)
Enter PEM pass phrase:password
Verifying password - Enter PEM pass phrase:pasword
●プライベートCA用の証明書作成(CA用のもの)
# cd ../../CA
# openssl req -new -x509 -key private/cakey.pem
-out cacert.pem
Using configuration from /usr/local/ssl/openssl.cnf
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.
---↓プライベートCAは適当に値を入れても問題ありません
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Sumida
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Demo CA.inc
Organizational Unit Name (eg, section) []:Democa
Common Name (eg, YOUR name) []:CA dept
Email Address []:info@democa.com
●サーバーキーの作成
# /usr/local/ssl/bin/openssl genrsa -des3 -rand
/var/tmp/ -out /usr/local/ssl/home5-key.pem
1024
0 semi-random bytes loaded
Generating RSA private key, 1024 bit long
modulus
..........++++++
...........................................++++++
e is 65537 (0x10001)
Enter PEM pass phrase:パスワード
Verifying password - Enter PEM pass phrase:もう一回パスワード
apapcheをssl化すると毎回プロセスの起動時にパスワード入力を求められます。
パスワード入力なしで起動するために以下の作業をしておきます。
もし毎回パスワードを入力する場合は必要ありません。
# openssl rsa -in /usr/local/ssl/home5-key.pem
-out /usr/local/ssl/home5-key.pem
read RSA key
Enter PEM pass phrase:パスワード
writing RSA key
●CSR (SSL Certificate Signing Request)証明書署名リクエストの作成
# /usr/local/ssl/bin/openssl req -new -key
/usr/local/ssl/home5-key.pem -out home5-csr.pem
Using configuration from /usr/local/ssl/openssl.cnf
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) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Chiyoda
Organization Name (eg, company) [Internet Widgits Pty Ltd]:UNIX-STUDY
Organizational Unit Name (eg, section) []:Sales
Common Name (eg, YOUR name) []:unix-study.com
Email Address []:info@unix-study.com
※CSR作成時にcommon Nameは必ず自分のサイトのURLのホスト名にしておきましょう
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:password
An optional company name []:password
●CSRを署名してサーバ証明書作成
# mkdir /usr/local/ssl/CA/newcerts (newcertsディレクトリがないとエラーがでるので)
# openssl ca -config ./openssl.cnf -policy
policy_anything -in ../home5-csr.pem -out
home5-cert.pem
Using configuration from ./openssl.cnf
Enter PEM pass phrase:password
Check that the request matches the signature
Signature ok
The Subjects Distinguished Name is as follows
countryName :PRINTABLE:'JP'
stateOrProvinceName :PRINTABLE:'Toyko'
localityName :PRINTABLE:'Chiyoda'
organizationName :PRINTABLE:'UNIX-STUDY'
commonName :PRINTABLE:'Sales'
emailAddress :IA5STRING:'info@unix-study.com'
Certificate is to be certified until Jul
7 14:16:30 2002 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
#
●ルート証明書をDER形式で作成配布するために用意
CAの証明書をあらかじめFirefoxやInternetExplorerに組み込むにはCAのCRTをDER形式に変更為ておく。
# openssl x509 -in cacert.pem -outform der -out cacert.der
もしくは
# /usr/local/ssl/bin/openssl pkcs12 -export -inkey private/cakey.pem -in cacert.pem -out cacert.p12
Enter pass phrase for private/cakey.pem:
Enter Export Password:password
Verifying - Enter Export Password:password
Firefoxはpkcs12形式でないとインポートできません
cadert.derをWWWサーバで公開する場合は
cacert.derをmimeタイプに追加しておく
# vi /usr/local/apache2/conf/httpd.conf
| #Added for cacert.der AddType application/x-x509-ca-cert der |
IEの場合
「ツール」→「インターネットオプション」
1.コンテンツタブの証明書クリック

2.信頼されたルート証明書クリック→インポート

3.次へクリック

4.参照クリック

5.cacert.p12を選択

6.次へクリック

7.次へクリック(バックアップが必要な場合はチェック)

8.次へクリック

9.完了クリック

【設定】
●apacheの公開ディレクトリ
ssl関係のファイルは/usr/local/apache2/conf/配下に配置(任意)
# mkdir /var/log/httpd
# chown www:www /var/log/httpd
# chmod 700 /var/log/httpd
・ssl関係のファイルをコピーしておく
# cp /usr/local/ssl/home5-* /usr/local/apache2/conf/
# cp home5-cert.pem /usr/local/apache2/conf/
# vi httpd.conf
httpd.confファイルは下記参照
/usr/local/apache2/conf/httpd.conf
※あとでいろいろ変更したのでちょっとディレクトリなど変わってます。
ちょっとした小技ですがディレクトリの中身を表示させたいときに
「FancyIndexing」を使うと思いますがこのときに長いファイル名は
デフォルトでは途中で切れて見えなくなってしまいます。
そこで、どうしてもファイル名を全部表示させたいと思ったときに
「NameWidth=*」をつければファイル名の長さに応じてファイル名フィールドが
広がります。設定は以下の一行のような感じです。
※FancyIndexingなんかで検索してその後ろに「NameWidth=*」をつけます。
*のところは数字で固定の長さに指定することも可能です。
| IndexOptions FancyIndexing NameWidth=* |
【起動】
# cd /usr/local/apache2/bin/apachectl configtest
Syntax OK
# cd /usr/local/apache2/bin/apachectl start
apache2.xで起動したときはまったくメッセージが出力されない。よって必ず
# tail -f /usr/local/apache2/logs/error.log
# ps -ef|grep http
として起動の確認をすること
ブラウザから
https://webservername/
http://webservername/
にアクセスしてちゃんと動いているかどうか確認
自動起動するようにしておく。(以下ディレクトリの構成は自分の環境に合わせて読み替えてほしい。)
実際にはlinuxとsolarisでやったがログはとりあえずlinuxのものを使わせてもらう。
# cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
# chown root:sys /etc/rc.d/init.d/httpd
# ln -s /etc/rc.d/init.d/httpd /etc/rc5.d/S99httpd
その他すべてのrc.Xにシンボリックを張っておく。(ただしSolarisでは/etc/rc2.d以下だけでよい)
【apacheのベンチマーク】
# ./ab -n 1000 -c 5 http://URL/
(100,000回のアクセスを500個ずつ200回行うということ)
| This is ApacheBench, Version 2.0.44-dev <$Revision: 1.105 $> apache-2.0 Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.128.13 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Finished 1000 requests Server Software: Apache/2.0.44 #ターゲットのApacheのバージョン Server Hostname: 192.168.128.13 #ホスト名 Server Port: 80 #相手のhttpポート Document Path: / #リクエストしたパス Document Length: 67819 bytes #ドキュメントの容量 Concurrency Level: 5 Time taken for tests: 4.97122 seconds #ベンチにかかった時間 Complete requests: 1000 #リクエストが完了した数 Failed requests: 0 #リクエストの失敗した数 Write errors: 0 #ライトエラー数 Total transferred: 68106000 bytes #転送総容量 HTML transferred: 67819000 bytes #転送したhtmlだけの容量 Requests per second: 244.07 [#/sec] (mean) #一秒間のリクエスト処理数(この数字結構重要) Time per request: 20.486 [ms] (mean) Time per request: 4.097 [ms] (mean, across all concurrent requests) #同時に処理したリクエスト/ms Transfer rate: 16233.10 [Kbytes/sec] received #一秒間の転送容量 Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.4 0 41 Processing: 5 19 9.7 17 124 Waiting: 0 4 5.2 3 84 Total: 5 19 9.8 17 124 Percentage of the requests served within a certain time (ms) 50% 17 66% 18 75% 19 80% 19 90% 20 95% 23 98% 57 99% 58 100% 124 (longest request) |
| #! /bin/sh # # httpd access log rotation shell program # This script requires shutils(GNU). # かならずshutilsをインストールしてください。 # /usr/local/dateコマンドを使うため。 # Last modified 2003.Mar.04 #------Modify for your enviroment--- TARDIR='/usr/local/apache2/logs/oldlogs/'; SRCDIR='/usr/local/apache2/logs/'; accessfile=$SRCDIR'http_access.log'; errorsfile=$SRCDIR'error_log'; #TARDIRは過去のログを保存するディレクトリ。 #SRCDIRは実際に現在ログがapacheにより使われているディレクトリ。 #accessfileは実際に現在apacheにより書き込みが行われているアクセスログファイル名。 #errorsfileは実際に現在apacheにより書き込みが行われているエラーログファイル名。 lastaccessfile=$TARDIR'access.last'; lasterrorsfile=$TARDIR'error.last'; newaccessfile=$TARDIR'access.0'; newerrorsfile=$TARDIR'error.0'; #ここまでは一時的に使うファイル名なので気に入らない場合やログが大きすぎて #一時ファイルを置けないときは場所を変えてください。 #ここから下で日付の計算をします。 #---年月の定義--- year=`/bin/date +%Y`; month=`/bin/date +%m`; day=`/bin/date +%d`; preyear=`/usr/local/bin/date |awk '{print $6}'`; langmonth=`/usr/local/bin/date |awk '{print $2}'`; langpremonth=`/usr/local/bin/date --date '1 months ago'|awk '{print $2 $6}'`; langonlypremonth=`/usr/local/bin/date --date '1 months ago'|awk '{print $2}'`; langpre2month=`/usr/local/bin/date --date '2 months ago'|awk '{print $2 $6}'`; langpre3month=`/usr/local/bin/date --date '3 months ago'|awk '{print $2 $6}'`; ##--ココまで年月の定義-- #----------------START PROGRAM---------- #--ココから実際にファイルの削除や圧縮・移動の作業を開始します。-- #3ヶ月前のログを削除 if test -f $TARDIR'access.'$langpre3month'.gz' ;then /bin/rm $TARDIR'access.'$langpre3month'.gz'; fi #apacheのログを/usr/local/apache2/logs/access_logをaccess.lastという名前で移動 /bin/mv $accessfile $lastaccessfile; #移してきたaccess.lastから今月のこのシェルを走らせるまでのログを元に戻す /bin/grep "$langmonth/$year" $lastaccessfile > $accessfile; #所有者やアクセス権を設定する /bin/chown www:www $accessfile; /bin/chmod 644 $accessfile; #apacheを再起動する /www/bin/apachectl graceful; #access.lastから先月分のログだけを抜き出してaccess.0と言う名前にする /bin/grep "$langonlypremonth/$preyear" $lastaccessfile > $newaccessfile; #access.0と言う名前からaccess.月年と言う名前に変換 /bin/mv $newaccessfile $TARDIR'access.'$langpremonth; /usr/local/bin/gzip $TARDIR'access.'$langpremonth; #access.lastファイル削除 /bin/rm $lastaccessfile; ############################## #ここからはエラーログ処理 # ############################## #apacheのログを/usr/local/apache2/logs/oldlogsにうつしてerrors.lastという名前で上書き /bin/mv $errorsfile $lasterrorsfile; #移してきたerrors.lastから今月のこのシェルを走らせるまでのログを元に戻す /bin/grep "$langmonth" $lasterrorsfile > $errorsfile; #所有者やアクセス権を設定する /bin/chown www:www $errorsfile; /bin/chmod 644 $errorsfile; #apacheを再起動する /www/bin/apachectl graceful; #errors.lastから先月分を抜き出してerrors.0と言う名前にする /bin/grep "$langonlypremonth" $lasterrorsfile > $newerrorsfile; #errors.0と言う名前からerrors.月年と言う名前に変換 /bin/mv $newerrorsfile $TARDIR'error.'$langpremonth; #先月分を圧縮する /usr/local/bin/gzip $TARDIR'error.'$langpremonth; #errors.lastを削除する /bin/rm $lasterrorsfile; #先々月分以降を削除する if test -f $TARDIR'error.'$langpre2month'.gz' ; then /bin/rm $TARDIR'error.'$langpre2month'.gz'; fi #もしも4ヶ月前のものが残るようなことがあった場合のため(特に関係ない) if test -f $TARDIR'error.'$langpre3month'.gz' ; then /bin/rm $TARDIR'error.'$langpre3month'.gz'; fi |
| 0 0 1 * * /usr/local/bin/newhttplog.sh > /dev/null 2>&1 |