1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOS 6.10 で nginx をHTTP/2対応にする(パッケージ作成編)

Last updated at Posted at 2019-03-26

例によって個人的なメモ。
誤字脱字はご容赦。改善点やツッコミは歓迎。編集リクエストください。

概要

この記事は、必要に迫られて(2019.3.26時点の)最新版のnginx 1.15.9をソースからコンパイルし、ついでにHTTP/2に対応したいので OpenSSL 1.0.2r をリンクしてRPMパッケージを作る記事です。

2019.03.28 タイトルを修正し、動作確認手順を追加

環境

CentOS 6.10 x86_64

追加パッケージのインストール

前回やってるので、そのまま転用w

# yum groupinstall "Development Tools"
# yum install pcre-devel openssl-devel libxslt-devel gd-devel perl-ExtUtils-Embed epel-release

RPMパッケージを作るための準備

rpmdevtoolsをインストールし、ビルド専用ユーザ builder を作成し su する。

# sudo yum install rpmdevtools yum-utils
# useradd builder
# su - builder

builderユーザで、rpmdev-setuptreeを実行し各種ディレクトリを作成。これ便利。
できた rpmbuild ディレクトリを確認しておく。

$ cd
$ rpmdev-setuptree 
$ ls -al rpmbuild

ソースの準備

nginx公式にSRPMパッケージがあるので、ダウンロードする
今回はCentoOS6用のパッケージをダウンロードしておく。CentOS7の方はURLが違うので適宜。CentOS5の人は知らんw

OpenSSLの最新版もダウンロードしておく。

両方ともバージョンごとに名前が違う(当たり前)なので、バージョンが変わったら適宜。

$ wget http://nginx.org/packages/mainline/centos/6/SRPMS/nginx-1.15.9-1.el6.ngx.src.rpm
$ wget https://www.openssl.org/source/openssl-1.0.2r.tar.gz

ダウンロードしたら、それぞれ展開しておく。

$ rpm -ivh ./nginx-1.15.9-1.el6.ngx.src.rpm
$ tar zxvf ./openssl-1.0.2r.tar.gz

SPECファイルの編集

既にOSに入っているOpenSSL1.0.1を使用しないため、該当箇所をコメントアウトする。
さらに、configureオプションでOpenSSL1.0.2rの場所を指定することで、静的にリンクしてビルドする。

$ vim ./rpmbuild/SPECS/nginx.spec 

nginxのバージョンが古いとこの分岐がないために一瞬迷うが、OSバージョンによる分岐のようなので、★の行の先頭に#を入れてコメントアウトする。

(./rpmbuild/SPECS/nginx.spec)15~16行目付近
%if 0%{?rhel} == 6
%define _group System Environment/Daemons
Requires(pre): shadow-utils
Requires: initscripts >= 8.36
Requires(post): chkconfig
# Requires: openssl >= 1.0.1 ★
# BuildRequires: openssl-devel >= 1.0.1 ★
%endif

続いて、58行目付近にconfigureオプションの記載があるので、行末に以下のOpenSSL1.0.2のパスとオプションを追加する。
--with-openssl=/home/builder/openssl-1.0.2r --with-openssl-opt=-fPIC
OpenSSLの展開後パスは適宜指定。
「--with-openssl-opt=-fPIC」は指定しないと、コンパイル時にエラーが発生してビルドが成功しないため必ず指定する。

(./rpmbuild/SPECS/nginx.spec)58行目付近
%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/builder/openssl-1.0.2r --with-openssl-opt=-fPIC")

長い…。

コンパイル & パッケージのビルド

環境によっては結構時間が掛かる。お茶でも飲んでエラーが出ないことを祈りつつ待つ。

$ rpmbuild -bb rpmbuild/SPECS/nginx.spec -D 'debug_package %{nil}'

ビルドの確認

無事にビルドが終わったらパッケージを確認する。
RPMが出来ていればほぼ成功。

$ ls -al ./rpmbuild/RPMS/x86_64/

nginxインストールと確認

インストールはrootに戻ってyumで。rpm -ivhとかでもいい。

# yum install /home/builder/rpmbuild/RPMS/x86_64/nginx-1.15.9-1.el6.ngx.x86_64.rpm

built with OpenSSL1.0.2rになっていることを確認する。

# nginx -V
nginx version: nginx/1.15.9
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) 
built with OpenSSL 1.0.2r  26 Feb 2019
TLS SNI support enabled
configure arguments:(省略)

動作確認

設定ファイル(適宜)のserverディレクティブ内、listenhttp2を加えるだけ。

server {

    listen       80;
    listen 443 ssl http2;
    server_name  _;
    (以下略)

もちろんサーバ証明書とか秘密鍵は適宜適切に設定すること。(詳しくは割愛)

Chomeのデベロッパーツールで、SchemehttpsProtocolh2になっていれば大成功!
失敗していると、Protocolhttp/1.1になっている。

最後に

今回はSPECファイル中のバージョンとかサマリ表記は変更していないが、記録を残すという意味ではきちんとのこしておくべき。
ついでに、ビルド時の手順も残しておかないと、後世が苦労するのよね…。(苦労している世代)

参考にしたページ

CentOS 6にopenssl1.0.2以上を使用したnginx1.10以上を入れる
https://nekko1119.hatenablog.com/entry/2018/05/07/094614

nginxをHTTP/2対応にする方法(Chrome 51以降でも有効にする)
https://qiita.com/bj1024/items/3a4029f7ca2383d7a641

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?