Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

nginx unit を試してみる!!

こんにちは、鯨井貴博@opensourcetechです。

 

 今回はnginxからリリースされているunit(現時点では、beta-0.3)を使ってみます。

www.nginx.com

 

unit とは?

unitは、アプリケーションサーバで現時点では以下の画像にあるように、

Python / PHP / Goに対応しております。

※今後リリース予定のバージョンで、JavaScript / Node.js / Java / Rubyに対応予定。 

https://unit.nginx.org/?_ga=2.184543142.1686155333.1514812376-601047674.1446458460

f:id:opensourcetech:20180107215336p:plain

 

unit で出来ること

unitでは、以下のような環境を構築することが出来ます。

Webクライアント---Webサーバ(nginx)---Applicationサーバ(unit)---Program(Python / PHP / Go)

 

unitがない場合はPython用には

Webクライアント---Webサーバ(nginx)---Applicationサーバ(php-fpm)---Program(Python)

となり、Python以外には別のApplicationサーバなどを用意する必要ありました。

 

unitにより、よりシンプルな構成でマルチ言語に対応出来ます。

 

 

unit の構築

①nginx のインストール

Ubuntu LTS16.04を使用して、nginxをインストールしました。

root@ubuntu-virtual-machine:~# uname -a
Linux ubuntu-virtual-machine 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

手順については、以下の通りなので割愛。

http://nginx.org/en/linux_packages.html

 

②unit のインストール

以下を参考にインストールします。

https://unit.nginx.org/installation/

Nginx のレポジトリ及びパッケージに署名する為の鍵をダウンロードします。

root@ubuntu-virtual-machine:~# wget https://nginx.org/keys/nginx_signing.key?_ga=2.150891702.1686155333.1514812376-601047674.1446458460
--2018-01-06 23:58:20-- https://nginx.org/keys/nginx_signing.key?_ga=2.150891702.1686155333.1514812376-601047674.1446458460
nginx.org (nginx.org) をDNSに問いあわせています... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ...
nginx.org (nginx.org)|206.251.255.63|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 1561 (1.5K) [text/plain]
`nginx_signing.key?_ga=2.150891702.1686155333.1514812376-601047674.1446458460' に保存中

nginx_signing.key?_ 100%[===================>] 1.52K --.-KB/s in 0s

2018-01-06 23:58:21 (113 MB/s) - `nginx_signing.key?_ga=2.150891702.1686155333.1514812376-601047674.1446458460' へ保存完了 [1561/1561]

root@ubuntu-virtual-machine:~# ls
nginx_signing.key?_ga=2.150891702.1686155333.1514812376-601047674.1446458460

 ③ダウンロードした鍵をAPTに追加します。

root@ubuntu-virtual-machine:~# apt-key add nginx_signing.key\?_ga\=2.150891702.1686155333.1514812376-601047674.1446458460
OK

 ④APT用のレポジトリファイルを作成します。

root@ubuntu-virtual-machine:~# vi /etc/apt/sources.list.d/unit.list
root@ubuntu-virtual-machine:~# cat /etc/apt/sources.list.d/unit.list
deb https://packages.nginx.org/unit/ubuntu/ xenial unit
deb-src https://packages.nginx.org/unit/ubuntu/ xenial unit

⑤unitをインストールします。

root@ubuntu-virtual-machine:~# apt-get update
ヒット:1 http://jp.archive.ubuntu.com/ubuntu xenial InRelease
ヒット:2 http://archive.ubuntulinux.jp/ubuntu xenial InRelease
ヒット:3 http://jp.archive.ubuntu.com/ubuntu xenial-updates InRelease
ヒット:4 http://jp.archive.ubuntu.com/ubuntu xenial-backports InRelease
無視:5 http://archive.ubuntulinux.jp/ubuntu-ja-non-free xenial InRelease
ヒット:6 http://archive.ubuntulinux.jp/ubuntu-ja-non-free xenial Release
取得:8 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
取得:9 https://packages.nginx.org/unit/ubuntu xenial InRelease [2,788 B]
取得:10 https://packages.nginx.org/unit/ubuntu xenial/unit Sources [20 B]
取得:11 https://packages.nginx.org/unit/ubuntu xenial/unit amd64 Packages [3,341 B]
取得:12 https://packages.nginx.org/unit/ubuntu xenial/unit i386 Packages [1,973 B]
110 kB を 2秒 で取得しました (50.3 kB/s)

