Nginx をソースビルドに変更

Linux
この記事は約9分で読めます。
この記事は最終更新日より 1 年以上経過しています。
画像やリンクが無効になっている可能性もあるのでご了承下さい。

 Nginx オフィシャルのリポジトリから yum でインストールしていたけど、やはり Proxy Cache を WordPress からも Purge したいと思うとモジュールの追加は必須になる。モジュールを追加するという事はソースからのビルドが必須になるからどうしようもない。
 仕方が無いのでソースビルドに置き換えることにした。


 必要な物は Nginx のソースと、追加するモジュール ngx_cache_purge のソースになる。
 現在、Nginx は Mainline 版の 1.9.9、ngx_cache_purge は 2.3 となっている。

 インストールまでの作業は一般ユーザで行ったのでパスは一部伏せることに。

$ wget 'http://nginx.org/download/nginx-1.9.9.tar.gz'
$ tar xzvf nginx-1.9.9.tar.gz

$ wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
$ tar xzvf ngx_cache_purge-2.3.tar.gz

$ cd ./nginx-1.9.9

 各ソースのダウンロードと展開が終わったら nginx のソースがあるディレクトリに移動する。
 configure のオプションは既にインストールされている RPM 版ビルドオプション nginx -V にて表示される物のパスを一部変更しつつ、モジュールのソースがあるパスを指定する。

$ nginx -V
nginx version: nginx/1.9.9
built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --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-ipv6 --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

 確認が終わったら yum でインストールしてあった nginx のパッケージをアンインストールする。/etc/nginx のバックアップは別途とっておくこと。

$ sudo yum erase nginx

 変更箇所は –prefix と末尾に追加したモジュールパスの 2 点。

$ ./configure --prefix=/usr/local/nginx-1.9.9 --sbin-path=/usr/local/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-ipv6 --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --add-module=/path/to/ngx_cache_purge-2.3
$ make
$ sudo make install

 configure や make で足りないパッケージがあればその都度 yum で入れていけば良い。今回はエラーは何も無しですんなり通った。
 systemd で起動する為のファイルを設置する。これもまた RPM 版の物を再利用してパスをビルド時の prefix に合わせた物になる。

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 起動ユニットのファイルを作成したら自動起動するようにしておこう。

# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

 設定ファイル /etc/nginx 以下には nginx.conf.rpmsave などと残っていたりするので、リネームするなりバックアップからまるっと復元させておけばそのまま使える。
 一部設定ファイルは mime.type.default 等と、末尾に .default が付いたままの物が残るから使用しないのであれば削除する。
 nginx -t で設定ファイルに問題がなければ systemctl start nginx すると今までパッケージ版で動いていたままに動作するハズだ。

 ngx_cache_purge は使いたいバーチャルホストのフロントエンドに次の様な記述を行う。zone_name はキャッシュゾーン名を合わせること。

server {
(略
    location ~ /purge(/.*) {
        allow 127.0.0.1;
        allow 192.168.1.0/24;
        deny all;
        proxy_cache_purge zone_name $scheme://$host$1$is_args$args;
    }
}

 ブラウザから例えば http://example.com/purge/ とアクセスすればルートのキャッシュが削除されるし、WordPress であれば http://example.com/purge/archives/12345 なんて感じにするとアーカイブ個別のキャッシュを削除する事も出来る。
 WordPress からの利用方法はまた後日、別の記事として書きたい。

著者プロフィール
ぶっち

本格的に PC へ触れ始めてたのは 1990 年位から。
興味は PC 全般。OS は Windows と Linux などを嗜む。
プログラマやネットワークエンジニアを経てフリーに活動している 2 児の父な 40 代半ばのおじさんです。

ぶっちをフォローする

コメント

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.