「インストール - Nginx(RHEL)」の版間の差分

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
ナビゲーションに移動 検索に移動
59行目: 59行目:
<br><br>
<br><br>


== インストールと起動 ==
== NginXのインストール ==
以下のコマンドを入力して、Nginxをインストールする。<br>
==== パッケージ管理システムからインストール ====
  sudo yum install nginx
まず、システムの更新を行う。<br>
  sudo dnf update
<br>
<br>
インストールしたNginxのバージョンを確認する。<br>
次に、NginXをインストールする。<br>
  nginx -v
NginXは、RHELの公式リポジトリからインストールできる。<br>
ただし、常に最新バージョンが提供されるとは限らない。<br>
  sudo dnf install nginx
<br>
<br>
NginXサービスを自動起動する。<br>
sudo systemctl enable nginx
<br>
NginXを開始する。<br>
sudo systemctl start nginx
<br>
NginXのステータスを確認する場合は、次のコマンドを実行する。<br>
sudo sytemctl status nginx
<br>
==== ソースコードからインストール ====
NginXのビルドに必要なライブラリをインストールする。<br>
sudo dnf install libxslt-devel pcre2-devel gd-devel
                  GeoIP-devel                        # GeoIPを使用する場合
                  libcurl-devel                      # Passengerを使用する場合
                  kernel-headers kernel-devel        # AIOを使用する場合
