NGINX コミュニティ版のインストール

◆ Live配信スケジュール ◆
サイオステクノロジーでは、Microsoft MVPの武井による「わかりみの深いシリーズ」など、定期的なLive配信を行っています。
⇒ 詳細スケジュールはこちらから
⇒ 見逃してしまった方はYoutubeチャンネルをご覧ください
【4/18開催】VSCode Dev Containersで楽々開発環境構築祭り〜Python/Reactなどなど〜
Visual Studio Codeの拡張機能であるDev Containersを使ってReactとかPythonとかSpring Bootとかの開発環境をラクチンで構築する方法を紹介するイベントです。
https://tech-lab.connpass.com/event/311864/

こんにちは。サイオステクノロジー OSS サポート担当 Y です。

今回は NGINX をソースコードからインストールしてみます。(※以下の内容は CentOS 7.4/NGINX 1.12.2 にて検証しています。)

■はじめに

NGINX は高速かつ軽量な Web/Proxy サーバとして知名度が上がってきており、最近はシェアが拡大しているという話も多く耳にします。

また、コミュニティ版には含まれていない追加機能を提供している商用版の NGINX Plus という製品があり、この NGINX Plus は開発元である NGINX 社がサポートサービスも提供しているため、通常の OSS 製品よりエンタープライズ領域に採用するハードルが低くなっているのも魅力の一つではないでしょうか。

今更な部分があるかもしれませんが、今回はコミュニティ版の NGINX をソースコードからインストールしてみました。

■検証

ではさっそくインストール作業を行なってみます。まずはソースコードの入手からですが、NGINX のソースコードは以下の URL から入手可能です。

