Apache | Apache のインストール | 設定ファイル (http.conf) の初期設定

Apache の設定は http.conf ファイルで行う。Apache をインストールした後は、インストールしたディレクトリや使用するポート番号に合わせて設定ファイルを変更する必要がある。ここでは、Apache のインストール直後に行う初期設定について説明する。

インストールディレクトリの修正

Apache の設定ファイルである http.conf ファイルは、インストールディレクトリの “/Apache24/conf/” にある。

macOS では /etc/apache2/httpd.conf にある。

修正する前に、念のため http.conf ファイルをコピーしてバックアップとして別に保存しておく。必要ないと思う場合は省略してもよい。

それでは http.conf ファイルを編集する。http.conf ファイルはテキストファイルなので、テキストエディタで開ける。たとえば Windows のメモ帳でも編集できる。

まず、ファイル内で “ServerRoot” を検索する。すると、次のように書かれた箇所が見つかる。

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path.  If you point
# ServerRoot at a non-local disk, be sure to specify a local disk on the
# Mutex directive, if file-based mutexes are used.  If you wish to share the
# same ServerRoot for multiple httpd daemons, you will need to change at
# least PidFile.
#
Define SRVROOT "c:/Apache24"

ServerRoot "${SRVROOT}"

ServerRoot には Apache サーバーのディレクトリを設定する。デフォルトで “c:/Apache24” になっている部分を、実際に Apache をインストールしたディレクトリに変更すればよい。具体的には、SRVROOT 変数にインストールしたディレクトリを設定し、ServerRoot ではその SRVROOT 変数を使用して設定している。ここでは次のように変更した。

Define SRVROOT "C:/apache/Apache24"

ServerRoot "${SRVROOT}"

上で設定した SRVROOT 変数は、次のように他の設定箇所でも使用され、同じディレクトリが設定される。

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "${SRVROOT}/htdocs"

<Directory "${SRVROOT}/htdocs">

 ...省略...

</Directory>
<IfModule alias_module>

 ...省略...

    #
    # ScriptAlias: This controls which directories contain server scripts. 
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    #
    ScriptAlias /cgi-bin/ "${SRVROOT}/cgi-bin/"

</IfModule>

#
# "${SRVROOT}/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "${SRVROOT}/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

ポート番号の設定

次に、ポート番号の設定を確認する。ファイル内で “Listen” を検索する。

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of 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.
#
#Listen 12.34.56.78:80
Listen 80

Listen は、Apache がどのポート番号と IP アドレスを listen するかを設定する。一般的に Web サーバーとブラウザの通信には 80 番ポートを使用するため、デフォルト設定でも 80 番が設定されている。通常は変更する必要はないが、複数の Web サーバーが同じサーバーコンピュータ上で実行される場合は、ポート番号が重複しないようにここで設定を変更する。

ServerName の設定

最後に ServerName の設定である。設定ファイル内で “ServerName” を検索する。

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80

ServerName は、サーバー自身を表すホスト名とポート番号を設定する。初期設定ではコメントアウトされており、設定する場合は “www.example.com:80” のようにホスト名とポート番号を指定する。ポート番号を省略すると、サーバーへリクエストできるデフォルトポート番号である 80 が使用される。

ここでは Apache をローカル環境にインストールしているため、ServerName の前にある “#” を削除し、次のように変更した。

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName localhost:80

設定ファイルの修正が終わったら、設定ファイルを保存する。