www.suzu841.com | mrs.suzu841.com

neko1sa.gifApacheの解体、斜め読み CentOS5.3


今回はきっと、Apache使いである方々の顰蹙を大幅に買うだろうなぁと思いつつ、Apacheの良く使う機能だけを簡単に斜め読みしてみました。 こうして書いている間にも、我が家のApache使いが目を吊り上げ牙をむいています・・・シクシク ( でも、いいんだもんねえぇ~♪)


2009年10月7日現在の環境 : CentOS 5.3 (Kernel 2-6-18-164.e15 on an i686) / Apache 2.2 系



Apacheと.htaccessについて

簡単な言葉で私流の解釈を言ってしまえば、Apacheというのは、httpd の別名(マスコット・ネーム)で、httpd というのは、webサービスを提供するデーモンのことで、ぶっちゃけてしまえばhttpdというのはwebサーバのこと。で、httpd.conf というのはそのApacheが読み込む設定ファイルの名前です。

さらに、.htaccessファイルというのはこのApacheの機能をクライアント側で設定できるファイルのことで・・・と。
ただし、Apacheのどの機能を許可するのかはサーバ管理側のポリシー次第ですから、クライアント側で.htaccessファイルを使ってどこまで自由に設定できるかは、それぞれの環境によって違ってきますので、ご承知おきください。

httpd.confについて

httpd.conf は大きく分けると以下の3つのブロックから成り立っていると思っています。
Section 1: Global Environment ← 全体設定
Section 2: 'Main' server configuration ← メインサーバの設定
Section 3: Virtual Hosts ← バーチャルホストの設定

以下はhttpd.confの行頭の部分です。
概要が述べてあり、ここには設定はありません。
#
# This is the main Apache server configuration file. It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.2/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
# for a discussion of each configuration directive.
#
#
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
#
# The configuration directives are grouped into three basic sections:
# 1. Directives that control the operation of the Apache server process as a
# whole (the 'global environment').
# 2. Directives that define the parameters of the 'main' or 'default' server,
# which responds to requests that aren't handled by a virtual host.
# These directives also provide default values for the settings
# of all virtual hosts.
# 3. Settings for virtual hosts, which allow Web requests to be sent to
# different IP addresses or hostnames and have them handled by the
# same Apache server process.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path. If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so "logs/foo.log"
# with ServerRoot set to "/etc/httpd" will be interpreted by the
# server as "/etc/httpd/logs/foo.log".
#


Section 1: Global Environment

では、ここからがSection 1であり、 Global Environment つまり、webサーバの全体設定になります。
### Section 1: Global Environment
#
# The directives in this section affect the overall operation of Apache,
# such as the number of concurrent requests it can handle or where it
# can find its configuration files.
#

#
# Don't give away too much information about all the subcomponents
# we are running. Comment out this line if you don't mind remote sites
# finding out what major optional modules you are running
ServerTokens OS

上記のServerTokens OSの部分はサーバ情報をOSレベルで表示すること。
上の OS と書かれている部分を ProductOnly と編集することでServerのシステム情報を出さなくすることが可能になります。
このサイトのApache の Server 情報の隠蔽に詳細が書いてありますので、よろしければ参考にどうぞ。



#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE! If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the LockFile documentation
# (available at <URL:http://httpd.apache.org/docs/2.2/mod/mpm_common.html#lockfile>);
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
ServerRoot "/etc/httpd"

ServerRootとは、webサービスを提供するのに必要な .conf ファイルがある場所。


例えば、私のCentos5.3では /etc/httpd/ には以下のようなディレクトリがあります。
etc.png
これらもろもろのディレクトリの中にはwebサービスで必要な .confファイルがいろいろ入っています。
confディレクトリの中にはApacheの設定ファイルであるhttpd.conf 、conf.dのディレクトリにはphpなどの設定ファイルとか・・・。



参考ですが、私がいつも使うWindowsマシンの Xampp の ServerRoot は以下のようになっています。

win-httpd.png

当然のことですが、場所も記述方法もLinuxとWindowsでは違っています。




#
# PidFile: The file in which the server should record its process
# identification number when it starts.
#
PidFile run/httpd.pid