・・・・長いので、省略・・・・

root@ubuntu-virtual-machine:~# apt-get install unit
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下のパッケージが新たにインストールされます:
unit
アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 581 個。
174 kB のアーカイブを取得する必要があります。
この操作後に追加で 672 kB のディスク容量が消費されます。
取得:1 https://packages.nginx.org/unit/ubuntu xenial/unit amd64 unit amd64 0.3-1~xenial [174 kB]
174 kB を 5秒 で取得しました (31.6 kB/s)
以前に未選択のパッケージ unit を選択しています。
(データベースを読み込んでいます ... 現在 180030 個のファイルとディレクトリがインストールされています。)
.../unit_0.3-1~xenial_amd64.deb を展開する準備をしています ...
----------------------------------------------------------------------

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/

----------------------------------------------------------------------
unit (0.3-1~xenial) を展開しています...
ureadahead (0.100.0-19) のトリガを処理しています ...
systemd (229-4ubuntu4) のトリガを処理しています ...
unit (0.3-1~xenial) を設定しています ...
ureadahead (0.100.0-19) のトリガを処理しています ...
systemd (229-4ubuntu4) のトリガを処理しています ...

⑥言語(Python / PHP / Go)用のモジュールパッケージをインストールします。

赤字にしたところが、各モジュールをテストする方法。

