インストール - NginX(SUSE)

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
ナビゲーションに移動 検索に移動

概要

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/

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



NginXのインストール方法

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

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

sudo zypper update


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

sudo zypper install nginx


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

sudo systemctl enable nginx


NginXを開始する。

sudo systemctl start nginx


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

sudo sytemctl status nginx


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

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

sudo zypper install libxslt-devel pcre2-devel gd-devel
sudo zypper install kernel-source kernel-devel  # AIOを使用する場合


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

tar xf openssl-<バージョン>


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

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


NginXをビルドおよびインストールする。
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_xslt_module --with-http_xslt_module=dynamic \
--with-http_image_filter_module --with-http_image_filter_module=dynamic --with-http_dav_module --with-http_mp4_module --with-http_flv_module \
--with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_degradation_module --with-http_slice_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 \
--with-cc=/usr/bin/gcc --with-cpp=/usr/bin/g++ \
--user=<任意のユーザ名> \  # 任意のユーザ名 例.nginx
--group=<任意のグループ名> # 任意のグループ名 例.nginx
# 不要の可能性あり
--without-poll_module --without-select_module --with-ipv6
# 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



ファイアウォールの設定

次に、ファイアウォール設定にいくつかの変更を加えて、httpサービスを許可する。

sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --reload



Nginxのテスト

Nginxが正しく実行されていることを確認する。
これを行うには、htmlファイルを作成し、それをNginxのルートディレクトリに配置する必要がある。
テキストエディタで以下のファイルを作成する。

 sudo nano /srv/www/htdocs/index.html
 
 <html>
 <body>
 <h1>Welcome to osradar</h1>
 </body>
 </html>
 


最後に、Webブラウザを開き、http://localhost/ にアクセスして、これが表示されるか確認する。


NginX上でPHP-FPM(PHP 7 / 8)の有効化

NginXにはPHPモジュールが無いため、CGIによりプログラムを動作させている。
PHPをNginX上で動作させるためには、PHP-FPM(PHP-FastCGI Process Manager)に対して、設定を行う必要がある。

必要な場合、PHP-FPMのソケットファイルを配置するディレクトリを作成する。

mkdir /<PHPのインストールディレクトリ>/var/run/php-fpm


PHP-FPMの構成ディレクトリ(/etc/php7/fpm)にあるphp-fpm.confファイルのerror_logキーを以下のように編集する。

# PHP 7
sudo cp /etc/php7/fpm/php-fpm.conf.default /etc/php7/fpm/php-fpm.conf

# PHP 8
sudo cp /etc/php8/fpm/php-fpm.conf.default /etc/php8/fpm/php-fpm.conf


# PHP 7
sudo vi /etc/php7/fpm/php-fpm.conf

# PHP 8
sudo vi /etc/php8/fpm/php-fpm.conf


# /etc/php7/fpm/php-fpm.confファイル または /etc/php8/fpm/php-fpm.confファイル

error_log = /var/log/php-fpm.log


次に、www.conf構成ファイルで構成済みプールの正しい設定を定義する。

# PHP 7
sudo cp /etc/php7/fpm/php-fpm.d/www.conf.default /etc/php7/fpm/php-fpm.d/www.conf

# PHP 8
sudo cp /etc/php8/fpm/php-fpm.d/www.conf.default /etc/php8/fpm/php-fpm.d/www.conf


# PHP 7
sudo vi /etc/php7/fpm/php-fpm.d/www.conf

# PHP 8
sudo vi /etc/php8/fpm/php-fpm.d/www.conf


# /etc/php7/fpm/php-fpm.d/www.confファイル または /etc/php8/fpm/php-fpm.d/www.confファイル

user = <Nginxの実行ユーザ名 (例. nginx)>
group = <Nginxの実行グループ名 (例. nginx)>
listen = <nginx.confファイルで設定したfastcgi_passの値 (例. /var/run/php-fpm/php-fpm.sock)>
listen.owner = <PHP-FPMの実行ユーザ名 (例. nginx)>
listen.group = <PHP-FPMの実行グループ名 (例. nginx)>


次に、/etc/php7/cli/php.iniファイル、または、/etc/php8/cli/php.iniファイルを作成・編集する。

# PHP 7
sudo vi /etc/php7/cli/php.ini

# PHP 8
sudo vi /etc/php8/cli/php.ini


# /etc/php7/cli/php.iniファイル または /etc/php8/cli/php.iniファイル
cgi.fix_pathinfo=0


----- ここは不要かも? -----
次に、/etc/php7/fpm/php.iniファイルを作成・編集する。
php.iniファイルをコピーして、php.iniファイルのcgi.fixキーを編集する。

sudo cp /etc/php7/cli/php.ini /etc/php7/fpm/


