LoginSignup
1
1

More than 5 years have passed since last update.

nginx 公式SPRMからRebuildする

Posted at

nginx 公式SPRMからRebuildする

目的

Apache httpdだとyumでサクッとインストールできるのですが
nginxではモジュールが3rdpartyで完全にオプションのため
nginx公式Yumレポジトリのものにできるだけ近いかたちでカスタムしたRPMを作ることになりました。

今回の要件

  • LDAP対応
  • HTTP/2対応
  • SSLライブラリ埋め込み
  • 複数サーバへ展開するのと後々バージョンあげるかもしれないのでお手軽にrpm化
  • CentOS7向け

LDAPモジュールは、nginx-auth-ldap
HTTP/2の規格に完全対応するためopensslライブラリが一定以上のバージョンが必須です。
しかし、CentOS7の標準レポジトリでは配布されてないバージョンなので組み込みが必須です。

そのようなこともあり、必要なソースパッケージを集めたりするのがいちいち面倒なのでそこはnginx-buildをダウンローダーとして使っています。

rpmbuildはDockerのコンテナ内でやるとさらにお手軽です。

注意事項

  • pcre
  • zlib

以上のパッケージも組み込めるが、特に困ってないのでOSの標準にあるパッケージを使用してコンパイルしています。

手順

1.必要なものをダウンロード

  1. nginx-buildのバイナリをダウンロードしてHOMEディレクトリへ解凍
  2. buildしたいnginx-SRPMのバージョンを公式からダウンロードwget http://nginx.org/packages/centos/7/SRPMS/nginx-1.12.1-1.el7.ngx.src.rpm
  3. HOMEディレクトリでnginx-SRPMをインストールするとファイルが展開されますrpm -Uvh {SRPM_PATH}
  4. 必要な依存パッケージ(OS標準のもの)をインストール sudo yum install pcre-devel.x86_64 zlib-devel.x86_64 openldap-devel.x86_64
  5. nginx-buildで最新版のものを使うものをダウンロードします。./nginx-build -v 1.12.1 -m module_3rdparty.ini -openssl -clear -configureonly -d work
//module_3rdparty.ini
[nginx-auth-ldap]
form=git
url=https://github.com/kvspb/nginx-auth-ldap
rev=master

nginx-buildでダウンロードするとこんなディレクトリになります。

$ ls -la work/nginx/1.12.1/
nginx-1.12.1/ nginx-1.12.1.tar.gz nginx-auth-ldap/ nginx-auth-ldap.log openssl-1.0.2n/ openssl-1.0.2n.tar.gz

moduleのディレクトリはこんな感じです

ls -la work/nginx/1.12.1/nginx-auth-ldap/
total 100
drwxrwxrwx 3 builder builder 113 Apr 16 11:43 .
drwxr-xr-x 5 builder builder 148 Apr 16 11:43 ..
drwxrwxrwx 8 builder builder 152 Apr 16 11:43 .git
-rw-rw-rw- 1 builder builder 1507 Apr 16 11:43 LICENSE
-rw-rw-rw- 1 builder builder 3537 Apr 16 11:43 README.md
-rw-rw-rw- 1 builder builder 672 Apr 16 11:43 config
-rw-rw-rw- 1 builder builder 1590 Apr 16 11:43 example.conf
-rw-rw-rw- 1 builder builder 83529 Apr 16 11:43 ngx_http_auth_ldap_module.c

2.BUILDの準備

  1. cd work/nginx/1.12.1/
  2. tar cvzf ~/rpmbuild/SOURCES/nginx-auth-ldap.tar.gz nginx-auth-ldap
  3. cp openssl-1.0.2l.tar.gz ~/rpmbuild/SOURCES/

3.SPECを編集する

やっていることは

  • opensslは同梱させるのでrequireを外す
  • module追加するのでそれのオプションを追加
    • --add-module=nginx-auth-ldap --with-openssl=openssl-1.0.2l --with-openssl-opt='-fPIC'"
  • build時に同梱させるのでそのオプションを追加
    • Source14: nginx-auth-ldap.tar.gz
    • Source15: openssl-1.0.2l.tar.gz
    • %setup -a 14 -a 15 -q -n nginx-%{version}

setupマクロのオプションの意味についてはこちらが参考になりました。
https://vinelinux.org/docs/vine6/making-rpm/setup-macro.html

以上の内容のdiffはこちらになります。

diff -u nginx.spec rpmbuild/SPECS/nginx.spec
--- nginx.spec 2017-07-11 22:49:34.000000000 +0900
+++ rpmbuild/SPECS/nginx.spec 2017-09-12 18:53:00.060241472 +0900
@@ -12,8 +12,8 @@
Requires(pre): shadow-utils
Requires: initscripts >= 8.36
Requires(post): chkconfig
-Requires: openssl
-BuildRequires: openssl-devel
+#Requires: openssl
+#BuildRequires: openssl-devel
%endif
%if 0%{?rhel} == 6
@@ -21,8 +21,8 @@
Requires(pre): shadow-utils
Requires: initscripts >= 8.36
Requires(post): chkconfig
-Requires: openssl >= 1.0.1
-BuildRequires: openssl-devel >= 1.0.1
+#Requires: openssl >= 1.0.1
+#BuildRequires: openssl-devel >= 1.0.1
%endif
%if 0%{?rhel} == 7
@@ -31,9 +31,9 @@
Epoch: %{epoch}
Requires(pre): shadow-utils
Requires: systemd
-Requires: openssl >= 1.0.1
+#Requires: openssl >= 1.0.1
BuildRequires: systemd
-BuildRequires: openssl-devel >= 1.0.1
+#BuildRequires: openssl-devel >= 1.0.1
%endif
%if 0%{?suse_version} == 1315
@@ -41,7 +41,7 @@
%define nginx_loggroup trusted
Requires(pre): shadow
Requires: systemd
-BuildRequires: libopenssl-devel
+#BuildRequires: libopenssl-devel
BuildRequires: systemd
%endif
@@ -55,7 +55,7 @@
%define WITH_CC_OPT $(echo %{optflags} $(pcre-config --cflags)) -fPIC
%define WITH_LD_OPT -Wl,-z,relro -Wl,-z,now -pie
-%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")
+%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 --add-module=nginx-auth-ldap --with-openssl=openssl-1.0.2l --with-openssl-opt='-fPIC'")
Summary: High performance web server
Name: nginx
@@ -78,6 +78,8 @@
Source11: nginx-debug.service
Source12: COPYRIGHT
Source13: nginx.check-reload.sh
+Source14: nginx-auth-ldap.tar.gz
+Source15: openssl-1.0.2l.tar.gz

License: 2-clause BSD-like license

@@ -96,7 +98,7 @@
%endif

%prep
-%setup -q
+%setup -a 14 -a 15 -q -n nginx-%{version}
cp %{SOURCE2} .
sed -e 's|%%DEFAULTSTART%%|2 3 4 5|g' -e 's|%%DEFAULTSTOP%%|0 1 6|g' \
-e 's|%%PROVIDES%%|nginx|g' < %{SOURCE2} > nginx.init

4.build実行

  1. 実行ユーザはbuildユーザなのでそのユーザに変更してください
  2. cd ~/rpmbuild && rpmbuild -ba --clean SPECS/nginx.spec
  3. エラーがなければRPMS/の中にrpmファイルができあがります
1
1
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
1