root@ubuntu-virtual-machine:~# apt-get install unit-php unit-python2.7 unit-python3.5 unit-go
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
以下の追加パッケージがインストールされます:
golang golang-1.6 golang-1.6-doc golang-1.6-go golang-1.6-race-detector-runtime
golang-1.6-src golang-doc golang-go golang-race-detector-runtime golang-src
libphp-embed libphp7.0-embed php-common php7.0-common
提案パッケージ:
bzr git mercurial subversion php-pear
以下のパッケージが新たにインストールされます:
golang golang-1.6 golang-1.6-doc golang-1.6-go golang-1.6-race-detector-runtime
golang-1.6-src golang-doc golang-go golang-race-detector-runtime golang-src
libphp-embed libphp7.0-embed php-common php7.0-common unit-go unit-php unit-python2.7
unit-python3.5
アップグレード: 0 個、新規インストール: 18 個、削除: 0 個、保留: 581 個。
31.9 MB のアーカイブを取得する必要があります。
この操作後に追加で 211 MB のディスク容量が消費されます。
続行しますか? [Y/n] y
取得:1 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 golang-1.6-src amd64 1.6.2-0ubuntu5~16.04.4 [6,416 kB]
取得:2 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 golang-1.6-go amd64 1.6.2-0ubuntu5~16.04.4 [20.5 MB]
取得:3 https://packages.nginx.org/unit/ubuntu xenial/unit amd64 unit-go all 0.3-1~xenial [67.2 kB]
取得:4 https://packages.nginx.org/unit/ubuntu xenial/unit amd64 unit-php amd64 0.3-1~xenial [10.3 kB]
取得:5 https://packages.nginx.org/unit/ubuntu xenial/unit amd64 unit-python2.7 amd64 0.3-1~xenial [11.5 kB]
取得:6 https://packages.nginx.org/unit/ubuntu xenial/unit amd64 unit-python3.5 amd64 0.3-1~xenial [11.7 kB]
取得:7 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 golang-1.6-doc all 1.6.2-0ubuntu5~16.04.4 [2,368 kB]
取得:8 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 golang-1.6 all 1.6.2-0ubuntu5~16.04.4 [16.8 kB]
取得:9 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 golang-src amd64 2:1.6-1ubuntu4 [3,066 B]
取得:10 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 golang-go amd64 2:1.6-1ubuntu4 [21.8 kB]
取得:11 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 golang-doc all 2:1.6-1ubuntu4 [2,808 B]
取得:12 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 golang all 2:1.6-1ubuntu4 [2,766 B]
取得:13 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 php-common all 1:35ubuntu6 [10.8 kB]
取得:14 http://jp.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-common amd64 7.0.22-0ubuntu0.16.04.1 [843 kB]
取得:15 http://jp.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 libphp7.0-embed amd64 7.0.22-0ubuntu0.16.04.1 [1,227 kB]
取得:16 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 libphp-embed all 1:7.0+35ubuntu6 [2,920 B]
取得:17 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 golang-1.6-race-detector-runtime amd64 0.0+svn252922-0ubuntu1 [404 kB]
取得:18 http://jp.archive.ubuntu.com/ubuntu xenial/main amd64 golang-race-detector-runtime amd64 2:1.6-1ubuntu4 [2,854 B]
31.9 MB を 3秒 で取得しました (8,042 kB/s)
以前に未選択のパッケージ golang-1.6-src を選択しています。
(データベースを読み込んでいます ... 現在 180045 個のファイルとディレクトリがインストールされています。)
.../golang-1.6-src_1.6.2-0ubuntu5~16.04.4_amd64.deb を展開する準備をしています ...
golang-1.6-src (1.6.2-0ubuntu5~16.04.4) を展開しています...
以前に未選択のパッケージ golang-1.6-go を選択しています。
.../golang-1.6-go_1.6.2-0ubuntu5~16.04.4_amd64.deb を展開する準備をしています ...
golang-1.6-go (1.6.2-0ubuntu5~16.04.4) を展開しています...
以前に未選択のパッケージ golang-1.6-doc を選択しています。
.../golang-1.6-doc_1.6.2-0ubuntu5~16.04.4_all.deb を展開する準備をしています ...
golang-1.6-doc (1.6.2-0ubuntu5~16.04.4) を展開しています...
以前に未選択のパッケージ golang-1.6 を選択しています。
.../golang-1.6_1.6.2-0ubuntu5~16.04.4_all.deb を展開する準備をしています ...
golang-1.6 (1.6.2-0ubuntu5~16.04.4) を展開しています...
以前に未選択のパッケージ golang-src を選択しています。
.../golang-src_2%3a1.6-1ubuntu4_amd64.deb を展開する準備をしています ...
golang-src (2:1.6-1ubuntu4) を展開しています...
以前に未選択のパッケージ golang-go を選択しています。
.../golang-go_2%3a1.6-1ubuntu4_amd64.deb を展開する準備をしています ...
golang-go (2:1.6-1ubuntu4) を展開しています...
以前に未選択のパッケージ golang-doc を選択しています。
.../golang-doc_2%3a1.6-1ubuntu4_all.deb を展開する準備をしています ...
golang-doc (2:1.6-1ubuntu4) を展開しています...
以前に未選択のパッケージ golang を選択しています。
.../golang_2%3a1.6-1ubuntu4_all.deb を展開する準備をしています ...
golang (2:1.6-1ubuntu4) を展開しています...
以前に未選択のパッケージ php-common を選択しています。
.../php-common_1%3a35ubuntu6_all.deb を展開する準備をしています ...
php-common (1:35ubuntu6) を展開しています...
以前に未選択のパッケージ php7.0-common を選択しています。
.../php7.0-common_7.0.22-0ubuntu0.16.04.1_amd64.deb を展開する準備をしています ...
php7.0-common (7.0.22-0ubuntu0.16.04.1) を展開しています...
以前に未選択のパッケージ libphp7.0-embed を選択しています。
.../libphp7.0-embed_7.0.22-0ubuntu0.16.04.1_amd64.deb を展開する準備をしています ...
libphp7.0-embed (7.0.22-0ubuntu0.16.04.1) を展開しています...
以前に未選択のパッケージ libphp-embed を選択しています。
.../libphp-embed_1%3a7.0+35ubuntu6_all.deb を展開する準備をしています ...
libphp-embed (1:7.0+35ubuntu6) を展開しています...
以前に未選択のパッケージ golang-1.6-race-detector-runtime を選択しています。
.../golang-1.6-race-detector-runtime_0.0+svn252922-0ubuntu1_amd64.deb を展開する準備をしています ...
golang-1.6-race-detector-runtime (0.0+svn252922-0ubuntu1) を展開しています...
以前に未選択のパッケージ golang-race-detector-runtime を選択しています。
.../golang-race-detector-runtime_2%3a1.6-1ubuntu4_amd64.deb を展開する準備をしています ...
golang-race-detector-runtime (2:1.6-1ubuntu4) を展開しています...
以前に未選択のパッケージ unit-go を選択しています。
.../unit-go_0.3-1~xenial_all.deb を展開する準備をしています ...
----------------------------------------------------------------------

