使用itamae创建支持digest认证的nginx的rpm,并通过itamae更新web服务器
概览
我之前为LDAP认证准备了一个nginx的RPM包,现在想在此基础上添加摘要认证。
预备工作
与前一次投稿相同
从Nginx官方网站上下载Mainline版本的nginx-1.11.3.tar.gz和nginx-1.11.3.tar.gz.asc文件,并把nginx-auth-ldap和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中添加,为了明确地进行64位编译,还需要添加LDFLAGS=”-L/usr/lib64″以及支持ldap,添加–add-module=nginx-auth-ldap,为了支持digest认证,添加–add-module=nginx-http-auth-digest。
制作rpm的操作步骤
- rpm作成のために、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ファイルを作成する
詳細な説明は省略する
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
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を追加
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头,可以看出进行了摘要认证。
