基本情報

バージョン毎の特徴

2010年1月でついに新規リリース停止。2007年あたりからバイナリパッケージではほぼ入手困難になりつつあるのでコンパイルの知識が必要とされる。

スレッドを使えるようになったが、実際にはこれまでと同じプロセスを増やしていく起動(prefork)がデフォルトになっていることが多い。2.2がリリースされてちょっと中途半端な位置づけに感じる。

最新バージョンだが、すでにFedoraや比較的安定志向のVineですらバイナリパッケージを用意している。Tomcatと容易に連携できる。

設定ファイルの場所

たいていのOSにはバイナリパッケージも用意されていてその場合はOS毎に違ってくる。コンパイルの場合は何も指定していなければ/usr/local/apache/conf以下になる。

RedHat系/etc/httpd/conf
Debian系/etc/apache/conf

内部IP対応

SetEnvIf Remote_Addr 192.168.1. no_log
CustomLog /var/log/httpd/access_log common env=!no_log
SetEnvIf Remote_Addr 192.168.1. no_log
RequestHeader add MyHeader "%D %t mytext" env=no_log

DOS対策

  1. mod_evasive
  2. mod_dosdetector
  3. mod_limitipconn
  4. fail2 ban

mod_evasive

yum search mod_evasive --enablerepo=epel
LoadModule evasive20_module modules/mod_evasive20.so
<IfModule mod_evasive20.c>
    DOSHashTableSize    3097
    DOSPageCount        5
    DOSSiteCount        30
    DOSPageInterval     1
    DOSSiteInterval     1
    DOSBlockingPeriod   30
#    DOSEmailNotify      "-s 'DoS Alert' [email protected]"
    DOSLogDir           "/var/tmp/"
#    DOSWhitelist   127.0.0.1
#    DOSWhitelist   192.168.1.*
</IfModule>

パラメータの意味

上記の例だと同一ページに1秒間5アクセス、もしくは同一サイトに1秒間30アクセスだとブロックリストに入り、30秒間はブロックされる。ブラックリストのIPからのアクセスがあった場合はまた30秒になるので、長い時間を指定する必要はない。

DOSHashTableSize内部のメモリ、小さいと記憶できるIPが少なくなる
DOSPageCount一つのページに対する閾値
DOSSiteCountサイト全体に対する閾値
DOSPageInterval一つのページに対するDOSと判定する計測時間
DOSSiteIntervalサイト全体に対する計測時間
DOSSystemCommandOSコマンドを実施できる。内部での通知に使いたい場合など
DOSWhitelistDOS検地除外IPなど

mod_dosdetector

コンパイル

sudo yum install -y httpd-devel gcc
wget http://downloads.sourceforge.net/project/moddosdetector/moddosdetector/version-0.2/mod_dosdetector-0.2.tar.gz
tar xvzf mod_dosdetector-0.2.tar.gz
cd mod_dosdetector-0.2/
make
make install

設定

DoSDetection On
DoSPeriod 10
DoSThreshold 10
DoSHardThreshold 20
DoSBanPeriod 15
DoSTableSize 10
DoSIgnoreContentType image/*
RewriteEngine on
RewriteCond %{ENV:SuspectHardDoS} .+
RewriteRule .* - [R=503,L]

mod_limitipconn

コンパイル

2.0.x系

./configure --enable-mods-shared=all --enable-shared=yes --enable-ssl --enable-proxy
※SSLを有効にした場合opensslのライブラリが必要
再コンパイル時の注意./configure でやるとLoadModule一切なしのhttpd.confが作成される
そのあと--enable-mods-shared=allしてもhttpd.confはそのままなので注意
いったん消してmake installする

2.2.x系

./configure --prefix=/usr/local/apache22 --enable-mods-shared=all --enable-proxy-ajp --enable-proxy

mod_sslを静的に組み込む

PHP

LoadModuleは自動で入る。残りはhttpd.confに以下の設定をする
# for PHP
AddType application/x-httpd-php .php

PHP4.x

コンパイルにbison,flex必要gnuからとってきて、bison,flexの順でconfigureのmake installする。/usr/local/binにパスを通しておくこと!
今回はPostgresを有効にしてコンパイル
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pgsql=/usr/local/pgsql --enable-mbstring

PostgresもMYSQLも入ってれば以下でOK(MiracleLinux3.0 フルインストール)
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pgsql --with-mysql --enable-mbstring

PHP5.x

flex,libxml-devel,libxml2-devel,postgresql-devel必要
./configure --with-apxs2=/usr/local/apache2/bin/apxs  --enable-mbstring  --enable-mbregex  --with-pgsql  --with-openssl=/usr/local/ssl/ --without-mysql --with-libxml=/usr/local/lib 

Apacheの動作モード

preforkとworkerとがありApache2.0より追加されたスレッドで動作するのがworkerだが、実態はほとんどpreforkが継続して使われている。以下のコマンドで複数プロセスが見えればpreforkである。

ps -ef | grep apache

httpd.confの設定

インストール後のtodo

実行ユーザーはwwwにする

groupadd www
useradd -g www -s /sbin/nologin www
  1. AddDefaultCharSetコメントアウト(2.0.53より削除されている)
  2. LanguagePriority ja
  3. User www
  4. Group www
実行時のバージョンを隠す
ServerSignature Off

LocationとDirectoryの使い分け

Locationはファイルシステムの外にあるものを設定するときに使う。DocumentRoot以下に対する設定ならばDirectoryを使う。全体に対する設定にもLocationを使う。
<Location /server-status>
 SetHandler server-status
 Order deny,allow
 Deny  from all
 Allow from 192.168.0.1
</Location>

ユーザーディレクトリ使用可能にする

rootでchmod 751 username

CGIディレクトリ

<Directory /home/httpd/cgi-bin>
   Options ExecCGI
</Directory>
CGI実行は
httpdの実行者がapacheユーザーとなるので755にしないと500エラー
#For UserDir CGI
ScriptAliasMatch ^/~([a-zA-Z0-9_-]+)/cgi-bin/(.+) /home/$1/cgi-bin/$2
<Directory /home/*/cgi-bin/>
       Options ExecCGI
       AddHandler cgi-script .cgi