The Go module for NGINX Unit has been installed.

To check out the sample app, run these commands:

GOPATH=/usr/share/gocode go build -o /tmp/go-app /usr/share/doc/unit-go/examples/go-app/let-my-people.go
sudo service unit restart
sudo service unit loadconfig /usr/share/doc/unit-go/examples/unit.config
curl http://localhost:8500/

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

----------------------------------------------------------------------
unit-go (0.3-1~xenial) を展開しています...
以前に未選択のパッケージ unit-php を選択しています。
.../unit-php_0.3-1~xenial_amd64.deb を展開する準備をしています ...
----------------------------------------------------------------------

The PHP 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-php/examples/unit.config
curl http://localhost:8300/

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

----------------------------------------------------------------------
unit-php (0.3-1~xenial) を展開しています...
以前に未選択のパッケージ unit-python2.7 を選択しています。
.../unit-python2.7_0.3-1~xenial_amd64.deb を展開する準備をしています ...
----------------------------------------------------------------------

The Python 2.7 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-python2.7/examples/unit.config
curl http://localhost:8400/

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

----------------------------------------------------------------------
unit-python2.7 (0.3-1~xenial) を展開しています...
以前に未選択のパッケージ unit-python3.5 を選択しています。
.../unit-python3.5_0.3-1~xenial_amd64.deb を展開する準備をしています ...
----------------------------------------------------------------------

The Python 3.5 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.5/examples/unit.config
curl http://localhost:8400/

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

----------------------------------------------------------------------
unit-python3.5 (0.3-1~xenial) を展開しています...
man-db (2.7.5-1) のトリガを処理しています ...
golang-1.6-src (1.6.2-0ubuntu5~16.04.4) を設定しています ...
golang-1.6-go (1.6.2-0ubuntu5~16.04.4) を設定しています ...
golang-1.6-doc (1.6.2-0ubuntu5~16.04.4) を設定しています ...
golang-1.6 (1.6.2-0ubuntu5~16.04.4) を設定しています ...
golang-src (2:1.6-1ubuntu4) を設定しています ...
golang-go (2:1.6-1ubuntu4) を設定しています ...
golang-doc (2:1.6-1ubuntu4) を設定しています ...
golang (2:1.6-1ubuntu4) を設定しています ...
php-common (1:35ubuntu6) を設定しています ...
php7.0-common (7.0.22-0ubuntu0.16.04.1) を設定しています ...

Creating config file /etc/php/7.0/mods-available/calendar.ini with new version

Creating config file /etc/php/7.0/mods-available/ctype.ini with new version

Creating config file /etc/php/7.0/mods-available/exif.ini with new version

Creating config file /etc/php/7.0/mods-available/fileinfo.ini with new version

Creating config file /etc/php/7.0/mods-available/ftp.ini with new version

Creating config file /etc/php/7.0/mods-available/gettext.ini with new version

Creating config file /etc/php/7.0/mods-available/iconv.ini with new version

