2024年5月2日木曜日

GCEに「NGINX Unit」を入れる ver.2024

前回の記事は、こちら

https://yamayoshi.blogspot.com/2020/04/gcenginx-unitweb.html

セキュリティが強化されたようで、config.json に user を追加しています。

一旦動いてしまえば、Unit 最高。

 [Flask]

$ cd
$ mkdir project1 && cd project1
$ python3 -m venv venv
$ source venv/bin/activate
V pip install Flask
V nano main.py
--
from flask import Flask
application = Flask(__name__)
@application.route("/")
def index():
 return "Hello Project1!"
if __name__ == "__main__":
 application.run()
--
V deactivate

[Unit] Ubunt22.04
$ cd
$ sudo -i
# curl --output /usr/share/keyrings/nginx-keyring.gpg https://unit.nginx.org/keys/nginx-keyring.gpg
# nano /etc/apt/sources.list.d/unit.list
--
deb [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/ubuntu/ jammy unit
deb-src [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/ubuntu/ jammy unit
--
# apt update
# apt install unit unit-dev unit-python3.10
# systemctl restart unit

# nano config.json
--
{
 "listeners": {
  "*:80": {
   "pass": "applications/flask_app"
  }
 },
 "applications": {
  "flask_app": {
   "type": "python",
   "user": "<user>",
   "path": "/home/<user>/project1",
   "home": "/home/<user>/project1/venv",
   "module": "main"
  }
 }
}
--
# curl -X PUT --data-binary @config.json --unix-socket /var/run/control.unit.sock http://localhost/config
# service unit restart

以上です。