LoginSignup
0
0

More than 5 years have passed since last update.

digest認証対応のnginxのrpmを作成して、itamaeでwebサーバーを更新する

Last updated at Posted at 2016-09-09

概要

前投稿でLDAP認証対応のnginxのrpmを作成したが、それにdigest認証を追加する

前準備

前投稿と同じ

nginxオフィシャルより Mainline version の
nginx-1.11.3.tar.gz
nginx-1.11.3.tar.gz.asc
と、
nginx-auth-ldap の gitリポジトリのクローンに
nginx-http-auth-digest の gitリポジトリのクローン を追加する


ビルド用サーバーで一連の作業を行う
また、特別な指示が無い限りは、ビルド作業は一般ユーザーアカウントで行い、rootでは行わないことなぜならば、途中の作業で失敗した時に、システムに実際の追加などが行われないということを確保するため

ビルドユーザーで追加ソースコードを集める

cd /usr/local/src/rpm_src/nginx
git  clone https://github.com/atomx/nginx-http-auth-digest.git
  • 今後の作業のためにnginx-http-auth-digestのソースコードは、tar.gzファイルにまとめる
tar -zcf nginx-http-auth-digest.tar.gz nginx-http-auth-digest
  • tar.gz にまとめたソースコードを使いコンパイルが通るかチェックする、コンパイルオプションはnginxのオフィシャルサイトのMain lineのConfigure Argumentsにする
# ビルドのチェックは /tmp で行う
tar -zxf nginx-1.11.3.tar.gz -C /tmp
tar -zxf nginx-auth-ldap.tar.gz -C /tmp/nginx-1.11.3/
tar -zxf nginx-http-auth-digest.tar.gz -C /tmp/nginx-1.11.3/

# ソースコードを展開したディレクトに移動し、configureする
cd /tmp/nginx-1.11.3

LDFLAGS="-L/usr/lib64" \
"./configure" \
"--prefix=/etc/nginx" \
"--sbin-path=/usr/sbin/nginx" \
"--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-http_ssl_module" \
"--with-http_realip_module" \
"--with-http_addition_module" \
"--with-http_sub_module" \
"--with-http_dav_module" \
"--with-http_flv_module" \
"--with-http_mp4_module" \
"--with-http_gunzip_module" \
"--with-http_gzip_static_module" \
"--with-http_random_index_module" \
"--with-http_secure_link_module" \
"--with-http_stub_status_module" \
"--with-http_auth_request_module" \
"--with-threads" \
"--with-stream" \
"--with-stream_ssl_module" \
"--with-http_slice_module" \
"--with-mail" \
"--with-mail_ssl_module" \
"--with-file-aio" \
"--with-http_v2_module" \
"--with-ipv6" \
"--add-module=nginx-auth-ldap" \
"--add-module=nginx-http-auth-digest"

# configure が通れば、makeまですすめる
make

Main lineのConfigure Argumentsに加え、明確に64bitコンパイルにするために、LDFLAGS="-L/usr/lib64" とldap対応のために、--add-module=nginx-auth-ldap、digest認証対応のために、 --add-module=nginx-http-auth-digestを追加する

rpm作成の本作業

  • rpm作成のために、specファイルを作成する

詳細は前回同様

nginx_myrpm.spec
Summary: Nginx webserver (with ssl, ldap-auth, digest-auth)
Name: nginx_myrpm
Version: 1.11.3
Release: 2.el7
License: BSD-like
Group: Web-server-environment
Source0: nginx-1.11.3.tar.gz
Source1: nginx-auth-ldap.tar.gz
Source2: nginx
Source3: nginx.service
Source4: nginx-http-auth-digest.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
BuildRequires: openldap-devel,openssl-devel,zlib-devel,pcre-devel
Requires: openldap,openssl,zlib,pcre
Conflicts: nginx

%description
nginx webserver with ssl, nginx-auth-ldap, nginx-http-auth-digest

%prep
[ -d $RPM_BUILD_ROOT ] && rm -rf $RPM_BUILD_ROOT;

%setup -a 1 -a 4 -q -n nginx-%{version}

%build
[ -d $RPM_BUILD_ROOT ] && rm -rf $RPM_BUILD_ROOT;
LDFLAGS="-L/usr/lib64" \
"./configure" \
"--prefix=/etc/nginx" \
"--sbin-path=/usr/sbin/nginx" \
"--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-http_ssl_module" \
"--with-http_realip_module" \
"--with-http_addition_module" \
"--with-http_sub_module" \
"--with-http_dav_module" \
"--with-http_flv_module" \
"--with-http_mp4_module" \
"--with-http_gunzip_module" \
"--with-http_gzip_static_module" \
"--with-http_random_index_module" \
"--with-http_secure_link_module" \
"--with-http_stub_status_module" \
"--with-http_auth_request_module" \
"--with-threads" \
"--with-stream" \
"--with-stream_ssl_module" \
"--with-http_slice_module" \
"--with-mail" \
"--with-mail_ssl_module" \
"--with-file-aio" \
"--with-http_v2_module" \
"--with-ipv6" \
"--add-module=nginx-auth-ldap" \
"--add-module=nginx-http-auth-digest"
make



