0
0

More than 1 year has passed since last update.

UbuntuにFlaskとuwsgiとNginxを一気貫通する

Last updated at Posted at 2023-06-19

本記事の背景

以前はRaspberryPiにてFlask+uwsgi+Nginxを設定したことはあるが、
今回はubuntu上に同じように設定する
但し、参考となる記事が色々ばらけているので、本記事で実際の設定できていることをまとめる

本記事の前提

・すでにFlaskのアプリは構築済
・ubuntuインストール済
・uwsgiインストール済
・Nginxインストール済
・Pythonの環境構築は済

1.uwsgi.iniの設定

[uwsgi]
callable = app 
master = true
socket = /tmp/uwsgi.sock
chmod-socket = 664
#wsgi-file = /home/akeyi2016/FlaskStudy/tutorial/hello.py
wsgi-file = /mnt/d/hello/hello.py

※ httpまたはsocketにアドレスやポート番号を指定する必要なし
※ nginxはsocketパラメータを受け取るため

2.uwsgiサービスの設定

[Unit]
Description=uWSGI instance to serve flask
After=network.target

[Service]
User=akeyi2016
WorkingDirectory=/home/akeyi2016/FlaskStudy/tutorial
ExecStart=/home/akeyi2016/.local/bin/uwsgi uwsgi.ini

[Install]
WantedBy=multi-user.target

3.nginx.confの設定

user ※<www-data>だと動かない場合がある
http {
        ...
        include /etc/nginx/conf.d/*.conf;
        #include /etc/nginx/sites-enabled/*;
}

4.uwsgi.confの設定

server {
    listen 80 default_server;
    server_name _;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
}

5.サービスを有効にする

sudo systemctl start nginx.service
sudo systemctl start uwsgi.service

6.常時起動は以下を設定

sudo systemctl enable nginx.service
sudo systemctl enable uwsgi.service

7.備考

# uwsgi.serviceパス
/etc/systemd/system
# nginxファイル関連パス
/etc/nginx/nginx.conf
/etc/nginx/conf.d/uwsgi.conf
# nginxエラーログを確認する場合
tail /var/log/nginx/error.log -f

8.追加事項

nginx.confで以下user名をnginx実行ユーザ名へ変更する

user www-data ->default
user <ログインuser名>

参考文献

https://uwsgi-docs.readthedocs.io/en/latest/Nginx.html
https://amateur-engineer-blog.com/flask-uwsgi-nginx/
https://qiita.com/Jazuma/items/521cf31538cb618d285a
https://qiita.com/TrashBoxx/items/c4cbb2b2209c3bcb1689
https://qiita.com/akeyi2018/items/aeec3eb47416e51c3119

0
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
0
0