Apache httpd 2.4.27 + mod_http2 インストールメモ

Linux
Linux
スポンサーリンク

 

(2023年10月21日追記)このバージョンの Apache httpd にはセキュリティ脆弱性及び不具合が見つかっています。これらのセキュリティ脆弱性などが修正されている「Apache httpd 2.4.58(TLS1.3対応)+ HTTP/2 + Brotli インストールメモ」をご参照ください。

2017年7月11日 Apache httpd2.4.27 がリリースされました。バージョン2.4.26のリリースからわずか約3週間でのアップデートになります。このアップデートはいつものように複数の脆弱性(JVNVU#92256772)への対応と、互換性の改善が目的のようです。さっそく、CentOS7.3 (1611) に Apache httpd 2.4.27 と mod_http2 をインストールする手順をまとめてみました。

Prefork では mod_http2 が無効に

Apache httpd 2.4.27 から、Prefork(Apache httpd の動作モードのひとつです)を使っている場合は、mod_http2 が無効になるため HTTP/2 プロトコルを使った通信ができなくなりました。

*) COMPATIBILITY: mod_http2: Disable and give warning when using Prefork.
The server will continue to run, but HTTP/2 will no longer be negotiated.
[Stefan Eissing]

Complete ChangeLog for 2.4 の Changes with Apache 2.4.27 より引用

開発ツールのインストール

Apache httpd や各種ライブラリをソースからコンパイルしますので、CentOS7.3 (1611) を「最小限のインストール」でインストールしている場合は、基本コマンドと開発ツールをインストールしておきましょう。

yum -y groupinstall base
yum -y groupinstall development

OpenSSL 1.1.0 のインストール

まずはじめに OpenSSLのコンパイルに必要なパッケージをインストールしておきます。

yum -y install zlib-devel
yum -y install perl-core

OpenSSLのインストール(そこそこの時間がかかります)

cd /usr/local/src/
wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz
tar xvzf openssl-1.1.0f.tar.gz
cd openssl-1.1.0f/
./config --prefix=/usr/local/openssl-1.1.0f shared zlib
make depend
make
make test
make install

OpenSSL1.1.0 のライブラリにパスを通しておきます。

echo /usr/local/openssl-1.1.0f/lib > /etc/ld.so.conf.d/openssl110f.conf
ldconfig

Nghttp2 のインストール

HTTP/2(mod_http2)のコアエンジン Nghttp2 をインストールします。

Nghttp2 が必要とするライブラリのインストール

yum -y install libev-devel c-ares-devel

Nghttp2 のダウンロード
※頻繁にバージョンアップされているようです。ダウンロードの前に最新リリースを確認しましょう。
https://github.com/tatsuhiro-t/nghttp2/releases/latest

cd /usr/local/src/
wget https://github.com/nghttp2/nghttp2/releases/download/v1.24.0/nghttp2-1.24.0.tar.gz

Nghttp2 のインストール。環境変数 OPENSSL_CFLAGS と OPENSSL_LIBS に先ほどインストールした、OpenSSL1.1.0 のディレクトリパスを指定してコンパイルします。

tar xvzf nghttp2-1.24.0.tar.gz
cd nghttp2-1.24.0/
env OPENSSL_CFLAGS="-I/usr/local/openssl-1.1.0f/include" OPENSSL_LIBS="-L/usr/local/openssl-1.1.0f/lib -lssl -lcrypto" ./configure -enable-app
make
make install

/usr/local/lib 以下に「libnghttp2」がインストールされますので、こちらもライブラリのパスに追加しておきます。

echo /usr/local/lib > /etc/ld.so.conf.d/usr-local-lib.conf
ldconfig

Apache httpd インストールの下準備

Apache httpd のコンパイルに必要なパッケージをインストールしておきます。

yum -y install pcre-devel
yum -y install expat-devel

また、Apache 2.4系をソースコードからインストールする場合は、APR と APR-util が必要になりますので、インストールしておきます。

APR

cd /usr/local/src/
wget http://ftp.jaist.ac.jp/pub/apache//apr/apr-1.6.2.tar.gz
tar xvzf apr-1.6.2.tar.gz
cd apr-1.6.2/
./configure
make
make install

APR-util

cd /usr/local/src/
wget http://ftp.jaist.ac.jp/pub/apache//apr/apr-util-1.6.0.tar.gz
tar xvzf apr-util-1.6.0.tar.gz
cd apr-util-1.6.0/
./configure --with-apr=/usr/local/apr
make
make install

Apache httpd 2.4.27 (mod_http2) のインストール

本題の Apache httpd のインストールです。

Apache httpd のソースコードのダウンロード

cd /usr/local/src/
wget http://ftp.riken.jp/net/apache//httpd/httpd-2.4.27.tar.gz

