Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (implementation)

Continuation from Installation

A rough description and installation of each application See Previous article

Flask

Create a file with the following structure
flask_web_application/
├── application/
│   ├── __init__.py
│   └── views.py
└── server.py

First, create the main body of Flask called app

__init__.py


from flask import Flask

app = Flask(__name__)

import application.views

After accessing the root directory Just return "Hello, World"

views.py


from application import app

@app.route('/')
def show_entries():
    return "Hello World"

Files executed by uWSGI The process starts from here

server.py


from application import app

if __name__ == "__main__":
    app.run(host='0.0.0.0')

By the way, this time I made it in / home / ubuntu / application / </ code> For other directories Pass permissions in chown to the user running uWSGI Set the authority to around "775" with chmod I will explain later, but this time uWSGI

Group name: www-data Username: www-data

Running on

uWSGI

First create uwsgi.ini uwsgi.ini is a configuration file to read when starting uwsgi

It's just read at startup so you can put it anywhere Easy-to-understand Flask application made this time Put it right under

uwsgi.ini



[uwsgi]
base = /home/ubuntu/application/flask_web_application

module = server:app

virtualenv = /home/ubuntu/py36_flask_server

pythonpath = %(base)

callable = app

uid = www-data
gid = www-data
master = true
processes = 1
threads = 1
socket = /tmp/uwsgi.sock
chmod-socket = 666
vacuum = true
die-on-term = true
thunder-lock = true

Only the main part

variable Description
base Application directory
module app = Flask(__name__)The app made in
virtualenv Python virtual environment directory
pythonpath Same as base
callable Module to call(app)
uid Run by username
gid Run by group name
processes The number of cores is recommended depending on the environment and application
threads FlaskThe number of threads used by Flask, it is not recommended to run Flask in multiple threads(GIL problem)
socket UNIX domain socket

Create the user name and group name specified by uid and gid in advance. www-data should already exist If you do not specify it, it will be executed as root, so it is not very good for security

Various articles that will be helpful https://uwsgi-docs.readthedocs.io/en/latest/ThingsToKnow.html https://qiita.com/yasunori/items/64606e63b36b396cf695 https://qiita.com/wapa5pow/items/f4326aed6c0b63617ebd https://qiita.com/11ohina017/items/da2ae5b039257752e558

Add uWSGI to service
$ sudo vi /etc/systemd/system/uwsgi.service

#Add the following
[Unit]
Description=uWSGI
After=syslog.target

[Service]
ExecStart=/home/ubuntu/py36_flask_server/bin/uwsgi --ini /home/ubuntu/application/flask_web_application/uwsgi.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Install uWSGI in virtual environment in ExecStart Specify the configuration file created earlier in --ini

Add uWSGI to service

$ sudo systemctl enable uwsgi

Nginx

Turn off the default Nginx welcome page

Delete conf.d / default.conf if it exists

$ sudo rm /etc/nginx/conf.d/default.conf

Remove / nginx / sites-enabled / default if present

$ sudo rm /etc/nginx/sites-enabled/default

This will make the default site disappear

Create sites-available and sites-enabled if they do not exist

$ sudo mkdir /etc/nginx/sites-available
$ sudo mkdir /etc/nginx/sites-enabled

What are sites-available and sites-enabled after all Basically for using multiple domains on one server Easy to manage

However, this time I don't use multiple domains separately. Due to the structure of Ubuntu + Nginx + uWSGI More stable than using conf.d

How to use -Create a domain-specific configuration file in sites-available -Create a site-available symbolic link to sites-enabled

Very simple thing Symbolic links are so-called shortcuts

In the http block part of /etc/nginx/nginx.conf Rewrite /etc/nginx/conf.d/*.conf

$ sudo vi /etc/nginx/nginx.conf

http{
…
include /etc/nginx/conf.d/*.conf;
}

http{
…
include /etc/nginx/sites-enabled/*;
}

Added /etc/nginx/sites-available/myapplication.conf

$ sudo vi /etc/nginx/sites-available/myapplication.conf

#Add the following
server {
    listen       80;
    root /var/www/html;
    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
}

Roughly speaking, try_files with the URL as it is A structure that searches from root and internally redirects to uWSGI (with the URL as it is) if it does not exist root has nothing in particular You can throw everything to uWSGI, but the formula is as above

uwsgi_params is a uWSGI configuration file unix: ///tmp/uwsgi.sock is a UNIX domain socket This is what is called to communicate with uWSGI.

You can also connect via TCP, but it will occupy the port. Better to use domain sockets

If you create a shortcut, nginx settings are complete

$ sudo ln -s /etc/nginx/sites-available/myapplication.conf /etc/nginx/sites-enabled/myapplication

Access It is recommended to restart the entire system once here. If you can't, just restart Nginx
$ sudo systemctl restart nginx

If uwsgi is not running, start it

& sudo systemctl start uwsgi

uwsgi error checking

$ sudo systemctl status uwsgi

nginx error checking

$ sudo systemctl status nginx

Read through and if there seems to be no error, try accessing

$ curl http://127.0.0.1
Hello, World

Summary Basically it is faster to let Nginx process static files

Please let me know if there is something wrong with this

Recommended Posts