%install
[ -d $RPM_BUILD_ROOT ] && rm -rf $RPM_BUILD_ROOT;
make install DESTDIR=${RPM_BUILD_ROOT}
mkdir -p ${RPM_BUILD_ROOT}/etc/logrotate.d/
mkdir -p ${RPM_BUILD_ROOT}/usr/lib/systemd/system/
mkdir -p ${RPM_BUILD_ROOT}/var/lib/nginx/tmp
mkdir -p ${RPM_BUILD_ROOT}/var/log/nginx
mkdir -p ${RPM_BUILD_ROOT}/var/cache/nginx
install -m 644 %{SOURCE2} ${RPM_BUILD_ROOT}/etc/logrotate.d/
install -m 755 %{SOURCE3} ${RPM_BUILD_ROOT}/usr/lib/systemd/system/

%clean
[ -d $RPM_BUILD_ROOT ] && rm -rf $RPM_BUILD_ROOT;

%pre
/usr/bin/getent group nginx || /sbin/groupadd nginx
/usr/bin/getent passwd nginx || /sbin/useradd nginx -g nginx -s /sbin/nologin -d /etc/nginx -c "Nginx web server"

%postun
/sbin/userdel nginx -rf

%files
%defattr(644,nginx,nginx)
   /etc/nginx/html/
   /etc/logrotate.d/nginx
   /etc/nginx/fastcgi.conf
   /etc/nginx/fastcgi.conf.default
   /etc/nginx/fastcgi_params
   /etc/nginx/fastcgi_params.default
   /etc/nginx/html/50x.html
   /etc/nginx/html/index.html
   /etc/nginx/koi-utf
   /etc/nginx/koi-win
   /etc/nginx/mime.types
   /etc/nginx/mime.types.default
   /etc/nginx/nginx.conf
   /etc/nginx/nginx.conf.default
   /etc/nginx/scgi_params
   /etc/nginx/scgi_params.default
   /etc/nginx/uwsgi_params
   /etc/nginx/uwsgi_params.default
   /etc/nginx/win-utf
   /var/lib/nginx/
   /var/lib/nginx/tmp
   /var/log/nginx/
   /var/cache/nginx/
%defattr(755,nginx,nginx)
   /usr/lib/systemd/system/nginx.service
   /usr/sbin/nginx

%changelog
* Fri Sep 9 2016 Hirotaka Tajiri <ganryu_koziro@excite.co.jp> 1-11-3.2
- Add Source4 : nginx-http-auth-digest.tar.gz 
- Modify nginx_myrpm.spec: Add configure option(--add-module=nginx-http-auth-digest)
- Modify nginx_myrpm.spec: Modify %pre | %postun

* Thu Sep 1 2016 Hirotaka Tajiri <ganryu_koziro@excite.co.jp> 1-11-3.1
- first build

◇specファイルの主な編後点

□ Source4: nginx-http-auth-digest.tar.gz の追加と%setupへの追加
□ コンパイルオプション --add-module=nginx-http-auth-digest の追加
□ %pre 部分のユーザー/グループの追加に条件を設定
□ Summary / %description の変更
  • specファイルの作成が完了したら、ソース類、specファイルのシンボリックリンクを作業ディレクトリに作成する