上の設定はプロセスid ファイルの置き場所になります。

プロセスid を知りたい場合には、cat httpd.pid で調べることができます。
pid.png



#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 120

コネクションのタイムアウトの時間の設定です。ここでは120秒になっています。



#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive Off

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15

KeepAliveはデフォルトではオフになっています。
MaxKeepAliveRequests 100というのは、KeepAliveコネクションの最大数です。100という数値を0にすると無制限となり、 OSの限度までということになります。

KeepAliveTimeout 15 というのは、KeepAliveコネクションに対してのタイムアウトの時間です。

※ いずれの設定もKeepAlive が Off になっている場合には無視されます。
設定する場合には、KeepAlive Off を KeepAlive On にしなければ有効にはなりません。




##
## Server-Pool Size Regulation (MPM specific)
##

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>

# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule worker.>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

ここはサーバーのプロセス起動方式の設定です。
prefork MPM とは、前もってプロセスをあげておくことであり、worker MPM はスレッドで起動されます。
デフォルトではprefork MPMが有効になっています。
※ この設定を変更するためには/etc/sysconfig/httpdの編集が必要になります。

etc-sysconfig-httpd.png


物は試しで変更してみたら、私の環境では、以下のようなメッセージが出て、エラーになってしまいました。
etc-sysconfig-httpd-error.png
つまり、私の環境ではPHPがスレッドに対応していないからダメだよ、と。




#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, in addition to the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
Listen 80

Listen 80 というのは待ちうけポートのことです。この80という数値を変更することによってApacheの待ち受けポートが変わります。
もし、ローカルで動かしているwebサーバの待ち受けポートを調べたいと思ったら、netstat -antp で調べることができると思います。



#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_alias_module modules/mod_authn_alias.so
LoadModule authn_anon_module modules/mod_authn_anon.so
LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_owner_module modules/mod_authz_owner.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_default_module modules/mod_authz_default.so
LoadModule ldap_module modules/mod_ldap.so
LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
LoadModule include_module modules/mod_include.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule logio_module modules/mod_logio.so
LoadModule env_module modules/mod_env.so
LoadModule ext_filter_module modules/mod_ext_filter.so
LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule expires_module modules/mod_expires.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
LoadModule usertrack_module modules/mod_usertrack.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule mime_module modules/mod_mime.so
LoadModule dav_module modules/mod_dav.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule info_module modules/mod_info.so
LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule dir_module modules/mod_dir.so
LoadModule actions_module modules/mod_actions.so
LoadModule speling_module modules/mod_speling.so
LoadModule userdir_module modules/mod_userdir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.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
LoadModule cache_module modules/mod_cache.so
#LoadModule suexec_module modules/mod_suexec.so
LoadModule disk_cache_module modules/mod_disk_cache.so
LoadModule file_cache_module modules/mod_file_cache.so
LoadModule mem_cache_module modules/mod_mem_cache.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule version_module modules/mod_version.so

#
# The following modules are not loaded by default:
#
#LoadModule cern_meta_module modules/mod_cern_meta.so
#LoadModule asis_module modules/mod_asis.so

#
# Load config files from the config directory "/etc/httpd/conf.d".
#
Include conf.d/*.conf

モジュールの設定です。
デフォルトでは、suexecが無効となっていますが、理由は設定が面倒くさいから・・・だそうです。



#
# ExtendedStatus controls whether Apache will generate "full" status
# information (ExtendedStatus On) or just basic information (ExtendedStatus
# Off) when the "server-status" handler is called. The default is Off.
#
#ExtendedStatus On


ここはApacheのプロセスとコネクションのステータス表示を有効にするかどうかの設定です。
デフォルトでは無効になっています。


#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# . On SCO (ODT 3) use "User nouser" and "Group nogroup".
# . On HPUX you may not be able to use shared memory as nobody, and the
# suggested workaround is to create a user www and use that user.
# NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
# when the value of (unsigned)Group is above 60000;
# don't use Group #-1 on these systems!
#
User apache
Group apache

Apacheのチャイルドプロセスのユーザとグループ権限の設定です。
デフォルトでは、上のようになっていますが、できれば、このデフォルトのまま使われることをおすすめしたいと思います。