Error 502 when I try to deploy a Flask app with NGINX, uWSGI on Ubuntu

November 25, 2018 1k views
Nginx Python Ubuntu 16.04

I tried to configure a python application (based on Flask) on Ubuntu sever 16.04. I used Nginx, uWSGI and I followed this link: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04 but I get error 502.

The uwsgi.log file gives me this error:

detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /var/www/dev/develop/api/socket.sock fd 3
Python version: 3.6.7 (default, Oct 21 2018, 04:56:05)  [GCC 5.4.0 20160609]
Set PythonHome to /var/www/dev/develop/api/my-api
Python main interpreter initialized at 0x1432e60
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 1313856 bytes (1283 KB) for 64 cores
*** Operational MODE: preforking+threaded ***
added /var/www/dev/develop/api/ to pythonpath.
ModuleNotFoundError: No module named '/var/www/dev/develop/api/app'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 12701)
spawned uWSGI worker 1 (pid: 12705, cores: 8)
spawned uWSGI worker 2 (pid: 12706, cores: 8)
spawned uWSGI worker 3 (pid: 12707, cores: 8)
spawned uWSGI worker 4 (pid: 12708, cores: 8)
spawned uWSGI worker 5 (pid: 12709, cores: 8)
spawned uWSGI worker 6 (pid: 12710, cores: 8)
spawned uWSGI worker 7 (pid: 12711, cores: 8)
spawned uWSGI worker 8 (pid: 12729, cores: 8)
--- no python application found, check your startup logs for errors ---
[pid: 12711|app: -1|req: -1/1] 162.158.210.49 () {58 vars in 1117 bytes} [Sun Nov 25 14:36:42 2018] POST //auth/refresh => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 byte$
announcing my loyalty to the Emperor...
--- no python application found, check your startup logs for errors ---
[pid: 12711|app: -1|req: -1/2] 162.158.210.109 () {56 vars in 1016 bytes} [Sun Nov 25 16:48:38 2018] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches$
--- no python application found, check your startup logs for errors ---
[pid: 12706|app: -1|req: -1/3] 162.158.210.73 () {58 vars in 1021 bytes} [Sun Nov 25 16:48:39 2018] GET /favicon.ico => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes ($
announcing my loyalty to the Emperor...

The content of the /var/www/dev/develop/api/run.py is:

from app import app

if __name__ == "__main__":
    app.run()

app.py contains the Flask app.

The content of /var/www/dev/develop/api/uwsgi.ini is:

[uwsgi]
base = /var/www/dev/develop/api
app = run
module = %(base)/app

master = true
home = %(base)/my-api
pythonpath = %(base)

socket = %(base)/socket.sock
chmod-socket = 777

processes = 8

threads = 8

harakiri = 15

callable = app

#vacuum = true

logto = /var/www/dev/develop/api/log/%n.log

The content of /etc/systemd/system/uwsgidevapi.service is:

[Unit]
Description=uWSGI dev api

[Service]
ExecStart=/var/www/dev/develop/api/my-api/bin/uwsgi --master --emperor /var/www/dev/develop/api/uwsgi.ini --die-on-term --uid www-data --gid www-data --logto /var/www/dev/develop/api/log/emperor.log
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

The content of /etc/nginx/sites-available/my-app.conf is:

server {
        listen 80;
        #listen [::]:80;

        real_ip_header X-Forwarded-For;
        set_real_ip_from 127.0.0.1;

        server_name dev-api.my-app.co;

        # Error pages
        error_page 401 /custom_401.html;
        error_page 403 /custom_403.html;
        error_page 404 /custom_404.html;
        error_page 400 402 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 426 428 429 431 440 444 449 450 451 494 495 496 497 498 /custom_ERROR.html;
        error_page 500 501 502 503 504 505 506 507 508 509 510 511 520 522 598 599 /custom_50x.html;
        location = /custom_401.html {
                root /usr/share/nginx/html;
                internal;
        }
        location = /custom_403.html {
                root /usr/share/nginx/html;
                internal;
        }
        location = /custom_404.html {
                root /usr/share/nginx/html;
                internal;
        }
        location = /custom_50x.html {
                root /usr/share/nginx/html;
                internal;
        }
        location = /custom_ERROR.html {
                root /usr/share/nginx/html;
                internal;
        }

        # Service
        location / {
                include uwsgi_params;
                uwsgi_pass unix:///var/www/dev/develop/api/socket.sock;
                uwsgi_modifier1 30;
        }
}

Be the first one to answer this question.