sudo vi /etc/php7/fpm/php.ini

# /etc/php7/fpm/php.iniファイル
cgi.fix_pathinfo=0


そして、/etc/php7/fpm/php.iniファイルをPHP構成ディレクトリにコピーする。

sudo cp /etc/php7/fpm/php.ini /etc/php7/conf.d/

----- ここまで -----


NginXの設定

NginXを構成するため、NginXの設定ファイルであるnginx.confファイルを、以下に示すような内容で編集する。

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org


sudo vi /etc/nginx/nginx.conf


# /etc/nginx/nginx.confファイル

http {
   # ...略

   server {
       listen       80;
       server_name  localhost;

       #charset koi8-r;

       access_log  <アクセスログファイルのフルパス>;

       location / {
           root   html;
           index  index.php index.html index.htm;
       }

       error_page  404  /404.html;
       location = /404.html {
           root   html;
       }

       # redirect server error pages to the static page /50x.html
       #
       error_page  500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }

       # proxy the PHP scripts to Apache listening on 127.0.0.1:80
       #
       #location ~ \.php$ {
       #    proxy_pass   http://127.0.0.1;
       #}

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       #
       #location ~ \.php$ {
       #    root           html;
       #    fastcgi_pass   127.0.0.1:9000;
       #    fastcgi_index  index.php;
       #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
       #    include        fastcgi_params;
       #}

       # このセクションは、NginXからPHP-FPMの通信は
       # Unixドメインソケット通信でlistenしているFastCGIサーバにPHPスクリプトを渡すために使用される
       location ~ \.php$
       {
          root           html;
          fastcgi_pass   <PHP-FPMのソケットファイルのフルパス>;
                         # 例1. unix:/<PHPのインストールディレクトリ>/var/run/php-fpm.sock;
                         # 例2. unix:/var/run/php-fpm/php-fpm.sock;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
       }

       # deny access to .htaccess files, if Apache's document root
       # concurs with nginx's one
       #
       #location ~ /\.ht {
       #    deny  all;
       #}
   }

   # ...略

}


NginXおよびPHP-FPMを起動する。

sudo systemctl restart nginx php-fpm


Webブラウザを起動した後、http://localhost/index.php にアクセスして、正常に表示されるか確認する。


仮想ホストの構築

NginXの設定ファイルを編集する。

# パッケージ管理システムからインストールしている場合
sudo vi /etc/nginx/nginx.conf

# ソースコードからインストールしている場合
sudo vi /<Nginxのインストールディレクトリ>/etc/nginx.conf


 # nginx.confファイル
 
 http {
    # ...略
 
    server {
       listen       <ポート番号 例. 80>;
       listen       <仮想ホスト名 例. virtualhost01>:<ポート番号 例. 80>;
       server_name  <ドメイン名 例. virtualhost01>  alias  <ドメイン名 例. virtualhost01>.alias;
 
       location / {
          root   <仮想ホストのドキュメントルート>;
          index  index.php index.html index.htm;
       }
 
       # このセクションは、NginxからPHP-FPMの通信はUnixドメインソケット通信でlistenしているFastCGIサーバにPHPスクリプトを渡すために使用される
       location ~ \.php$
       {
          root           <仮想ホストのドキュメントルート>;
          fastcgi_pass   unix:<PHP-FPMのソケットのパス>;
                         # 例1. unix:/var/run/php-fpm/php-fpm.sock;
                         # 例2. unix:/home/user/PHP/var/run/php-fpm/php-fpm.sock;
          fastcgi_index  index.php;
          include        fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          # proxy_pass   http://127.0.0.1;
       }
    }
 
    # ...略
 }


最後に、NginxとPHP-FPMサービスを開始する。

sudo systemctl start php-fpm
sudo systemctl start nginx



エラー関連

test failed

Webサイトにアクセスする時、"test failed"というエラーメッセージが表示される場合がある。
これは、NginXの初期設定ではIPv4とIPv6をリッスンするが、使用しているWebサーバがIPv6をサポートしていない場合に起きる。

nginx.confファイルにおいて、IPv6を使用しないように設定する。

vi nginx.conf


# nginx.confファイル

# 編集前
listen [::]:80 default_server;

# 編集後
# listen [::]:80 default_server;


NginXを再読み込みする。

sudo systemctl	reload nginx



Xdebugの設定

PHP-FPMの初期設定では、9000番ポートを使用しているので、Xdebugのデバッグポートと競合が発生する。
Xdebugのデバッグポートを以下のように変更する。

sudo vi /etc/php7/conf.d/xdebug.ini

# /etc/php7/conf.d/xdebug.iniファイル
xdebug.remote_port=9009


PHP-FPMとNginXを再起動する。

sudo systemctl restart php-fpm
sudo systemctl restart nginx