</Directory>

ログの設定

ログ設定の基本commonとcombined

combinedLogFormat "%h %l %u %t ¥"%r¥" %>s %b ¥"%{Referer}i¥" ¥"%{User-Agent}i¥"" combined
commonLogFormat "%h %l %u %t ¥"%r¥" %>s %b" common
  1. リモートホスト
  2. 認証領域
  3. 認証ユーザー
  4. 時間
  5. リクエストの最初の行
  6. ステータス
  7. 送信バイト
  8. リファラー
  9. UA

ワームログの除去と家庭内アクセス(192.168.1.0/24)を別のログに記録する

#Log setting
SetEnvIf Remote_Addr 192.168.1. homelog nolog
SetEnvIf Request_URI "default.ida" wormlog nolog
SetEnvIf Request_URI "root.exe" wormlog nolog
SetEnvIf Request_URI "cmd.exe" wormlog nolog
SetEnvIf Request_URI "Admin.dll" wormlog nolog
SetEnvIf Request_Method "(GET)|(POST)|(PUT)|(DELETE)|(HEAD)" !worm
SetEnvIf Request_URI "\.(gif)|(jpg)|(png)|(css)|(swf)$" nolog
LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""  combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog logs/home_log common env=homelog
CustomLog logs/worm_log common env=wormlog
CustomLog logs/access_log combined env=!nolog

SSLの設定

LoadModule ssl_module modules/mod_ssl.so
<IfDefine SSL>
LoadModule ssl_module modules/mod_ssl.so
Listen 443
SSLSessionCache shm:/var/cache/ssl_gcache_data(524288)
<VirtualHost _default_:443>
DocumentRoot /usr/local/apache2/htdocs
SSLEngine on
SSLCertificateFile conf/ssl/server.crt
SSLCertificateKeyFile conf/ssl/server.key
<Files ~ "\.(cgi|shtml)$">
   SSLOptions +StdEnvVars
</Files>
<Directory /home/httpd/cgi-bin>
   SSLOptions +StdEnvVars
</Directory>
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
CustomLog logs/ssl_request_log \
         "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
</IfDefine>

SSIの利用

<Directory "xxx">
Options Includes or IncludesNoExec
AddHandler server-parsed .shtml
AddType text/html .shtml
</Directory>

上記設定でxxxディレクトリ以下で.shtmlのみ利用可能になる

リバースプロキシ

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
#Proxy
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /tu/ http://192.168.1.78/
ProxyPassReverse /tu/ http://192.168.1.78/

SSLProxyEngine On

WebDAVの設定

1.3系は日本語問題ありなのでできれば2.0系で運用すべし

  1. mod_dav.soをインストール
  2. httpd.confに以下のように追加

Windowsで2.0とjkで連携

mod_jk.soはバイナリを入れる。configの設定はlinuxとまったく同じでよい

ワンテク

BASIC認証回避http://id:pass@URL/

コマンドラインログ解析

awk -F " " '{print $1}' access_log  | uniq
 awk -F " " '{print $1}' access_log  | sort | uniq -c | sort

利用しているモジュール解説

mod_rewrite

URL書き換えできるモジュール。ローカルアドレスをそのまま返すのでリバースプロキシーになりません。サイト移転時などに重宝する。
AllowOverride FollowSymLinks
RewriteEngine On
RewriteRule ^/app/(.*)$ http://転送先/$1 
RewriteRule ^hoge\.html /hogehoge\.html?%{QUERY_STRING} [L,PT]

mod_header

レスポンスヘッダーに付け加えたりできる。キャッシュコントロールなどをまとめて入れておくと便利。カスタマイズした内容を確認するにはPHPなどの動的スクリプトで!HttpHederを見てもわからんぞ。
SetEnvIf Remote_Addr xx.xx.xx.xx internal
RequestHeader add X-INTERNAL-LAN "true" env=internal
RequestHeader add X-ENV "staging"

パフォーマンスについて

KeepAliveについて考察する

時間切れまでつなぎっぱなしという恐るべきリソース消費。詳細は要調査 なおPHPを有効にするとプロセスのサイズが膨れるのでKeepAliveを短く(1秒ほど)して、プロセスが増えないようにしたほうが良い。

有用リンク

Counter: 17580, today: 1, yesterday: 1

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-10-14 (金) 13:16:53