Apache2.4のインストール

CentOS7では、yumのデフォルトで2.4がインストールできます。

1. Apache2.4のインストール

# yum install httpd
# yum install httpd-devel
# yum install mod_ssl
# yum install mod_security
※「httpd-devel」のインストールは任意です。

2. 各ファイルの設定

ここでは一例を挙げます。サーバの環境や使用目的などによって任意に変更して下さい。
(1) /etc/httpd/conf/httpd.conf
ServerAdmin webmaster@example.jp
ServerName www.example.jp:80
<Directory "/var/www/html">
    Options FollowSymLinks ExecCGI
    AllowOverride All
    Require all granted
</Directory>
画像ファイルを記録させない場合に設定。
SetEnvIf Request_URI "\.(gif|jpg|png|ico)$" image-object
CustomLog logs/access_log combined env=!image-object
<IfModule alias_module>
#    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
#<Directory "/var/www/cgi-bin">
#    AllowOverride None
#    Options None
#    Require all granted
#</Directory>
#AddDefaultCharset UTF-8
AddDefaultCharset Off

■追加設定(最終行に追加)
ServerTokens Prod
ExtendedStatus Off
ServerSignature Off
UseCanonicalName Off

■追加セキュリティ設定(VirtualHostで記述)
<VirtualHost *:80>
    SSLEngine off
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteOptions inherit
        RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS)
        RewriteRule .* - [F]
    </IfModule>
</VirtualHost>
※Apache2.4では「VirtualHost」の前に記述していた「NameVirtualHost」ディレクティブは廃止になりました。

(2) /etc/httpd/conf.d/autoindex.conf
Index機能は無効にしているので、このファイルをリネームします。
# cd /etc/httpd/conf.d
# mv autoindex.conf autoindex.conf.bak

(3) /etc/httpd/conf.d/userdir.conf
ユーザディレクトリ機能は使用しないのでリネームします。
※使用する場合には有効にして下さい。
# cd /etc/httpd/conf.d
# mv userdir.conf userdir.conf.bak

(4) /etc/httpd/conf.modules.d/00-dav.conf
WebDAV機能は使用しないのでリネームします。
※使用する場合には有効にして下さい。
# cd /etc/httpd/conf.modules.d
# mv 00-dav.conf 00-dav.conf.bak

(5) /etc/httpd/conf.modules.d/00-mpm.conf
ApacheのMPMを「event」に変更します。
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
LoadModule mpm_event_module modules/mod_mpm_event.so

(6) 設定ファイルのエラーチェック
設定ファイルにエラーがないかどうかチェックします。
# apachectl configtest
※何も表示されなかったらエラーはありませんでした。

3. サービスの起動

httpdサービスを起動します。
■ サービスの起動
# systemctl start httpd.service
■ サービスの停止
# systemctl stop httpd.service
■ サービスの自動起動
# systemctl enable httpd.service
■ サービスの自動起動解除
# systemctl disable httpd.service
■ サービスが自動起動かどうかチェック
# systemctl is-enabled httpd.service

4. ログの自動整理の解除

logrotateでApacheのログは自動的に整理されますが、自分がスクリプトで管理している場合には、自動的に整理されると不都合を生じますので、この機能を解除します。

「/etc/logrotate.d」というディレクトリの中に「httpd」というファイルがあります。
/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

Apacheのログを週ごとに整理したくない場合には、行頭に#を付けて、コメントアウトします。
※ファイルを削除しても、また作成されるようです。
vi /etc/logrotate.d/httpd

【ファイルの修正】
#/var/log/httpd/*log {
#    missingok
#    notifempty
#    sharedscripts
#    postrotate
#        /sbin/service httpd reload > /dev/null 2>/dev/null || true
#    endscript
#}
 

念のため、パーミッションを000に変更します。
# chmod 000 /etc/logrotate.d/httpd

5. gzip圧縮の設定

アクセス速度アップのために、通信速度を短縮する「gzip圧縮」を導入します。
「/etc/httpd/conf.d」フォルダに「deflate.conf」というファイルを作成します。
※ファイル名は任意です。
# vi /etc/httpd/conf.d/deflate.conf

以下の内容を記入します。
# gzip setting
AddOutputFilterByType DEFLATE text/html text/plain text/css
AddOutputFilterByType DEFLATE text/javascript application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# expire setting
ExpiresActive On
ExpiresDefault "access plus 600 seconds"
ExpiresByType text/html "access plus 10 seconds"

Apacheを再起動します。
# systemctl restart httpd.service



最終更新:2017年04月17日 17:47