nginxの操作コマンドメモ

nginxを操作するためのコマンドメモです。

nginx起動

nginxを起動させる方法いろいろです。

nginxを直接実行

「service」コマンドや「systemctl」コマンドを使用せずに「nginx」を起動させるには、コマンドラインから直接「nginx」を実行することで起動させることが出来ます。

# /usr/sbin/nginx

「nginx」のパスはインストールされている環境によってことなる場合があるので適宜読み替えてください。

パスが通っているのであれば「nginx」だけでも起動させることはできます。

設定ファイルを指定して起動

デフォルトの設定ファイル以外を使用する場合は、「-c」オプションを使用して設定ファイルを指定します。

下記は「/etc/nginx」ディレクトリにテスト用の設定ファイル(test.conf)が作成してあって、それを設定ファイルとして指定し「nginx」を起動させる場合の例となります。

# /usr/sbin/nginx -c /etc/nginx/test.conf

systemctl start nginx(CentOS7)

「yum」を使用してインストールした場合は、CentOS7ならば「systemctl」コマンドで「nginx」を起動させることが出来ます。

# systemctl start nginx

service start nginx(CentOS6以前)

CentOS6以前の環境に「yum」で「nginx」をインストールした場合は、「service」コマンドで起動させることが出来ます。

# service start nginx

nginx停止

nginxを停止させる方法いろいろです。

nginxを直接停止

「systemctl」コマンドや「service」コマンドを使用せずに「nginx」を停止させる方法を2通り説明します。

リクエスト処理完了後に停止

こちらの方法は、クライアントと通信している処理を完了させてから停止させる形になります。(apacheでいう「apachectl -k graceful」です)

# /usr/sbin/nginx -s quit

クライアントとの通信を完了させてからの停止なので、通信が強制切断されることが無いのでクライアントに優しいですね。

直ちに停止

こちらの方法は、クライアントとの通信を強制切断させて停止させる形になります。(apacheでいう「apachectl -k stop」です)

# /usr/sbin/nginx -s stop

systemctl stop nginx (CentOS7)

CentOS7で「systemctl」コマンドを使用して「nginx」を停止させる方法です。

# systemctl stop nginx

service stop nginx (CentOS6以前)

CentOS6以前の環境で、「service」コマンドを使用して「nginx」を停止させる方法です。

# service stop nginx

nginx再起動

「nginx」を再起動させる方法色々です。

nginxを直接再起動

「systemctl」コマンドや「service」コマンドを使用せずに「nginx」を再起動させる方法です。

# /usr/sbin/nginx -s reload

systemctl restart nginx (CentOS7)

CentOS7で「systemctl」コマンドを使用して「nginx」を再起動させる方法です。

# systemctl restart nginx

service restart nginx (CentOS6以前)

CentOS6以前の環境で、「service」コマンドを使用して「nginx」を再起動させる方法です。

# service restart nginx

nginx設定ファイルの構文確認

設定ファイルの構文(文法)に間違いがないか確認するには「-t」オプションを使います。

# /usr/sbin/nginx -t

下記は実際に設定ファイルのチェックを行った際のログとなります。

# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

確認する設定ファイルを指定

設定ファイルがデフォルト設定の「/etc/nginx/nginx.conf」以外の場合は、「-c」オプションを付けることで設定ファイルを指定できます。

# /usr/sbin/nginx -t -c /etc/nginx/test.conf 
nginx: the configuration file /etc/nginx/test.conf syntax is ok
nginx: configuration file /etc/nginx/test.conf test is successful

nginxバージョン表示

「nginx」のバージョンを表示させるコマンドは2種類あるので、それぞれについて説明していきます。

バージョンのみを表示

こちらは「nginx」のバージョンのみを表示させる方法となります。

# /usr/sbin/nginx -v

実際にバージョンを表示させてみました。

# /usr/sbin/nginx -v
nginx version: nginx/1.10.1

詳しいバージョン情報を表示

こちらの方法は、「nginx」のバージョン以外にもコンパイラーのバージョンおよびconfigerのオプションとして指定されている情報も併せて表示させます。

#/usr/sbin/nginx -V

こちらの場合は「-V」は大文字の「V」なので注意してください。

実際に実行していみると、コンパイラー(gcc)のバージョンやOpenSSLのバージョンとconfigerの設定内容が表示されることが確認できます。

# /usr/sbin/nginx -V
nginx version: nginx/1.10.1
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=/usr/share/nginx --sbin-path=/usr/sbin/nginx 
--modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log 
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body 
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi 
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi 
--pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx 
--with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module 
--with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic 
--with-http_geoip_module=dynamic --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_degradation_module 
--with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic 
--with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic 
--with-stream_ssl_module --with-google_perftools_module 
--with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong 
--param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' 
--with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

※「configure arguments」の部分は本来一行で表示されますが、そのままでは見にくくなってしまうため適当なところで改行してあります。

「configure」の設定を見やすく表示

「configure」で指定されている値だけを抜き出して、見やすいように表示させたい場合は「grep」で「configure」の部分だけを抜き出して、その結果を「tr」コマンドと組み合わせることで見やすく表示させることができます。

ここで注意する点は、「nginx -V」の表示結果は「標準エラー出力」となっているため「2>&1」として、標準エラー出力を標準出力に渡す必要があります。

nginx -V 2>&1 | grep configure | tr ' ' '\n'

「--prefix」以降が「configure」時の設定となっていて、実際に表示させるとかなり見やすくなっていることが分かると思います。

$ nginx -V 2>&1 | grep configure | tr ' ' '\n'
configure
arguments:
--prefix=/etc/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--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-compat
--with-file-aio
--with-threads
--with-http_addition_module
--with-http_auth_request_module
--with-http_dav_module
--with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_mp4_module
--with-http_random_index_module
--with-http_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_module
--with-http_stub_status_module
--with-http_sub_module
--with-http_v2_module
--with-mail
--with-mail_ssl_module
--with-stream
--with-stream_realip_module
--with-stream_ssl_module
--with-stream_ssl_preread_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
-fPIC'
--with-ld-opt='-Wl,-z,relro
-Wl,-z,now
-pie'

コメント

タイトルとURLをコピーしました