(https://nginx.org/en/download.html)

今回は /usr/local/src の配下にソースコードをダウンロード及び展開して作業を行います。

[root@nginx ~]# cd /usr/local/src/
[root@nginx src]# 
[root@nginx src]# curl -O https://nginx.org/download/nginx-1.12.2.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  958k  100  958k    0     0   221k      0  0:00:04  0:00:04 --:--:--  222k
[root@nginx src]# 
[root@nginx src]# ls -l
合計 960
-rw-r--r-- 1 root root 981687  3月 13 16:12 nginx-1.12.2.tar.gz
[root@nginx src]# 
[root@nginx src]# tar xzf nginx-1.12.2.tar.gz 
[root@nginx src]# 
[root@nginx src]# ls -l
合計 960
drwxr-xr-x 8 1001 1001    158 10月 17 22:16 nginx-1.12.2
-rw-r--r-- 1 root root 981687  3月 13 16:12 nginx-1.12.2.tar.gz

ソースコードのダウンロード及び展開が完了したら、展開されたディレクトリ “nginx-1.12.2” 内にある configure スクリプトを実行します。

[root@nginx src]# cd nginx-1.12.2/
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# ls -l
合計 700
-rw-r--r-- 1 1001 1001 278202 10月 17 22:16 CHANGES
-rw-r--r-- 1 1001 1001 423948 10月 17 22:16 CHANGES.ru
-rw-r--r-- 1 1001 1001   1397 10月 17 22:16 LICENSE
-rw-r--r-- 1 1001 1001     49 10月 17 22:16 README
drwxr-xr-x 6 1001 1001    326  3月 13 16:13 auto
drwxr-xr-x 2 1001 1001    168  3月 13 16:13 conf
-rwxr-xr-x 1 1001 1001   2481 10月 17 22:16 configure
drwxr-xr-x 4 1001 1001     72  3月 13 16:13 contrib
drwxr-xr-x 2 1001 1001     40  3月 13 16:13 html
drwxr-xr-x 2 1001 1001     21  3月 13 16:13 man
drwxr-xr-x 9 1001 1001     91  3月 13 16:13 src

なお、configure スクリプトのオプションについては、以下の URL に記載されています。

(https://nginx.org/en/docs/configure.html)

また、上記オプションについては以下のコマンドでも確認可能です。

[root@nginx nginx-1.12.2]# ./configure --help

ということで実際に configure スクリプトを実行するとさっそくエラーになってしまいました。そうですね… メッセージの通り C コンパイラがインストールされていません…

[root@nginx nginx-1.12.2]# ./configure 
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found

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

ということで gcc をインストールします。実はこのタイミングで併せてインストールしておいた方が良いパッケージがあるのですが、今回はインストール検証なので一つずつエラーを眺めながら作業を実施してみようと思います。

[root@nginx nginx-1.12.2]# yum install -y gcc

~(中略)~

Installed:
  gcc.x86_64 0:4.8.5-16.el7_4.2

Dependency Installed:
  cpp.x86_64 0:4.8.5-16.el7_4.2           glibc-devel.x86_64 0:2.17-196.el7_4.2
  glibc-headers.x86_64 0:2.17-196.el7_4.2 kernel-headers.x86_64 0:3.10.0-693.21.1.el7
  libmpc.x86_64 0:1.0.1-3.el7

Dependency Updated:
  glibc.x86_64 0:2.17-196.el7_4.2   glibc-common.x86_64 0:2.17-196.el7_4.2
  libgcc.x86_64 0:4.8.5-16.el7_4.2  libgomp.x86_64 0:4.8.5-16.el7_4.2

Complete!
[root@nginx nginx-1.12.2]# 

それでは再度 configure スクリプトを実行してみます。すると今度は別のエラーが出力されました。

# ./configure 
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 

~(中略)~

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-https_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= option.

エラーの内容を見ると、”PCRE library” が必要なようです。ということで、pcre-devel パッケージをインストールします。

[root@nginx nginx-1.12.2]# yum install -y pcre-devel

~(中略)~

Installed:
  pcre-devel.x86_64 0:8.32-17.el7

Complete!
[root@nginx nginx-1.12.2]# 

pcre-devel のインストールが完了したらもう一度 configure スクリプトを実行してみます。すると、またまたエラーが出力されました。

[root@nginx nginx-1.12.2]# ./configure 
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 

~(中略)~

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-https_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= option.

今度は “zlib library” が必要だと言われているようです。ということで zlib-devel パッケージをインストールします。

[root@nginx nginx-1.12.2]# yum install -y zlib-devel

~(中略)~

Installed:
  zlib-devel.x86_64 0:1.2.7-17.el7

Complete!
[root@nginx nginx-1.12.2]# 

さあ、zlib-devel のインストールが終わったら、めげずに configure スクリプトを実行してみます。すると、今度は無事 configure スクリプトの実行が完了しました!

[root@nginx nginx-1.12.2]# ./configure 
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 

~(中略)~

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx https access log file: "/usr/local/nginx/logs/access.log"
  nginx https client request body temporary files: "client_body_temp"
  nginx https proxy temporary files: "proxy_temp"
  nginx https fastcgi temporary files: "fastcgi_temp"
  nginx https uwsgi temporary files: "uwsgi_temp"
  nginx https scgi temporary files: "scgi_temp"

このままでも以降の作業は実施可能なのですが、よくよく見ると “OpenSSL library is not used” と出力されています。ソースコードからインストールする場合、デフォルトだと SSL は利用できない状態になっているようですが、常時 SSL 化という言葉が叫ばれる昨今なので、SSL を利用できるようにし、通信に HTTPS も使える形でビルドしたいと思います。

SSL を有効にしてビルドする場合は configure スクリプトに “–with-https_ssl_module” を付けて実行します。が、ここでもエラーが出力されました。

[root@nginx nginx-1.12.2]# ./configure --with-https_ssl_module
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 

~(中略)~

./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= option.

“OpenSSL library” が必要であると言われていますね。ということで、今度は openssl-devel パッケージをインストールします。

[root@nginx nginx-1.12.2]# yum install -y openssl-devel

~(中略)~

Installed:
  openssl-devel.x86_64 1:1.0.2k-8.el7

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7    krb5-devel.x86_64 0:1.15.1-8.el7
  libcom_err-devel.x86_64 0:1.42.9-10.el7     libkadm5.x86_64 0:1.15.1-8.el7
  libselinux-devel.x86_64 0:2.5-11.el7        libsepol-devel.x86_64 0:2.5-6.el7
  libverto-devel.x86_64 0:0.2.5-4.el7

Complete!
[root@nginx nginx-1.12.2]# 

openssl-devel のインストールが終わったら、再度 “–with-https_ssl_module” オプションを付けて configure スクリプトを実行します。

[root@nginx nginx-1.12.2]# ./configure --with-https_ssl_module
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 

~(中略)~

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx https access log file: "/usr/local/nginx/logs/access.log"
  nginx https client request body temporary files: "client_body_temp"
  nginx https proxy temporary files: "proxy_temp"
  nginx https fastcgi temporary files: "fastcgi_temp"
  nginx https uwsgi temporary files: "uwsgi_temp"
  nginx https scgi temporary files: "scgi_temp"

すると、無事 configure スクリプトの実行が完了し、”using system OpenSSL library” と出力されているため、SSL が利用できる状態になっているようです。また、configure スクリプト実行後の各出力からバイナリファイル等のインストール先 (デフォルトの場合 /usr/local/nginx 配下) が確認できます。(詳細は割愛しますが、このインストール先のパスは configure スクリプトのオプションで指定可能です。configure –help で確認してみて下さい。)

configure スクリプトの実行が完了したら、make コマンドでビルドします。(環境によっては “yum install -y make” で make コマンドをインストールする必要があるかもしれません。)

[root@nginx nginx-1.12.2]# make
make -f objs/Makefile
make[1]: Entering directory `/usr/local/src/nginx-1.12.2'

~(中略)~

make[1]: Leaving directory `/usr/local/src/nginx-1.12.2'
[root@nginx nginx-1.12.2]# 

make コマンドが完了したら make install にてビルドしたバイナリ等をインストールします。(インストール先は、configure スクリプト実行時に出力されていた /usr/local/nginx 配下です。)

[root@nginx nginx-1.12.2]# make install
make -f objs/Makefile install
make[1]: Entering directory `/usr/local/src/nginx-1.12.2'

~(中略)~

make[1]: Leaving directory `/usr/local/src/nginx-1.12.2'
[root@nginx nginx-1.12.2]# 

インストールが完了したらインストール先のディレクトリを確認してみます。すると、実行ファイルや設定ファイルのサンプル等が確認できると思います。

[root@nginx nginx-1.12.2]# ls -l /usr/local/nginx/
total 0
drwxr-xr-x 2 root root 333 Mar 13 16:56 conf
drwxr-xr-x 2 root root  40 Mar 13 16:56 html
drwxr-xr-x 2 root root   6 Mar 13 16:56 logs
drwxr-xr-x 2 root root  19 Mar 13 16:56 sbin
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# ls -l /usr/local/nginx/sbin/
total 5476
-rwxr-xr-x 1 root root 5604736 Mar 13 16:56 nginx
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# ls -l /usr/local/nginx/conf/
total 60
-rw-r--r-- 1 root root 1077 Mar 13 16:56 fastcgi.conf
-rw-r--r-- 1 root root 1077 Mar 13 16:56 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Mar 13 16:56 fastcgi_params
-rw-r--r-- 1 root root 1007 Mar 13 16:56 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Mar 13 16:56 koi-utf
-rw-r--r-- 1 root root 2223 Mar 13 16:56 koi-win
-rw-r--r-- 1 root root 3957 Mar 13 16:56 mime.types
-rw-r--r-- 1 root root 3957 Mar 13 16:56 mime.types.default
-rw-r--r-- 1 root root 2656 Mar 13 16:56 nginx.conf
-rw-r--r-- 1 root root 2656 Mar 13 16:56 nginx.conf.default
-rw-r--r-- 1 root root  636 Mar 13 16:56 scgi_params
-rw-r--r-- 1 root root  636 Mar 13 16:56 scgi_params.default
-rw-r--r-- 1 root root  664 Mar 13 16:56 uwsgi_params
-rw-r--r-- 1 root root  664 Mar 13 16:56 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Mar 13 16:56 win-utf
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# ls -l /usr/local/nginx/html/
total 8
-rw-r--r-- 1 root root 537 Mar 13 16:56 50x.html
-rw-r--r-- 1 root root 612 Mar 13 16:56 index.html

実行ファイルに -v や -V オプションを付けて実行すると、バージョン情報等を確認することが可能です。

[root@nginx nginx-1.12.2]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.12.2
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-https_ssl_module

また、インストール直後の状態でも上記設定ファイルを使って起動可能なので、試しに起動してみましょう。(“-c” は設定ファイルを指定するオプションです。)

[root@nginx nginx-1.12.2]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# ps -ef | grep -e PID -e nginx | grep -v grep
UID        PID  PPID  C STIME TTY          TIME CMD
root     10538     1  0 17:01 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody   10539 10538  0 17:01 ?        00:00:00 nginx: worker process

ps コマンドで確認すると、nginx プロセスが動作していることが確認できます。また、ps コマンドの結果から worker process が nobody ユーザで動作していることが確認できます。

そのため、nginx ユーザを作成し、nginx ユーザで worker process が動作するように設定しておきます。まずは nginx ユーザを作成しますが、このユーザでサーバにログインする必要は無いので、”-s /sbin/nologin” と “-M” オプションを付けておきます。

[root@nginx nginx-1.12.2]# useradd -s /sbin/nologin -M nginx
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# tail -n 1 /etc/passwd
nginx:x:1001:1001::/home/nginx:/sbin/nologin

次に、NGINX の設定ファイル “nginx.conf” にて、worker process の実行ユーザを指定するディレクティブ “user” に、作成した nginx ユーザを指定します。

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# head -n 3 /usr/local/nginx/conf/nginx.conf

user  nginx;
worker_processes  1;
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# diff /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.default 
2c2
< user nginx; --- > #user  nobody;

設定ファイルの編集が完了したら、”nginx -s reload” コマンドで設定ファイルを再読み込みします。すると、worker process の実行ユーザが nginx ユーザになっていることが確認できます。

[root@nginx nginx-1.12.2]# ps -ef | grep nginx | grep -v grep
root     10538     1  0 17:01 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody   10539 10538  0 17:01 ?        00:00:00 nginx: worker process
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s reload
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# ps -ef | grep nginx | grep -v grep
root     10538     1  0 17:01 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx    22995 10538  0 17:14 ?        00:00:00 nginx: worker process

また、この状態で NGINX が listen している 80番ポートに対してリクエストを送信すると、デフォルトの index ファイルの内容がレスポンスとして出力されると思います。(ブラウザからアクセスすると綺麗に見えると思います。)

[root@nginx nginx-1.12.2]# netstat -lnp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10538/nginx: master 
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# curl https://127.0.0.1:80/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="https://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

ということで、インストール作業はここまでです。(ちなみに、nginx プロセスを停止させる場合は “nginx -s stop” コマンドを実行します。)

■最後に

さて、今回は NGINX をソースコードからインストールしただけですが、NGINX は主に Web サーバ/Reverse Proxy サーバとしての様々な機能を有しています。次回以降、この NGINX の機能を実際に検証してみようと思います。

アバター画像
About サイオステクノロジーの中の人 41 Articles
サイオステクノロジーで働く中の人です。
ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

0人がこの投稿は役に立ったと言っています。


ご覧いただきありがとうございます。
ブログの最新情報はSNSでも発信しております。
ぜひTwitterのフォロー&Facebookページにいいねをお願い致します!



>> 雑誌等の執筆依頼を受付しております。
   ご希望の方はお気軽にお問い合わせください!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


質問はこちら 閉じる