Apache httpd 2.4.29 + HTTP/2 + Brotli インストールメモ

Linux
Linux
スポンサーリンク

 

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

2017年10月23日 Apache httpd2.4.29 がリリースされました。バージョン2.4.28のリリースからわずか18日でのリリースになります。ここ最近 Apache httpd 2.4系は、新機能の追加や脆弱性への対応を積極的に行なっているため(利用者にとってはありがたいことです)アップデート情報から目がはなせませんね。さっそく、CentOS7.4 (1708) に HTTP/2 と Brotli に対応した Apache httpd 2.4.29 をインストールする手順をまとめてみました。

 
参考資料:Changes with Apache 2.4.29

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

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

yum -y groupinstall base
yum -y groupinstall development
yum -y update

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 のダウンロード(ダウンロードの前にNghttp2の最新リリースを確認しておきましょう)

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

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

tar xvzf nghttp2-1.27.0.tar.gz
cd nghttp2-1.27.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

HTTP/2 のライブラリ「libnghttp2」が /usr/local/lib 以下にインストールされます。Nghttp2 関連のコマンドは /usr/local/bin 以下にインストールされます。(特にベンチマークツールの h2load が便利です)

Brotli のインストール

Brotli のコンパイルに cmake を使いますので、インストールしておきます。

yum -y install cmake

Brotli のダウンロード(ダウンロードの前に Brotliの最新リリースを確認しておきましょう)

cd /usr/local/src/
wget https://github.com/google/brotli/archive/v1.0.1.tar.gz

Brotli をコンパイルしてインストールします。

tar xvzf v1.0.1.tar.gz
cd brotli-1.0.1/
mkdir out && cd out
../configure-cmake
make
make test
make install

Brotli のライブラリが /usr/local/lib 以下にインストールされます。

ライブラリへのパス追加

HTTP/2 と Brotli のライブラリが「/usr/local/lib」以下にインストールされましたので、ライブラリのパスに追加しておきます。

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 が必要になりますので、インストールしておきます。

※2017年10月22日に APR と APR-util のアップデートがあったため、旧バージョンの1.6.2はダウンロードできなくなってますので注意です。

APR

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

APR-util

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

Apache httpd 2.4.29 のインストール

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

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

cd /usr/local/src/
wget http://ftp.jaist.ac.jp/pub/apache//httpd/httpd-2.4.29.tar.gz

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

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

HTTP/2 と Brotli モジュールと SSL/TLS を有効にしてインストールします。

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

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

自己署名のSSLサーバー証明書の作成

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

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

秘密鍵の作成(ECDSAの256ビット鍵を生成)

openssl ecparam -name prime256v1 -genkey -out 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

SELinux を有効にしている場合は、秘密鍵とSSL証明書にセキュリティコンテキストをつけておきましょう。(Apache 起動時にエラーが発生することがあります)

restorecon -v /etc/pki/tls/private/server.key
restorecon -v /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
(略)

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

この記事をシェアする
あぱーブログをフォローする
スポンサーリンク
スポンサーリンク

コメント

  1. CentOSユーザ より:

    あのー
    Apache 2.4.29 には
    OpenSSL も Nghttp2 も mod_http2 も全て予めインストール済みなのですが
    きちんと確認されてます?

    • >CentOSユーザさん
      コメントありがとうございます。

      Apache 2.4.29 を IUSリポジトリなどからインストールする場合は、
      CentOSユーザさんがおっしゃるように Nghttp2 などのインストール?は不要なのですが
      Apache 2.4.29 を自前でソースからコンパイルする場合は、この記事のような手順になります。

