NGINX UnitをUbuntuに導入。PHPを動かす

NGINX UnitをUbuntuに導入。PHPを動かす

2018-09-125 min read

目次

  1. 概要
  2. 環境
  3. nginx-unitについて
  4. 結論
  5. セットアップ
  6. unitの開始
  7. アプリケーションの準備と設定
  8. 参考

概要

Ubuntu 18.04にNGINX Unitを導入し、PHPを動かしてみる。

環境

  • Ubuntu 18.04
  • PHP7
  • NGINX Unit 1.3

NGINX Unitについて

about

NGINX Unit is a dynamic web and application server, designed to run applications in multiple languages. Unit is lightweight, polyglot, and dynamically configured via API. The design of the server allows reconfiguration of specific application parameters as needed by the engineering or operations. Current latest version is 1.3, released on July 13, 2018.

See the changelog here: unit.nginx.org/CHANGES.txt.

The sources are distributed under the Apache 2.0 license. 引用 - https://unit.nginx.org

と言ってます。つまり NGINX Unitは複数の言語でアプリケーションを実行できるように設計されたWebおよびアプリケーションサーバであり、

軽量で、APIを介して動的に設定を変更できるのが大きな特徴ということでしょうか https://unit.nginx.org/

言語

Python、PHP、Go、Perl、Ruby、が利用可能であり、将来的にはJavaScript/Node.js、Javaが利用できるそう。

結論

先に結論から言うと、楽しかったです。ただ、導入はもう少し後かなと思いました。

情報量がまだまだ少ないかなぁと感じます。 また、Pythonも試しましたが、特別な理由がない限りgunicornやuWSGIを利用した方が良いと言う話もあります。 https://tokibito.hatenablog.com/entry/2018/04/25/045009 ただ、あらゆる言語を同じ設定、同じコマンドを利用して、サーバ立ち上げから、デーモン、設定の反映と再構成を行えることはとても魅了的だと思いました。 近いうちにNodeJS対応とかがあるらしいので、それを待って見るかな...思いました。

セットアップ

公式に基づいて導入していきます。 https://unit.nginx.org/installation/

NGINXサインインキーの登録

これを落としてきます。

https://nginx.org/keys/nginx_signing.key? ※リンク切れしていたらこの中にあると思うので探してください。 以下のコマンドで登録します。

apt-key add nginx_signing.key

リポジトリ取得の設定

Ubuntuのバージョンに合わせて

/etc/apt/sources.list.d/unit.list

に、

以下の内容を記述します。

16.04の場合

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

17.10の場合

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

18.04の場合

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

記述が追記できたら更新した後、Unitをインストールします。

apt update
apt install unit

続いてモジュール類をインストールします。

php以外のpythonやperlなどを利用する場合はこのようパッケージ名で取得できます。

apt install unit-php \ 
            unit-python2.7 \
            unit-python3.6 \
            unit-go1.9 \
            unit-go1.10 \
            unit-perl \
            unit-ruby

カーネルが古い状態でインストールすると不具合が発生したので注意が必要かもしれません。

https://tech-blog.s-yoshiki.com/2018/09/517/

Unitの開始

とりあえずスタートはこれ。

service unit start

その他にもこのようなコマンドが用意されてます。statusはよく使うと思います。

force-reload
loadconfig
reload
restart
saveconfig
status
stop

また、socketファイルは

/var/run/control.unit.sock

logは

/var/log/unit.log

にあります。 サーバーに構成が正しく反映されなかった場合はlogに何かしら吐かれると思います。

アプリケーションの準備と設定

とりあえずPHPのHelloWorldを準備します。

mkdir -p /var/www/app/
cd /var/www/app/
touch index.php

index.phpを編集します。

<?php
echo("Hello World");

そしたら適当なディレクトリにserver.jsonを作成します。

server.jsonは下記のように編集します。

{
  "listeners": {
    "*:8300": {
      "application": "php-app"
    }
  },
  "applications": {
    "php-app": {
      "type": "php",
      "processes": {
        "max": 10,
        "spare": 5
      },
      "root": "/var/www/app",
      "index": "index.php"
    }
  }
}

最後に設定ファイルをcurlでPUTすることでサーバに反映されます。

curl -X PUT -d @./server.json --unix-socket /var/run/control.unit.sock http://localhost

curlで--unix-socketが利用できない場合はこちらを参照してください。 https://tech-blog.s-yoshiki.com/2018/09/514/ 設定に関する部分は公式で丁寧に説明されてます。 https://unit.nginx.org/configuration/

良さそうなデモがあったので置いときます。

参考

https://dev.classmethod.jp/server-side/python/nginxunit-gunicorn-uwsgi/

Tags
javascript(109)
linux(54)
node.js(53)
amazon%20aws(47)
typescript(44)
%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0(36)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86(30)
html5(29)
php(24)
centos(24)
python(22)
%E7%AB%B6%E6%8A%80%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0(21)
mac(21)
mysql(20)
canvas(19)
opencv(17)
%E9%9B%91%E8%AB%87(16)
docker(16)
wordpress(15)
atcoder(14)
apache(12)
%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92(12)
%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9(12)
amazon%20s3(12)
red%20hat(12)
prisma(12)
ubuntu(11)
github(10)
git(10)
vue.js(10)
%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86100%E6%9C%AC%E3%83%8E%E3%83%83%E3%82%AF(10)
mariadb(10)
react(9)
aws%20cdk(9)
css3(8)
%E5%8F%AF%E8%A6%96%E5%8C%96(8)
%E5%B0%8F%E3%83%8D%E3%82%BF(8)
nestjs(8)
amazon%20lightsail(7)
next.js(7)
%E3%83%96%E3%83%AD%E3%82%B0(6)
cms(6)
oracle(6)
perl(6)
gitlab(6)
iam(5)
amazon%20ec2(5)
%E8%B3%87%E6%A0%BC%E8%A9%A6%E9%A8%93(5)
aws%20amplify(5)
curl(4)
Author
githubzennqiita
ただの備忘録です。

※外部送信に関する公表事項