ばーろぐわにる

SIerからWEB系?インフラエンジニアにジョブチェンジした見習いの備忘録。投稿内容は私個人の意見であり、所属企業・部門見解を代表するものではありません。

nginxにnginx-auth-ldapを導入したカスタムパッケージを作成する

目的

  • nginxでnginx-auth-ldapが導入された状態のカスタムパッケージを作成する
    • 一番最後にDockerfileとしてまとめています

ビルド用コンテナ準備&起動

docker pull ubuntu:latest
docker run -it --rm ubuntu:latest /bin/bash

パッケージビルドする準備

ngixはデフォルトで登録されているパッケージソースからはダウンロードできないため、nginx用のパッケージソースを追加する

apt-get update
apt-get -y install git wget
wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key

キー追加で以下エラー

E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operatio

gnupgを追加して再実行

apt-get install -y gnupg
apt-key add nginx_signing.key

パッケージソース追加。毎回ヒアドキュメントの使い方忘れる。。

cat << EOS > /etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/ubuntu/ bionic nginx
EOS
apt-get update

apt-get build-depで コンパイルに必要なパッケージをダウンロードする

APT HOWTO (Obsolete Documentation) - ソースパッケージでの作業

apt-get build-dep -y nginx

apt-get sourceソースコードを取得

apt-get source -y nginx

nginx-auth-ldapをgit clone

git clone https://github.com/kvspb/nginx-auth-ldap.git

パッケージ作成

ビルド時にnginx-auth-ldapを追加するように設定

sed -i 's/\(CFLAGS=.*\)/\1 --add-module=\/nginx-auth-ldap\//' ./nginx-1.16.0/debian/rules

dpkg-buildpackage でビルド。-bオプションでバイナリパッケージ(.deb)のみ作成

cd nginx-1.16.0 && dpkg-buildpackage -b

ldap.hがないと言われる

...
/nginx-auth-ldap//ngx_http_auth_ldap_module.c:33:10: fatal error: ldap.h: No such file or directory
 #include <ldap.h>
          ^~~~~~~~
...

ぐぐったら libldap2-dev に含まれてそうなのでインストールする(i386の検索結果だけどまあよし)

Debian -- Package Contents Search Results -- ldap.h

apt-get install -y libldap2-dev
dpkg-buildpackage -b

成功した。 -dbg の方はデバッグ用で使うパッケージ?

付録A 上級パッケージング

ls -alt /*.deb
-rw-r--r-- 1 root root 5710928 Aug 21 02:59 /nginx-dbg_1.16.0-1~trusty_amd64.deb
-rw-r--r-- 1 root root  864768 Aug 21 02:59 /nginx_1.16.0-1~trusty_amd64.deb

インストール

dpkg -i ./nginx_1.16.0-1~trusty_amd64.deb

確認

nginx -V
nginx version: nginx/1.16.0
built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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-cc-opt='-g -O2 -fdebug-prefix-map=/nginx-1.16.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=/nginx-auth-ldap/

実際にLDAPサーバとやりとりできるかは確認できていないので次の機会に

Dockerfile

これまでの手順をまとめたDockerfile

# Create custom build package
FROM ubuntu:18.04
ENV NGINX_VERSION="1.16.0"
RUN apt-get update && apt-get -y install git wget gnupg libldap2-dev
RUN wget -q http://nginx.org/keys/nginx_signing.key
RUN apt-key add nginx_signing.key
RUN echo 'deb http://nginx.org/packages/ubuntu/ bionic nginx \n\
    deb-src http://nginx.org/packages/ubuntu/ bionic nginx \n'\
    >> etc/apt/sources.list.d/nginx.list
RUN apt-get update
RUN apt-get build-dep -y nginx=${NGINX_VERSION}
RUN apt-get source -y nginx=${NGINX_VERSION}
RUN git clone https://github.com/kvspb/nginx-auth-ldap.git
RUN sed -i 's/\(CFLAGS=.*\)/\1 --add-module=\/nginx-auth-ldap\//' ./nginx-${NGINX_VERSION}/debian/rules
RUN cd nginx-${NGINX_VERSION} && dpkg-buildpackage -b

# Main Image
FROM ubuntu:18.04
COPY --from=0 /nginx_${NGINX_VERSION}-1~bionic_amd64.deb ./
RUN apt-get update && apt-get install -y libldap-2.4
RUN dpkg -i ./nginx_${NGINX_VERSION}-1~bionic_amd64.deb
CMD ["nginx", "-g", "daemon off;"]

TIPS

  • Debianだと昔はgit-coreというパッケージ名だったけど、今はgitを使ってる。git-coreは廃止予定

  • apt-get installでドキュメントをインストールしたくないなら apt-get --no-install-recommends install で。ただし、recommended pakagesなのでドキュメント以外も含まれる

  • trusty, bionicはUbuntuのコードネーム

    • 最初間違えてパッケージソースの指定をtrustyで進めてしまったけどエラーが出なかった
    • Releases - Ubuntu Wiki
  • gnupg