',b.captions&&s){var u=J("figcaption");u.id="baguetteBox-figcaption-"+t,u.innerHTML=s,l.appendChild(u)}e.appendChild(l);var c=J("img");c.onload=function(){var e=document.querySelector("#baguette-img-"+t+" .baguetteBox-spinner");l.removeChild(e),!b.async&&n&&n()},c.setAttribute("src",r),c.alt=a&&a.alt||"",b.titleTag&&s&&(c.title=s),l.appendChild(c),b.async&&n&&n()}}function X(){return M(o+1)}function D(){return M(o-1)}function M(e,t){return!n&&0<=e&&e=k.length?(b.animation&&O("right"),!1):(q(o=e,function(){z(o),V(o)}),R(),b.onChange&&b.onChange(o,k.length),!0)}function O(e){l.className="bounce-from-"+e,setTimeout(function(){l.className=""},400)}function R(){var e=100*-o+"%";"fadeIn"===b.animation?(l.style.opacity=0,setTimeout(function(){m.transforms?l.style.transform=l.style.webkitTransform="translate3d("+e+",0,0)":l.style.left=e,l.style.opacity=1},400)):m.transforms?l.style.transform=l.style.webkitTransform="translate3d("+e+",0,0)":l.style.left=e}function z(e){e-o>=b.preload||q(e+1,function(){z(e+1)})}function V(e){o-e>=b.preload||q(e-1,function(){V(e-1)})}function U(e,t,n,o){e.addEventListener?e.addEventListener(t,n,o):e.attachEvent("on"+t,function(e){(e=e||window.event).target=e.target||e.srcElement,n(e)})}function W(e,t,n,o){e.removeEventListener?e.removeEventListener(t,n,o):e.detachEvent("on"+t,n)}function G(e){return document.getElementById(e)}function J(e){return document.createElement(e)}return[].forEach||(Array.prototype.forEach=function(e,t){for(var n=0;n","http://www.w3.org/2000/svg"===(e.firstChild&&e.firstChild.namespaceURI)}(),m.passiveEvents=function i(){var e=!1;try{var t=Object.defineProperty({},"passive",{get:function(){e=!0}});window.addEventListener("test",null,t)}catch(n){}return e}(),function a(){if(r=G("baguetteBox-overlay"))return l=G("baguetteBox-slider"),u=G("previous-button"),c=G("next-button"),void(d=G("close-button"));(r=J("div")).setAttribute("role","dialog"),r.id="baguetteBox-overlay",document.getElementsByTagName("body")[0].appendChild(r),(l=J("div")).id="baguetteBox-slider",r.appendChild(l),(u=J("button")).setAttribute("type","button"),u.id="previous-button",u.setAttribute("aria-label","Previous"),u.innerHTML=m.svg?f:"<",r.appendChild(u),(c=J("button")).setAttribute("type","button"),c.id="next-button",c.setAttribute("aria-label","Next"),c.innerHTML=m.svg?g:">",r.appendChild(c),(d=J("button")).setAttribute("type","button"),d.id="close-button",d.setAttribute("aria-label","Close"),d.innerHTML=m.svg?p:"×",r.appendChild(d),u.className=c.className=d.className="baguetteBox-button",function n(){var e=m.passiveEvents?{passive:!1}:null,t=m.passiveEvents?{passive:!0}:null;U(r,"click",x),U(u,"click",E),U(c,"click",C),U(d,"click",B),U(l,"contextmenu",A),U(r,"touchstart",T,t),U(r,"touchmove",N,e),U(r,"touchend",L),U(document,"focus",P,!0)}()}(),S(e),function s(e,a){var t=document.querySelectorAll(e),n={galleries:[],nodeList:t};return w[e]=n,[].forEach.call(t,function(e){a&&a.filter&&(y=a.filter);var t=[];if(t="A"===e.tagName?[e]:e.getElementsByTagName("a"),0!==(t=[].filter.call(t,function(e){if(-1===e.className.indexOf(a&&a.ignoreClass))return y.test(e.href)})).length){var i=[];[].forEach.call(t,function(e,t){var n=function(e){e.preventDefault?e.preventDefault():e.returnValue=!1,H(i,a),I(t)},o={eventHandler:n,imageElement:e};U(e,"click",n),i.push(o)}),n.galleries.push(i)}}),n.galleries}(e,t)},show:M,showNext:X,showPrevious:D,hide:j,destroy:function e(){!function n(){var e=m.passiveEvents?{passive:!1}:null,t=m.passiveEvents?{passive:!0}:null;W(r,"click",x),W(u,"click",E),W(c,"click",C),W(d,"click",B),W(l,"contextmenu",A),W(r,"touchstart",T,t),W(r,"touchmove",N,e),W(r,"touchend",L),W(document,"focus",P,!0)}(),function t(){for(var e in w)w.hasOwnProperty(e)&&S(e)}(),W(document,"keydown",F),document.getElementsByTagName("body")[0].removeChild(document.getElementById("baguetteBox-overlay")),w={},h=[],o=0}}})
タイトルとURLをコピーしました