http_realip_moduleとstreamに興味があるのでソースからnginxをインストールしてみる。

http_realip_moduleとstreamの内容は次回。まずはインストールから。

nginxはstable版とmainline版があるが今回はmainline版をインストールする。公式ドキュメントのココを参考にインストールするが、起動スクリプトなど書かれていない部分はrpmパッケージを参考に設定する。

まずrpm版をインストールして、次にソースからインストールする。

環境

  • CentOS 7.2.1511
  • Nginx 1.11.4

rpm版のインストール

設定を参考にしたいのでrpmで最新版をインストールする。

レポジトリの登録とインストール

以下のレポジトリを登録するだけでmainlineの最新版がインストールできる。
stable版がインストールしたい場合は、urlのmainlineを削除して http://nginx.org/packages/centos/7/$basearch/ とすればstableがインストールできる。

$ cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
$ yum install nginx -y
$ rpm -qa | grep nginx
nginx-1.10.1-1.el7.ngx.x86_64

ファイルなどの確認

rpmファイルから展開されたファイルを確認する。

$ rpm -qs nginx
normal /etc/logrotate.d/nginx
normal /etc/nginx
normal /etc/nginx/conf.d
normal /etc/nginx/conf.d/default.conf
normal /etc/nginx/fastcgi_params
normal /etc/nginx/koi-utf
normal /etc/nginx/koi-win
normal /etc/nginx/mime.types
normal /etc/nginx/modules
normal /etc/nginx/nginx.conf
normal /etc/nginx/scgi_params
normal /etc/nginx/uwsgi_params
normal /etc/nginx/win-utf
normal /etc/sysconfig/nginx
normal /etc/sysconfig/nginx-debug
normal /usr/lib/systemd/system/nginx-debug.service
normal /usr/lib/systemd/system/nginx.service
normal /usr/lib64/nginx
normal /usr/lib64/nginx/modules
normal /usr/libexec/initscripts/legacy-actions/nginx
normal /usr/libexec/initscripts/legacy-actions/nginx/upgrade
normal /usr/sbin/nginx
normal /usr/sbin/nginx-debug
normal /usr/share/doc/nginx-1.10.1
normal /usr/share/doc/nginx-1.10.1/COPYRIGHT
normal /usr/share/nginx
normal /usr/share/nginx/html
normal /usr/share/nginx/html/50x.html
normal /usr/share/nginx/html/index.html
normal /var/cache/nginx
normal /var/log/nginx

nginx.serviceとかlogrotateのファイルを確認しておく。

$ cat /lib/systemd/system/nginx.service
[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/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/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

$ cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}

nginxユーザも追加されるのでhomeディレクトリなどを確認しておく。

$ cat /etc/passwd | grep nginx
nginx:x:995:993:nginx user:/var/cache/nginx:/sbin/nologin

nginxのhomeディレクトリは/var/cache/nginxだそうです。

ソースからインストール

ユーザを追加してコンパイルしてインストールする。

$ useradd -s /sbin/nologin -d /var/cache/nginx -c "nginx user" nginx

$ cat /etc/passwd | grep nginx
nginx:x:1001:1001:nginx user:/var/cache/nginx:/sbin/nologin

UIDとGIDは自動的に設定されたものを使う。

使うものは一部だが、とりあえず全モジュールをインストールする。
引数はココを参照した。

$ yum install wget gcc pcre-devel openssl-devel zlib-devel -y
$ wget http://nginx.org/download/nginx-1.11.4.tar.gz
$ tar zxfv nginx-1.11.4.tar.gz && cd nginx-1.11.4
$ ./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

$ make && make install

インストールはこれだけ。

インストールできるモジュールは以下のコマンドで一覧表示できる。

$ ./configure --help

--help print this message

--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname

--user=USER set non-privileged user for
worker processes
--group=GROUP set non-privileged group for
worker processes

--build=NAME set build name
--builddir=DIR set build directory
(...)

大量に表示されるが、先頭が--withで始まっているものはデフォルトで組み見込まれないモジュールで、先頭が--withoutで始まっているものがデフォルトで組み込まれるっぽいです。

以下、ビルド時に出るエラーについて記載しておく。minimalなCentOSを使うと./configureでは以下のエラーがでるので必要なモジュールをインストールする。

./configure: error: C compiler cc is not found

$ yum install gcc -y

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

$ yum install pcre-devel -y

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

$ yum install openssl-devel -y

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

$ yum install zlib-devel -y

設定と起動

./configureで設定した内容に合わせてserviceファイルを編集する。

nginx.confはデフォルトのまま使う。

$ cat /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target

起動する。

$ systemctl enable nginx
$ systemctl start nginx.service

確認

workerがnginxユーザで起動されていることが確認できる。

$ ps axu | grep nginx
root 28627 0.0 0.0 47836 1152 ? Ss 23:25 0:00 nginx: master process /usr/sbin/nginx
nginx 28628 0.0 0.1 48280 1928 ? S 23:25 0:00 nginx: worker process

ブラウザで接続するか、以下のコマンドを叩くと/etc/nginx/html/にあるindex.htmlが表示される。

$ curl localhost

htmlディレクトリはrpm版だと/usr/share/nginx/htmlだが/etc/nginx/に設置されているためconfigureにhtmlディレクトリを設置するディレクトリを指定するオプションがあると思って探してみたがなかった。

アンインストール

make uninstallが無いのでnginx -Vで./configureで指定したディレクトリを調べてファイルを全て削除する。

$ make uninstall
make: *** No rule to make target `uninstall'. Stop.

$ nginx -V
nginx version: nginx/1.11.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (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-は省略

プロセスを停止してから次のディレクトリとファイルを全て削除する。

  • /etc/nginx
  • /usr/sbin/nginx
  • /var/log/nginx
  • /var/run/nginx.*
  • /var/cache/nginx
$ systemctl stop nginx
$ systemctl disable nginx
$ rm -rf /etc/nginx /usr/sbin/nginx /var/log/nginx \
/var/run/nginx.* /var/cache/nginx

おわり。

参考

  1. http://nginx.org/en/linux_packages.html
  2. https://www.nginx.com/resources/wiki/start/topics/examples/systemd/