Creating config file /etc/php/7.0/mods-available/pdo.ini with new version

Creating config file /etc/php/7.0/mods-available/phar.ini with new version

Creating config file /etc/php/7.0/mods-available/posix.ini with new version

Creating config file /etc/php/7.0/mods-available/shmop.ini with new version

Creating config file /etc/php/7.0/mods-available/sockets.ini with new version

Creating config file /etc/php/7.0/mods-available/sysvmsg.ini with new version

Creating config file /etc/php/7.0/mods-available/sysvsem.ini with new version

Creating config file /etc/php/7.0/mods-available/sysvshm.ini with new version

Creating config file /etc/php/7.0/mods-available/tokenizer.ini with new version
libphp7.0-embed (7.0.22-0ubuntu0.16.04.1) を設定しています ...
update-alternatives: /usr/lib/libphp7.so (libphp7) を提供するために自動モードで /usr/lib/libphp7.0.so を使います

Creating config file /etc/php/7.0/embed/php.ini with new version
libphp-embed (1:7.0+35ubuntu6) を設定しています ...
golang-1.6-race-detector-runtime (0.0+svn252922-0ubuntu1) を設定しています ...
golang-race-detector-runtime (2:1.6-1ubuntu4) を設定しています ...
unit-go (0.3-1~xenial) を設定しています ...
unit-php (0.3-1~xenial) を設定しています ...
unit-python2.7 (0.3-1~xenial) を設定しています ...
unit-python3.5 (0.3-1~xenial) を設定しています ...
libc-bin (2.23-0ubuntu3) のトリガを処理しています ...

 
unit の動作確認

今回は、一つ前の手順⑥にあるPython2.7で確認を行いました。

 ①念の為、Pythonのバージョン確認。

root@ubuntu-virtual-machine:~# python -V
Python 2.7.11+

 ②unit の起動。 

root@ubuntu-virtual-machine:~# service unit restart

 ③Python2.7用のサンプルコンフィグを読み込み。 

root@ubuntu-virtual-machine:~# cat /usr/share/doc/unit-python2.7/examples/unit.config
{
"applications": {
"example_python": {
"type": "python",
"user": "nobody",
"workers": 2,
"path": "/usr/share/doc/unit-python2.7/examples/python-app",
"module": "wsgi"
}
},

"listeners": {
"*:8400": {
"application": "example_python"
}
}
}
root@ubuntu-virtual-machine:~# service unit loadconfig /usr/share/doc/unit-python2.7/examples/unit.config
Loading configuration from /usr/share/doc/unit-python2.7/examples/unit.config...
{
"success": "Reconfiguration done."
}

 ④curl にて、アクセスして確認。

 ※その他、セキュリティ設定(SELinuxとか、Firewalldとか)が必要であれば実施してくださいね。

以下のように、レスポンスがあれば大丈夫でしょう。

root@ubuntu-virtual-machine:~# curl http://localhost:8400/
2018-01-07 12:25:12 AM

Python: 3.5.1+ (default, Mar 30 2016, 22:46:26)
[GCC 5.3.1 20160330]

ENV Variables:

PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG ja_JP.UTF-8
DAEMON_ARGS --log /var/log/unit.log --pid /run/unit.pid

 

nginx との統合

統合と言ってますが、nginxでリバースプロキシの設定をしましょうってことです。

詳細は割愛しますが、以下のどちらかで出来ると思います。

https://unit.nginx.org/integration/

nginxによるリバースプロキシ(reverse proxy)構築 on CentOS6.5 - Opensourcetechブログ(ZeusITCamp裏BLOG)

あとは、Webクライアント(ブラウザ等)からアクセスすればOK!

 

今後出来ると嬉しいかもしれないこと

 Apache + unit(mod_unitとかになるのか!?)のように、

他のWebサーバソフトとの組み合わせ出来ること。

そうなれば、unitの活躍の場が広がりそうですね。

 

 

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

Opensourcetech by Takahiro Kujirai