Apache インストール後に必ず設定する 5の項目

ソフトウエア

<景品表示法に基づく表記> 本サイトのコンテンツには、商品プロモーションが含まれている場合があります。

Apacheをインストールした後に何を設定すればいいの?

この疑問に回答します。

私はCentOSを利用してApacheをインストールしています。OSの違いによりパッケージ管理のコマンドが違いますが、Apacheには直接関係ありません。Debian系のディストリビューションを利用している場合は、パッケージコマンドを読み替えてください。

Apacheパッケージのインストール

epel リポジトリ

Apache本体とは直接関係ありませんが、Apacheと一緒に利用するモジュール群が多く含んでいるので忘れずにリポジトリを追加しておきましょう。

# dnf install epel-release

 

Apacheのインストール

Apache本体、利用頻度が高い各モジュール、キャッシュによる高速化モジュールなどをインストールします。

まずは、Apache本体をインストールします。

# dnf install httpd

 

私はPHPで動作プログラムをよく利用するのでPHPに関するジュールをインストールします。

# dnf install php php-opcache php-pecl-apcu php-mbstring php-common php-mysqlnd php-json php-devel php-pdo

 

サイトをSSL化(https)を利用する場合は、SSL通信モジュール(mod_ssl)をインストールする必要があります。

# dnf install mod_ssl

 

これぐらいのモジュールをインストールしておけば、大体は事足ります。あとはApacheサービスの起動と、自動起動設定を行います。

# systemctl start httpd
# systemctl enable httpd

 

Apacheと各モジュールで行う必要最低限の設定

ヘッダー情報の隠匿

Apacheのデフォルト設定は、色々な情報を開示するようになっています。

 

クライアントは、サーバーの応答ヘッダーを見ると多くの情報を取得できます。

  • サーバの一般的な OS 種別
  • バージョン情報
  • コンパイルされて組み込まれているモジュールの情報

セキュリティホールになりかねないので最低限の開示に留める設定にします。

 

まずは、設定のバックアップを取得します。

# cd /etc/httpd/conf
# cp httpd.conf httpd.conf.org

テキストエディタを利用して設定ファイルを書き換えます。

# vi httpd.conf

 

設定ファイル(httpd.conf)の行末に設定を追加します。

# Apache バージョン非表示
ServerTokens Prod

# Hide Header X-Powered-By
Header always unset X-Powered-By
TraceEnable off

# 逆引き
HostnameLookups Off

 

これで最低限の情報しか開示されません。

ファイル一覧(Indexes)の隠匿

公開されているディレクトリ内に表示させるファイルが存在しないと、ブラウザでアクセスすると、ディレクトリのファイル一覧を表示される設定がデフォルトでは有効になっています。

ファイル一覧(Indexes)
ファイル一覧(Indexes)

ファイル一覧が表示されないように設定変更を行います。まずは、設定のバックアップを取得します。

# cd /etc/httpd/conf
# cp httpd.conf httpd.conf.org

テキストエディタを利用して設定ファイルを書き換えます。

# vi httpd.conf

 

<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    # 
    # Options Indexes FollowSymLinks ← 変更前
    Options FollowSymLinks ← 変更後
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

 

<Directory “/var/www/html”> </Directory>ディレクティブ内に記述された、

Options Indexes FollowSymLinks から Indexes を削除します。

URLの書き換え・リダイレクト処理(mod_rewrite)

「.htaccess」ファイルを利用して、URLの書き換え・リダイレクト処理を実施する事が多いので設定を有効化する必要があります。

# cd /etc/httpd/conf
# cp httpd.conf httpd.conf.org

テキストエディタを利用して設定ファイルを書き換えます。

# vi httpd.conf

 

<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    # 
    # Options Indexes FollowSymLinks
    Options FollowSymLinks
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None ← 変更前
    AllowOverride All ← 変更後

 

<Directory “/var/www/html”> </Directory>ディレクティブ内に記述された、

AllowOverrideNone から All に変更します。

Apacheで環境に合わせて行う設定

ハードウェアのスペックやアクセス数によって各種パラメーターを変更することで、Apacheを最適化することができます。いわゆるチューニングって呼ばれる内容です。

今回は、チューニングには触れません。

PHPで行う必要最低限の設定

Apahceと同様にPHPもデフォルト設定は、色々な情報を開示するようになっています。併せて最低限に変更します。

まずは、設定のバックアップを取得します。

# cd /etc
# cp php.ini php.ini.org

テキストエディタを利用して設定ファイルを書き換えます。

# vi php.ini

 

デフォルト設定では、「expose_php」パラメーターが「ON」になっているので、「off」に変更します。

# PHP バージョン非表示
expose_php = Off

これで最低限の情報しか開示されません。

各設定の反映

設定変更が完了したら忘れず設定を反映させてください。

# systemctl restart httpd

 

Apacheのログ管理

CentOSは、Apacheで出力されたログは1週間でログローテーションするように設定されています。

ウェブサービスの場合は、アクセスログが大量になるので毎日ログローテーションするように設定するほうが都合がよいです。

テキストエディタを利用して設定ファイルを書き換えます。

# vi /etc/logrotate.d/httpd

 

ログローテーションを毎日実施し、3カ月間(93日)のデータを保存するように設定します

/var/log/httpd/*log {
    daily ← 追加 毎日ログローテーション
    rotate 93 ← 追加 93日の保存
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

 

これで明日になれば、新しいファイルにログが記述されるようになります。

 

 

以上、「Apache インストール後に必ず設定する 5の項目」という記事でした。