# ソースコード
rm -f ~/rpmbuild/SOURCES/*
rm -f ~/rpmbuild/SPECS/*
rm -rf ~/rpmbuild/BUILD/*


ln -s /usr/local/src/rpm_src/nginx/nginx ~/rpmbuild/SOURCES/
ln -s /usr/local/src/rpm_src/nginx/nginx.service ~/rpmbuild/SOURCES/
ln -s /usr/local/src/rpm_src/nginx/nginx-auth-ldap.tar.gz ~/rpmbuild/SOURCES/
ln -s /usr/local/src/rpm_src/nginx/nginx-1.11.3.tar.gz ~/rpmbuild/SOURCES/
ln -s /usr/local/src/rpm_src/nginx/nginx-http-auth-digest.tar.gz ~/rpmbuild/SOURCES/

# specファイル
ln -s /usr/local/src/rpm_src/nginx/nginx_myrpm.spec ~/rpmbuild/SPECS
  • rpmbuildコマンドでrpmファイルを作成する

細かいbpmbuildオプションの詳しい説明は割愛

cd ~/rpmbuild/SPECS

# rpm作成
rpmbuild -bb nginx_myrpm.spec
  • rpmの確認作業

エラーでストップしなければ、作成終了しているので、rpmパッケージの確認を行う

# 64bitパッケージなので~/rpmbuild/RPMS/x86_64に作成されている
cd ~/rpmbuild/RPMS/x86_64

# rpm インフォメーションの確認
rpm -qpi nginx_myrpm-1.11.3-2.el7.x86_64.rpm 

# rpm含有ファイルの確認
rpm -qpl nginx_myrpm-1.11.3-2.el7.x86_64.rpm 

yumリポジトリ化

  • rpmファイルをリポジトリディレクトリにコピーし、リポジトリ化する
# rootでコピー
su -
cd /var/lib/myrepo_dir/el7

# rpmファイルのコピー(ビルドしたユーザーをxxxと仮定する)
cp /home/xxx/rpmbuild/RPMS/x86_64/nginx_myrpm-1.11.3-2.el7.x86_64.rpm x86_64/

# リポジトリ化
createrepo .

  • yumで検索してみる
# yum chache の初期化(myrepoのみ)
yum clean all --disablerepo=* --enablerepo=myrepo

# 検索
yum search nginx_myrpm

#=> 読み込んだプラグイン:fastestmirror, langpacks
#=> myrepo              | 2.9 kB  00:00:00     
#=> myrepo/primary_db   | 3.9 kB  00:00:00     
#=> Determining fastest mirrors
#=>  * base: mirror.fairway.ne.jp
#=>  * extras: mirror.fairway.ne.jp
#=>  * remi-safe: mirror.innosol.asia
#=>  * updates: mirror.fairway.ne.jp
#=> ================================ N/S matched: nginx_myrpm =================================
#=> nginx_myrpm.x86_64 : Nginx webserver (with ssl, ldap-auth, digest-auth)
#=> 
#=>   Name and summary matches only, use "search all" for everything.

itamaeを使い、管理サーバーよりWebサーバーのnginxを更新する

本稿で使用したレシピはgithubに上げてある

アップデートだが、アップデート専用のレシピ適用ではなく、削除/新規追加とする

  • nginx_myrpm削除用レシピを作成する
    • nginx のプロセス停止/自動起動の停止
    • nginx_myrpmを削除
    • systemctlデーモンでファイルを再読み込みさせ、クリア
    • yumのcacheファイルを削除
cd ~/itamae_cookbooks/nginx
uninstall_nginx_myrpm.rb
service "nginx" do
    action [:stop, :disable]
end

package "nginx_myrpm" do
    action :remove
end

[
    "systemctl daemon-reload",
    "yum clean all",
].each{| com |
    execute com do
        action  :run
        command com
    end
}

(管理サーバーでitamaeを実行する)

itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx uninstall_nginx_myrpm.rb

  • digest認証を追加するにあたり、digest認証パスワードファイルを管理サーバーで作成し、webサーバーに配布するので、digest認証パスワードファイルを作成する

    • digest認証をパスワード作成には、htdigestコマンドを使用するのでhttpd-toolsパッケージが必要
    yum install httpd-tools 
    
    • digest認証パスワードファイルを作成する
     htdigest -c files/nginx.digest 'digest realm' hoge
    
     New password: hogehoge
     Re-type new password:hogehoge
    
  • nginx_myrpm追加用レシピを更新する(主な変更点)

    • digest認証を設定するディレクトリ追加
    • digest認証パスワードファイル追加
    • nginx.confにdigest認証の設定追加
    • digest認証を設定するディレクトリにindex.htmlを追加
nginx_myrpm.rb
package "nginx_myrpm" do
    action :install
end

[
    "/var/www",
    "/var/www/ldap_auth",
    "/etc/nginx/html/auth",
].each {| dir |
    directory dir do
       action :create
       owner  "nginx"
       group  "nginx"
       mode   "755"
    end
}

[
    "server.crt",
    "server.key",
    "nginx.conf",
    "nginx.digest",
].each {| file |
    template "/etc/nginx/#{file}" do
        action :create
        owner  "nginx"
        group  "nginx"
        mode   "644"
        source "files/#{file}"
    end
}

[
    "index",
    "404",
    "50x",
].each{| com |
    execute "404" do
        action  :run
        command "echo \"#{com}\" > /var/www/ldap_auth/#{com}.html;chmod 666 /var/www/ldap_auth/#{com}.html"
        not_if  "test -e /var/www/ldap_auth/#{com}.html"
    end
}

execute "digest index" do
    action  :run
    command "echo 'digest auth OK' > /etc/nginx/html/auth/index.html"
    not_if  "test -e /etc/nginx/html/auth/index.html"
end

service "nginx" do
    action [:enable, :start]
end

(管理サーバーでitamaeを実行する)

itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx nginx_myrpm.rb

確認作業

  • webサーバー構築の確認

ブラウザで構築済みのサーバー(http://192.168.56.102/auth) にアクセスし確認する
スクリーンショット 2016-09-09 20.28.46.png

ダイアログに、digest認証パスワードファイル作成で登録した、hoge/hogehoge入力すると認証をパスできる
スクリーンショット 2016-09-09 20.29.03.png

httpヘッダーを確認すると、digest認証をおこなっていることがわかる

スクリーンショット 2016-09-09 20.29.14.png

0
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
0
0