1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

EC2でNGINX Unitを使う

Last updated at Posted at 2022-03-23

NGINX Unitについて

  1. PythonをNGINXで使用したいときに中継サーバーとして使用
  2. Python,PHP,Go,Perl,Ruby,Node.js,Javaなどに対応
  3. 同じ言語の複数バージョンを同時に運用可能
  4. サーバー停止せずに設定変更可能
  5. 公式ドキュメント
    https://unit.nginx.org/installation/

インストール

$ sudo vi /etc/yum.repos.d/unit.repo
[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/amzn2/$releasever/$basearch/
gpgcheck=0
enabled=1
$ sudo yum install unit
$ sudo yum install unit-devel unit-go unit-jsc8 unit-perl  \
      unit-php unit-python27 unit-python37

自動再起動設定

sudo systemctl enable unit

再起動

sudo systemctl restart unit

ログファイル

/var/log/unit/unit.log

設定確認

# sudo curl --unix-socket /var/run/unit/control.sock http://localhost/
{
	"certificates": {},
	"config": {
		"listeners": {},
		"applications": {}
	}
}

設定変更

プログラマブルかつノンストップであることが特徴で、PUT通信してコンフィグを反映する

・/vaw/www/python/wsgi.pyを作成

def application(environ, start_response):
    start_response("200 OK", [("Content-Type", "text/plain")])
    return (b"Hello, Python on Unit!")

・/var/www/python/config.jsonを作成

{
  "listeners": {
    "*:8080": {
      "pass": "applications/python_app"
    }
  },
  "applications": {
    "python_app": {
      "type": "python",
      "path": "/var/www/python/",
      "module": "wsgi"
    }
  }
}

・設定を反映

# cd /var/www/python/
# curl -X PUT --data-binary @config.json --unix-socket /var/run/unit/control.sock http://localhost/config/
{
	"success": "Reconfiguration done."
}

・実際に通信して確認

# curl http://localhost:8080
Hello, Python on Unit!
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?