Nginx Unitを自動起動させるには

毎回手動で立ち上げるのはめんどくさいからさ。

Nginx Unitをサーバ起動時に自動的に稼働させるためには
$ sudo systemctl enable unit
Synchronizing state of unit.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable unit

ではリブートした後どうなるか?

$ curl http://localhost:8400/
2018-07-03 08:44:39 PM

Python: 3.6.5 (default, Apr  1 2018, 05:46:30)
[GCC 7.3.0]

ENV Variables:

LANG    ja_JP.UTF-8
LANGUAGE        ja_JP:ja
PATH    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
INVOCATION_ID   8bc876ae223a4d618ebed74ed7ed9650
JOURNAL_STREAM  9:20761
DAEMON_ARGS     --log /var/log/unit.log --pid /run/unit.pid

うん、立ち上がっているね!!

Nginx Unitのインストール

Ubuntuだと比較的楽にインストールできるそうだ。

最初にキーファイルをダウンロードする。

$ wget https://nginx.org/keys/nginx_signing.key
--2018-07-02 20:15:26--  https://nginx.org/keys/nginx_signing.key
nginx.org (nginx.org) をDNSに問いあわせています... 2606:7100:1:69::3f, 2001:1af8:4060:a004:21::e3, 206.251.255.63, ...
nginx.org (nginx.org)|2606:7100:1:69::3f|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 1561 (1.5K) [text/plain]
`nginx_signing.key' に保存中

nginx_signing.key             100%[=================================================>]   1.52K  --.-KB/s    時間 0s

2018-07-02 20:15:26 (105 MB/s) - `nginx_signing.key' へ保存完了 [1561/1561]

apt-keyでキーファイルを追加する。

$ sudo apt-key add nginx_signing.key

/etc/apt/sources.list.d/unit.listを作成する。

$ sudo vi /etc/apt/sources.list.d/unit.list

今回はUbuntu18.04を使っているので、次の内容を記載して保存する。

deb https://packages.nginx.org/unit/ubuntu/ bionic unit
deb-src https://packages.nginx.org/unit/ubuntu/ bionic unit

それじゃインストールを始めますか。

$ sudo apt update
$ sudo apt install unit

インストールが終わるとこんなメッセージが出る。

----------------------------------------------------------------------

Thank you for installing NGINX Unit!

Additional modules are available in standalone packages.
To see the available modules, run: apt search --names-only '^unit-'

Online documentation is available at https://unit.nginx.org/

----------------------------------------------------------------------

追加モジュールをインストールしろということなのな。
で、何を利用できるのか、追加モジュール一覧を表示させてみる。

$ apt search --names-only '^unit-'
ソート中... 完了
全文検索... 完了
unit-dbg/stable 1.2-1~bionic amd64
  NGINX Unit (debug symbols)

unit-go1.10/stable 1.2-1~bionic all
  Go 1.10 module for NGINX Unit

unit-go1.9/stable 1.2-1~bionic all
  Go 1.9 module for NGINX Unit

unit-perl/stable 1.2-1~bionic amd64
  Perl module for NGINX Unit

unit-perl-dbg/stable 1.2-1~bionic amd64
  Perl module for NGINX Unit (debug symbols)

unit-php/stable 1.2-1~bionic amd64
  PHP module for NGINX Unit

unit-php-dbg/stable 1.2-1~bionic amd64
  PHP module for NGINX Unit (debug symbols)

unit-python2.7/stable 1.2-1~bionic amd64
  Python 2.7 module for NGINX Unit

unit-python2.7-dbg/stable 1.2-1~bionic amd64
  Python 2.7 module for NGINX Unit (debug symbols)

unit-python3.6/stable 1.2-1~bionic amd64
  Python 3.6 module for NGINX Unit

unit-python3.6-dbg/stable 1.2-1~bionic amd64
  Python 3.6 module for NGINX Unit (debug symbols)

unit-ruby/stable 1.2-1~bionic amd64
  Ruby module for NGINX Unit

unit-ruby-dbg/stable 1.2-1~bionic amd64
  Ruby module for NGINX Unit (debug symbols)

今回はPythonだから、次のコマンドで追加モジュールをインストールする。

$ sudo apt install unit-python3.6

インストールが終わるとこんなメッセージが表示される。

----------------------------------------------------------------------

The Python 3.6 module for NGINX Unit has been installed.

To check out the sample app, run these commands:

 sudo service unit restart
 sudo service unit loadconfig /usr/share/doc/unit-python3.6/examples/unit.config
 curl http://localhost:8400/

Online documentation is available at https://unit.nginx.org

----------------------------------------------------------------------

いったんサーバをリブートしてみる。

$ sudo service unit restart
$ sudo service unit loadconfig /usr/share/doc/unit-python3.6/examples/unit.config
Loading configuration from /usr/share/doc/unit-python3.6/examples/unit.config...
{
        "success": "Reconfiguration done."
}
$ curl http://localhost:8400/
2018-07-02 08:39:02 PM

Python: 3.6.5 (default, Apr  1 2018, 05:46:30)
[GCC 7.3.0]

ENV Variables:

LANG    ja_JP.UTF-8
LANGUAGE        ja_JP:ja
PATH    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
INVOCATION_ID   11aa09ac823a44d2ab01da6ad6208da1
JOURNAL_STREAM  9:26162
DAEMON_ARGS     --log /var/log/unit.log --pid /run/unit.pid

なんかよくわからないが、なんか動いているんだよね。

Nginx稼働に関する管理方法

大事なことだから転記しておく。

Nginxを停止する場合
$ sudo systemctl stop nginx

停止していたNginxを稼働させる場合
$ sudo systemctl start nginx

Nginxを再起動(一旦停止後再起動)させる場合
$ sudo systemctl restart nginx

Nginxの設定を変更した場合は、再起動させずに設定の反映が可能
$ sudo systemctl reload nginx

Nginxをサーバ起動時に自動的に稼働させたくない場合
$ sudo systemctl disable nginx

Nginxをサーバ起動時に自動的に稼働させたい場合
$ sudo systemctl enable nginx