ダウンロードしたソースコードを解凍して、ディレクトリを移動します。

tar xvzf httpd-2.4.27.tar.gz
cd httpd-2.4.27/

HTTP/2 を使うために必要なモジュール mod_http2 と mod_ssl を有効にし、--with-ssl オプションで、OpenSSL1.1.0 のディレクトリパスを指定してコンパイル&インストールします。

./configure \
--enable-http2 \
--enable-ssl \
--with-ssl=/usr/local/openssl-1.1.0f \
--enable-so \
--enable-mods-shared=all \
--enable-mpms-shared=all
 
make
make install

冒頭にも書きましたが、Apache httpd の動作モードが Prefork の場合 mod_http2 が無効になります。念のため動作モードを確認しておきましょう。

/usr/local/apache2/bin/httpd -V
(略)
Server MPM: event ←動作モード

以上で Apache が /usr/local/apache2/ 以下にインストールされました。続いてSSLサーバー証明書の作成と、Apacheの設定を行います。

自己署名のSSLサーバー証明書の作成(HTTPS用)

正規の認証局が発行したサーバー証明書を、無料で取得することもできます。よければご参照ください → Let's Encrypt サーバー証明書の取得と自動更新設定メモ

HTTP/2 は事実上 HTTPS が必須になりますので、Apacheの設定の前に、SSLサーバー証明書を作成しておきます。

秘密鍵の作成

openssl genrsa 2048 > server.key

CSR(証明書署名要求)の作成(入力するのは2箇所だけです)

openssl req -new -key server.key > server.csr
 
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:<空エンター>
Locality Name (eg, city) [Default City]:<空エンター>
Organization Name (eg, company) [Default Company Ltd]:<空エンター>
Organizational Unit Name (eg, section) []:<空エンター>
Common Name (eg, your name or your server's hostname) []:www.example.com
Email Address []:<空エンター>
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:<空エンター>
An optional company name []:<空エンター>

SSLサーバー証明書の作成(有効期限10年)

openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt

秘密鍵とSSL証明書を移動

mv -i server.key /etc/pki/tls/private/
mv -i server.crt /etc/pki/tls/certs/

パーミッションを変更

chmod 600 /etc/pki/tls/private/server.key
chmod 600 /etc/pki/tls/certs/server.crt

CSRを削除

rm server.csr

Apache httpd の設定

オリジナルの設定ファイルをバックアップ

mv -i /usr/local/apache2/conf/httpd.conf /usr/local/apache2/conf/httpd.conf.org
mv -i /usr/local/apache2/conf/extra/httpd-ssl.conf /usr/local/apache2/conf/extra/httpd-ssl.conf.org

・設定ファイルを作成します
vim /usr/local/apache2/conf/httpd.conf

vim /usr/local/apache2/conf/extra/httpd-ssl.conf

systemd サービスファイルの作成

Apache httpd 用の systemd サービスファイル(起動スクリプトのようなもの)を作成します。

vim /etc/systemd/system/httpd.service

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl start
ExecReload=/usr/local/apache2/bin/apachectl graceful
ExecStop=/usr/local/apache2/bin/apachectl stop
 
[Install]
WantedBy=multi-user.target

作成したサービスファイルを systemd に反映

systemctl daemon-reload

systemd に反映されているか確認

systemctl list-unit-files | grep httpd
httpd.service disabled ←この表示があればOK

起動

systemctl start httpd

自動起動設定

systemctl enable httpd

firewalld設定

HTTP(80/tcp) と HTTPS(443/tcp) を開けておきます。

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --reload

・確認
firewall-cmd --list-all

public (default, active)
interfaces: enp0s3 enp0s8
sources:
services: dhcpv6-client ssh
ports: 443/tcp 80/tcp ←この表示があればOK
(略)

ログのローテーション設定

・設定ファイルを作成します
vim /etc/logrotate.d/httpd

/usr/local/apache2/logs/*log { 
    daily 
    missingok 
    dateext 
    rotate 60 
    create 644 daemon daemon
    sharedscripts 
    postrotate 
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript 
}

・確認します
logrotate -dv /etc/logrotate.d/httpd
-----(下記のような表示であればOKです)-----

reading config file /etc/logrotate.d/httpd
 
Handling 1 logs
 
rotating pattern: /usr/local/apache2/logs/*log after 1 days (60 rotations)
empty log files are rotated, old logs are removed
(略)

以上です。設定お疲れ様でした!

おわりに

HTTP/2(mod_http2)と PHP(mod_php) を合わせて使う場合は、mod_php が Prefork 以外の動作モードを推奨していないため、PHP を Fast-CGI形式で動作させるための仕組み PHP-FPM が必要になりそうですね。

コメント

タイトルとURLをコピーしました