<br>
必要ならば、[https://www.openssl.org/source/ OpenSSLの公式Webサイト]にアクセスして、OpenSSL(1.X.Y)のソースコードをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
tar xf openssl-<バージョン>
<br>
* Digest認証モジュールを使用する場合
*: NginXのソースコードと一緒に配布されていないため、別途インストールする必要がある。<br>
*: [https://github.com/atomx/nginx-http-auth-digest Digest認証モジュールのGithub]にアクセスして、ソースコードをダウンロードする。<br>
*: ダウンロードしたファイルを解凍する。<br>
*: <code>tar xf nginx-http-auth-digest-<バージョン>.tar.gz</code>
*: <br>
*: または、<code>git clone</code>コマンドを実行して、ソースコードをダウンロードする。<br>
*: <code>git clone https://github.com/atomx/nginx-http-auth-digest.git</code>
*: <br>
* Passengerモジュールを使用する場合 (Rails連携)
*: NginXのソースコードと一緒に配布されていないため、別途インストールする必要がある。<br>
*: [https://www.phusionpassenger.com/docs/advanced_guides/install_and_upgrade/standalone/install/oss/tarball.html Passengerモジュールの公式webサイト]にアクセスして、ソースコードをダウンロードする。<br>
*: ダウンロードしたファイルを解凍する。<br>
*: <code>tar xf passenger-<バージョン>.tar.gz</code>
*: <code>cd passenger-<バージョン></code>
*: <br>
<br>
NginX 1.9.11以降から、<code>configure</code>スクリプト実行時において、<br>
<code>--add-module=<PATH></code>オプションの代わりに<code>--add-dynamic-module=<PATH></code>オプションを使用して、モジュールを動的モジュールとしてコンパイルすることができる。<br>
そして、<code>load_module</code>ディレクティブを使用して、nginx.confファイルで明示的にモジュールをロードすることができる。<br>
load_module /path/to/modules/<モジュール名>.so;
<br>
[https://nginx.org/en/download.html NginXの公式Webサイト]にアクセスして、NginXのソースコードをダウンロードする。<br>
ダウンロードしたファイルを解凍する。<br>
tar xf nginx-<バージョン>.tar.xz
cd nginx-<バージョン>
<br>
NginXをビルドおよびインストールする。<br>
<br>
Digest認証モジュールも併せてビルドおよびインストールする場合、<br>
<code>configure</code>スクリプトにおいて、<code>--add-module=<Digest認証モジュールのソースコードがあるディレクトリ></code>オプションを付加する。<br>
<br>
<u>NginXのビルドにおいて、ビルドディレクトリを作成すると失敗することに注意する。</u><br>
export NGINX_DIR=<NginXのインストールディレクトリ>
./configure \
--prefix=$NGINX_DIR \
--sbin-path=$NGINX_DIR/sbin/nginx \
--conf-path=$NGINX_DIR/etc/nginx.conf \
--pid-path=$NGINX_DIR/nginx.pid \
--modules-path=$NGINX_DIR/modules/ \
--error-log-path=$NGINX_DIR/log/error.log \
--http-log-path=$NGINX_DIR/log/access.log \
--lock-path=$NGINX_DIR/nginx.lock \
--http-client-body-temp-path=$NGINX_DIR/tmp/ \
--http-proxy-temp-path=$NGINX_DIR/proxy/ \
--http-fastcgi-temp-path=$NGINX_DIR/fastcgi/ \
--http-uwsgi-temp-path=$NGINX_DIR/uwsgi/ \
--http-scgi-temp-path=$NGINX_DIR/scgi/ \
--with-perl_modules_path=$NGINX_DIR/Perl \
--with-threads --with-file-aio --with-pcre --with-pcre-jit \
--with-http_v2_module --with-http_ssl_module --with-http_addition_module --with-http_realip_module --with-http_flv_module \
--with-http_random_index_module --with-http_degradation_module --with-http_slice_module  --with-http_dav_module --with-http_mp4_module \
--with-http_xslt_module --with-http_xslt_module=dynamic \
--with-http_image_filter_module --with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module \
--with-http_auth_request_module --with-http_secure_link_module --with-http_stub_status_module --with-http_sub_module \
--with-http_perl_module --with-http_perl_module=dynamic \
--with-mail --with-mail=dynamic --with-mail_ssl_module \
--with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module \
--with-compat \
--user=<任意のユーザ名  例. nginx> \
--group=<任意のグループ名  例. nginx>
# 不要の可能性あり
--without-poll_module --without-select_module --with-ipv6
# Digest認証を使用する場合
--add-module=<Digest認証モジュールのソースコードがあるディレクトリ>
# Passengerを使用する場合
--add-module=/<Passengerモジュールのディレクトリ>/src/nginx-module
# GCCコンパイラを指定する場合
--with-cc=/<gcc実行ファイルがあるディレクトリ>/gcc \
--with-cpp=/<g++実行ファイルがあるディレクトリ>/g++
# OpenSSLを手動で設定する場合に記述する
--with-openssl=<上記でダウンロードおよび解凍したOpenSSLのソースコードのトップディレクトリ>
# 以下の設定はAIOを使用する場合に記述する
--with-cc-opt='-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchron ous-unwind-tables -fstack-clash-protection -g -fPIC -D_GNU_SOURCE' \
--with-ld-opt='-Wl,-z,relro,-z,now -pie'
make -j $(nproc)
make install
<br>
configureスクリプト実行時において、<code>--user=<ユーザ名></code>および<code>--group=<グループ名></code>を付加した場合、それぞれユーザとグループを作成する必要がある。<br>
sudo useradd --system --no-create-home --user-group --shell /sbin/nologin nginx
<br>
Systemdサービスユニットを作成しない場合、NginXを開始するには、以下のコマンドを実行する。<br>
cd <NginXのインストールディレクトリ>/sbin
sudo ./nginx
<br>
Systemdサービスユニットを作成しない場合、NginXを停止するには、以下のコマンドを実行する。<br>
* 方法 1
*: <code>cd <NginXのインストールディレクトリ>/sbin</code>
*: <code>sudo ./nginx -s stop</code>
*: または
*: <code>sudo ./nginx -s quit</code>
*: <br>
* 方法 2
*: まず、NginXのプロセスIDを確認する。
*: <code>ps aux | grep -i nginx</code>
*: NginXを停止する。
*: <code>sudo kill <プロセスID 1> <プロセスID 2></code>
<br>
Systemdサービスユニットを作成しない場合、NginXを再起動するには、以下のコマンドを実行する。<br>
cd <NginXのインストールディレクトリ>/sbin
sudo ./nginx -s reload
# または
sudo ./nginx -s reopen
<br>
Systemdサービスユニットを作成する場合、/etc/systemd/systemディレクトリに、以下に示すような内容で作成する。<br>
sudo vi /etc/systemd/system/nginx.service
<br>
# /etc/systemd/system/nginx.serviceファイル
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
PIDFile=/<NginXのインストールディレクトリ>/nginx.pid
ExecStartPre=/<NginXのインストールディレクトリ>/sbin/nginx -t
ExecStart=/<NginXのインストールディレクトリ>/sbin/nginx -g "daemon off;"
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
# added automatically, for details please see
# https://en.opensuse.org/openSUSE:Security_Features#Systemd_hardening_effort
ProtectSystem=full
#ProtectHome=read-only
PrivateDevices=true
ProtectHostname=true
ProtectClock=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectKernelLogs=true
ProtectControlGroups=true
RestrictRealtime=true
# end of automatic additions
[Install]
WantedBy=multi-user.target
<br>
NginXを開始する。<br>
sudo systemctl start nginx
<br>
NginXを停止する。<br>
sudo systemctl stop nginx
<br>
NginXを再起動する。<br>
sudo systemctl restart nginx
<br>
NginXを自動起動する。<br>
sudo systemctl enable nginx
<br>
NginXの設定ファイルの内容を変更した時、変更を反映させるためにNginXを再読み込みする。<br>
sudo systemctl reload nginx
<br><br>
== NginXの設定 ==
自動起動設定とNginxの起動を行う。<br>
自動起動設定とNginxの起動を行う。<br>
  sudo systemctl enable nginx
  sudo systemctl enable nginx

2023年12月3日 (日) 04:47時点における版

概要

NginXとは、Apacheと並ぶWebサーバソフトウェアの1つであり、処理速度や機能面で注目を集めている。

NginXのメリットを、以下に示す。

  • 高速である。
  • 大量処理が得意である。
  • Webサイトの利用を向上させる機能が豊富である。
  • 設定が容易である。


Webサーバに同時に複数のアクセスがあった場合、下表に示すように動作の違いがある。

動作方法の違いによる処理速度の向上
Apache 2 NginX
同時・複数アクセスへの対処方法 1アクセスに対して、1つの対応 複数のアクセスに対して、1つの対応にまとまる
アクセス急増時のサーバへの負荷 一気に負荷が増す アクセスに比例して負荷は急激に増えない
その結果のWebサーバの挙動 動作が遅くなり、ダウンしやすくなる 処理速度を維持して、ダウンしにくい


例えば、Webサーバの負担を軽くして処理速度を上げる手段として、リバースプロキシやロードバランサー等があり、これらを実現する場合、Nginxが向いている。

NginXのデメリットを、以下に示す。

  • 大量の動的コンテンツの処理に不向き
    一般的には、大量の動的コンテンツの公開(動画を中心としたWebサイト等)には向かないことがNginXのデメリットとして挙げられる。
    ただし、比較的小規模のWebサイトを運営する場合は、それほど神経質にならなくてもよい。

  • 機能追加のしやすさ
    専用のモジュールを追加することにより、Nginxも機能を拡張することができる。
    ただし、Apacheの方が、実装方法について比較的充実し、情報が豊富でわかりやすい。

    Apache モジュール一覧
    https://httpd.apache.org/docs/trunk/ja/mod/
    NGINX 3rd Party Modules
    https://www.nginx.com/resources/wiki/modules/

  • 初心者向けの設定情報の少なさ
    設定等の情報が少ない。


参考書
q?_encoding=UTF8&ASIN=B01N183E3H&Format=_SL160_&ID=AsinImage&MarketPlace=JP&ServiceVersion=20070822&WS=1&tag=presire22-22&language=ja_JP
nginx実践ガイド
IT技術者のための現場ノウハウ
q?_encoding=UTF8&ASIN=4774178667&Format=_SL160_&ID=AsinImage&MarketPlace=JP&ServiceVersion=20070822&WS=1&tag=presire22-22&language=ja_JP
nginx実践入門
高速/安定稼働を実現する構築と運用のテクニック
q?_encoding=UTF8&ASIN=4774176338&Format=_SL160_&ID=AsinImage&MarketPlace=JP&ServiceVersion=20070822&WS=1&tag=presire22-22&language=ja_JP
Nginx ポケットリファレンス
q?_encoding=UTF8&ASIN=B078JP4HZN&Format=_SL160_&ID=AsinImage&MarketPlace=JP&ServiceVersion=20070822&WS=1&tag=presire22-22&language=ja_JP
Nginx HTTP Server
Nginxを利用して、高速なページ配信を実現する



NginXのインストール

パッケージ管理システムからインストール

まず、システムの更新を行う。

sudo dnf update


次に、NginXをインストールする。
NginXは、RHELの公式リポジトリからインストールできる。
ただし、常に最新バージョンが提供されるとは限らない。

sudo dnf install nginx


NginXサービスを自動起動する。

sudo systemctl enable nginx


NginXを開始する。

sudo systemctl start nginx


NginXのステータスを確認する場合は、次のコマンドを実行する。

sudo sytemctl status nginx


ソースコードからインストール

NginXのビルドに必要なライブラリをインストールする。

sudo dnf install libxslt-devel pcre2-devel gd-devel
                 GeoIP-devel                         # GeoIPを使用する場合
                 libcurl-devel                       # Passengerを使用する場合
                 kernel-headers kernel-devel         # AIOを使用する場合


必要ならば、OpenSSLの公式Webサイトにアクセスして、OpenSSL(1.X.Y)のソースコードをダウンロードする。
ダウンロードしたファイルを解凍する。

tar xf openssl-<バージョン>


  • Digest認証モジュールを使用する場合
    NginXのソースコードと一緒に配布されていないため、別途インストールする必要がある。
    Digest認証モジュールのGithubにアクセスして、ソースコードをダウンロードする。
    ダウンロードしたファイルを解凍する。
    tar xf nginx-http-auth-digest-<バージョン>.tar.gz

    または、git cloneコマンドを実行して、ソースコードをダウンロードする。
    git clone https://github.com/atomx/nginx-http-auth-digest.git

  • Passengerモジュールを使用する場合 (Rails連携)
    NginXのソースコードと一緒に配布されていないため、別途インストールする必要がある。
    Passengerモジュールの公式webサイトにアクセスして、ソースコードをダウンロードする。
    ダウンロードしたファイルを解凍する。
    tar xf passenger-<バージョン>.tar.gz
    cd passenger-<バージョン>


NginX 1.9.11以降から、configureスクリプト実行時において、
--add-module=<PATH>オプションの代わりに--add-dynamic-module=<PATH>オプションを使用して、モジュールを動的モジュールとしてコンパイルすることができる。
そして、load_moduleディレクティブを使用して、nginx.confファイルで明示的にモジュールをロードすることができる。

load_module /path/to/modules/<モジュール名>.so;


NginXの公式Webサイトにアクセスして、NginXのソースコードをダウンロードする。
ダウンロードしたファイルを解凍する。

tar xf nginx-<バージョン>.tar.xz
cd nginx-<バージョン>


NginXをビルドおよびインストールする。

Digest認証モジュールも併せてビルドおよびインストールする場合、
configureスクリプトにおいて、--add-module=<Digest認証モジュールのソースコードがあるディレクトリ>オプションを付加する。

NginXのビルドにおいて、ビルドディレクトリを作成すると失敗することに注意する。

export NGINX_DIR=<NginXのインストールディレクトリ>

./configure \
--prefix=$NGINX_DIR \
--sbin-path=$NGINX_DIR/sbin/nginx \
--conf-path=$NGINX_DIR/etc/nginx.conf \
--pid-path=$NGINX_DIR/nginx.pid \
--modules-path=$NGINX_DIR/modules/ \
--error-log-path=$NGINX_DIR/log/error.log \
--http-log-path=$NGINX_DIR/log/access.log \
--lock-path=$NGINX_DIR/nginx.lock \
--http-client-body-temp-path=$NGINX_DIR/tmp/ \
--http-proxy-temp-path=$NGINX_DIR/proxy/ \
--http-fastcgi-temp-path=$NGINX_DIR/fastcgi/ \
--http-uwsgi-temp-path=$NGINX_DIR/uwsgi/ \
--http-scgi-temp-path=$NGINX_DIR/scgi/ \
--with-perl_modules_path=$NGINX_DIR/Perl \
--with-threads --with-file-aio --with-pcre --with-pcre-jit \
--with-http_v2_module --with-http_ssl_module --with-http_addition_module --with-http_realip_module --with-http_flv_module \
--with-http_random_index_module --with-http_degradation_module --with-http_slice_module  --with-http_dav_module --with-http_mp4_module \
--with-http_xslt_module --with-http_xslt_module=dynamic \
--with-http_image_filter_module --with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module \
--with-http_auth_request_module --with-http_secure_link_module --with-http_stub_status_module --with-http_sub_module \
--with-http_perl_module --with-http_perl_module=dynamic \
--with-mail --with-mail=dynamic --with-mail_ssl_module \
--with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module \
--with-compat \
--user=<任意のユーザ名  例. nginx> \
--group=<任意のグループ名  例. nginx>

# 不要の可能性あり
--without-poll_module --without-select_module --with-ipv6

# Digest認証を使用する場合
--add-module=<Digest認証モジュールのソースコードがあるディレクトリ>

# Passengerを使用する場合
--add-module=/<Passengerモジュールのディレクトリ>/src/nginx-module

# GCCコンパイラを指定する場合
--with-cc=/<gcc実行ファイルがあるディレクトリ>/gcc \
--with-cpp=/<g++実行ファイルがあるディレクトリ>/g++

# OpenSSLを手動で設定する場合に記述する
--with-openssl=<上記でダウンロードおよび解凍したOpenSSLのソースコードのトップディレクトリ>

# 以下の設定はAIOを使用する場合に記述する
--with-cc-opt='-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchron ous-unwind-tables -fstack-clash-protection -g -fPIC -D_GNU_SOURCE' \
--with-ld-opt='-Wl,-z,relro,-z,now -pie'

make -j $(nproc)
make install


configureスクリプト実行時において、--user=<ユーザ名>および--group=<グループ名>を付加した場合、それぞれユーザとグループを作成する必要がある。

sudo useradd --system --no-create-home --user-group --shell /sbin/nologin nginx


Systemdサービスユニットを作成しない場合、NginXを開始するには、以下のコマンドを実行する。

cd <NginXのインストールディレクトリ>/sbin
sudo ./nginx


Systemdサービスユニットを作成しない場合、NginXを停止するには、以下のコマンドを実行する。

  • 方法 1
    cd <NginXのインストールディレクトリ>/sbin
    sudo ./nginx -s stop
    または
    sudo ./nginx -s quit

  • 方法 2
    まず、NginXのプロセスIDを確認する。
    ps aux | grep -i nginx
    NginXを停止する。
    sudo kill <プロセスID 1> <プロセスID 2>


Systemdサービスユニットを作成しない場合、NginXを再起動するには、以下のコマンドを実行する。

cd <NginXのインストールディレクトリ>/sbin
sudo ./nginx -s reload
# または
sudo ./nginx -s reopen


Systemdサービスユニットを作成する場合、/etc/systemd/systemディレクトリに、以下に示すような内容で作成する。

sudo vi /etc/systemd/system/nginx.service


# /etc/systemd/system/nginx.serviceファイル

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
PIDFile=/<NginXのインストールディレクトリ>/nginx.pid
ExecStartPre=/<NginXのインストールディレクトリ>/sbin/nginx -t
ExecStart=/<NginXのインストールディレクトリ>/sbin/nginx -g "daemon off;"
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

# added automatically, for details please see
# https://en.opensuse.org/openSUSE:Security_Features#Systemd_hardening_effort
ProtectSystem=full
#ProtectHome=read-only
PrivateDevices=true
ProtectHostname=true
ProtectClock=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectKernelLogs=true
ProtectControlGroups=true
RestrictRealtime=true
# end of automatic additions 

[Install]
WantedBy=multi-user.target


NginXを開始する。

sudo systemctl start nginx


NginXを停止する。

sudo systemctl stop nginx


NginXを再起動する。

sudo systemctl restart nginx


NginXを自動起動する。

sudo systemctl enable nginx


NginXの設定ファイルの内容を変更した時、変更を反映させるためにNginXを再読み込みする。

sudo systemctl reload nginx



NginXの設定

自動起動設定とNginxの起動を行う。

sudo systemctl enable nginx
sudo systemctl start nginx


デフォルトページの表示の確認をする。
http://CentOSのコンピュータ名/
上記のindex.htmlのデフォルトのパス

/usr/share/nginx/html/index.html

Nginxの設定ファイルのパス

/etc/nginx/conf.d/default.conf


簡単なリバースプロキシ
/etc/nginx/conf.d 以下に XXX.confという命名でファイルを作成する。
ココでは server.conf とし、80 番で受けたリクエストを 8000 番に転送する。

sudo nano /etc/nginx/conf.d/server.conf


server {
   listen 80;
   # アクセス可能なIPアドレスもしくはドメイン
   server_name hogehoge.com;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $http_host;
   proxy_redirect off;
   proxy_max_temp_file_size 0;
   location / {
      proxy_pass http://localhost:8000;
   }
}



Nginxの基本コマンド

起動

sudo systemctl start nginx


停止

sudo systemctl stop nginx


再起動

sudo systemctl restart nginx


再起動しても設定ファイルが反映されない場合

sudo nginx -s reload


状態の確認

sudo systemctl status nginx