CentOS 7でNginxをソースrpmからビルドしてインストールする方法

MiniTool_ad

CentOS 7でNginxをソースrpmからビルドしてインストールする方法

CentOS 7のNginx(Mainline 1.15.8)で「TLS1.3」や「nginx-cache-purge」モジュールを利用したくなり、Nginxをソースrpmからリビルドしてパッケージインストールしてみましたので、その手順を紹介したいと思います。

CentOS 7にmainline版のNginxをインストールする方法
ここでは、CentOS 7にmainline版のNginxをインストールする方法、stable版のNginxをmainline版へアップデートする方法を紹介します。

動作環境

この記事は、以下の環境での動作結果を基にしています。他のディストリビューションやバージョンでは、動作結果が異なる場合があることをご了承ください。

ソフトウェアバージョン
CentOS(Minimal)7.6.1810

ソースインストールとの違い

パッケージをカスタマイズしたいときや、いち早く最新バージョンのソフトウェアを利用したい場合の、ソフトウェアのインストール方法には、今回紹介しているソースrpmからパッケージ化してインストールする以外に「ソースインストール」という方法があります。

どちらにも一長一短ありますが、パッケージ化のメリットの一つに流用しやすいという点があります。

たとえば、複数台のマシンにソフトウェアを一括インストールしたい場合、ここで紹介している手順でソフトウェアをパッケージ化しておけば、出来上がったrpmファイルをそれぞれのマシンに配布して実行するだけで、カスタマイズしたソフトウェアをインストールできますが、ソースインストールだと一台ずつ面倒なインストール作業を実施する必要があります。

私の場合、複数台のCentOS 7マシンでWebサーバー(Nginx)を稼働させており、すべてのマシンのNginxを同一の内容にしたかったので、Nginxをソースrpmからリビルドしてパッケージ化することにしました。

rpmビルド手順

事前準備

ここからの作業は、Webサーバー上で実施してもよいですし、ビルド用マシンを別途用意してもよいです。

rpmビルドに必要なパッケージのインストール

まず、rpmビルドに必要となるパッケージをインストールしておきます。

# yum -y install rpmdevtools redhat-lsb-core zlib-devel pcre-devel gcc

ここでは、Nginxのソースrpmをビルドするのに必要なパッケージを記載しています。

rpmビルド用ユーザーの作成

rpmのビルドを実行するユーザを作成します。ここでは「rpmbuilder」という名前のユーザーを作成しています。

# useradd rpmbuilder

ユーザーを作成したら、そのユーザーに切り替えて「rpmdev-setuptree」コマンドを実行し、ディレクトリ作成など、ビルドを実行するための初期設定を行います。

# su - rpmbuilder
$ rpmdev-setuptree

ソースの準備

次に、必要となるソースをインターネットからダウンロードし、インストールor解凍しておきます。

実行場所は、先ほど作成したユーザー「rpmbuilder」のホームディレクトリで構いません。

Nginx

$ curl -L -O https://nginx.org/packages/mainline/centos/7/SRPMS/nginx-1.15.8-1.el7_4.ngx.src.rpm
$ rpm -ivh nginx-1.15.8-1.el7_4.ngx.src.rpm

OpenSSL

$ curl -L -O https://www.openssl.org/source/openssl-1.1.1a.tar.gz
$ tar xvfz openssl-1.1.1a.tar.gz

ngx_cache_purge

$ curl -L -O http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
$ tar xvfz ngx_cache_purge-2.3.tar.gz

specファイルの修正

次に、specファイルを修正します。

$ vi rpmbuild/SPECS/nginx.spec

修正箇所1

OpenSSLの要求バージョンが記載されている29-30行目と33-34行目をコメントアウトします。

BuildRequires: systemd
%define os_minor %(lsb_release -rs | cut -d '.' -f 2)
%if %{os_minor} >= 4
#Requires: openssl >= 1.0.2
#BuildRequires: openssl-devel >= 1.0.2
%define dist .el7_4
%else
#Requires: openssl >= 1.0.1
#BuildRequires: openssl-devel >= 1.0.1
%define dist .el7
%endif
%endif

修正箇所2

Nginxに組み込むモジュールなどの情報が記載されている58行目の末尾に「OpenSSL」と「ngx_cache_purge」の記述を追加して、以下のように修正します。

%define BASE_CONFIGURE_ARGS $(echo "--prefix=%{_sysconfdir}/nginx --sbin-path=%{_sbindir}/nginx --modules-path=%{_libdir}/nginx/modules --conf-path=%{_sysconfdir}/nginx/nginx.conf --error-log-path=%{_localstatedir}/log/nginx/error.log --http-log-path=%{_localstatedir}/log/nginx/access.log --pid-path=%{_localstatedir}/run/nginx.pid --lock-path=%{_localstatedir}/run/nginx.lock --http-client-body-temp-path=%{_localstatedir}/cache/nginx/client_temp --http-proxy-temp-path=%{_localstatedir}/cache/nginx/proxy_temp --http-fastcgi-temp-path=%{_localstatedir}/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=%{_localstatedir}/cache/nginx/uwsgi_temp --http-scgi-temp-path=%{_localstatedir}/cache/nginx/scgi_temp --user=%{nginx_user} --group=%{nginx_group} --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl=/home/rpmbuilder/openssl-1.1.1a --add-module=/home/rpmbuilder/ngx_cache_purge-2.3")

rpmパッケージをビルド

ホームディレクトリに移動して、以下のコマンドでビルドを開始します。

$ rpmbuild -bb rpmbuild/SPECS/nginx.spec

ビルド完了までは、10分ぐらいかかります。

処理が正常終了すると、最終行付近は以下のように表示されているはずです。

書き込み完了: /home/rpmbuilder/rpmbuild/RPMS/x86_64/nginx-1.15.8-1.el7_4.ngx.x86_64.rpm
書き込み完了: /home/rpmbuilder/rpmbuild/RPMS/x86_64/nginx-debuginfo-1.15.8-1.el7_4.ngx.x86_64.rpm
実行中(%clean): /bin/sh -e /var/tmp/rpm-tmp.AI5c6s
+ umask 022
+ cd /home/rpmbuilder/rpmbuild/BUILD
+ cd nginx-1.15.8
+ /usr/bin/rm -rf /home/rpmbuilder/rpmbuild/BUILDROOT/nginx-1.15.8-1.el7_4.ngx.x86_64
+ exit 0

なお、rpmパッケージは以下のディレクトリに生成されています。

$ /home/rpmbuilder/rpmbuild/RPMS/x86_64/nginx-1.15.8-1.el7_4.ngx.x86_64.rpm

Nginxのインストール

あとは、ビルドしたrpmパッケージをWebサーバーでインストールするだけです。

# yum install nginx-1.15.8-1.el7_4.ngx.x86_64.rpm
Memo

nginx.repoなどを作成して、yumでnginxをインストール・更新していた場合は、nginx.repoの無効化をお忘れなく。自分でビルドしてインストールしたパッケージを自動更新してしまうと、カスタマイズした箇所が無くなります。

組み込みモジュールの確認

インストールが完了したら、以下のコマンドでOpensslのバージョンや、ngx_cache_purgeが組み込まれていることを確認します。

# nginx -V
nginx version: nginx/1.15.8
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.1.1a  20 Nov 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl=/home/rpmbuilder/openssl-1.1.1a --add-module=/home/rpmbuilder/ngx_cache_purge-2.3 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

あとがき

インストールが完了したら、サービスの自動起動などの設定も忘れずに。