Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uWSGI / Flask: "no python application found, check your startup logs for errors"

Tags:

nginx

flask

uwsgi

I have a website running with NGINX + uWSGI + Flask.

The website runs fine most of the time, however every now and then it gets into a state where the pages returned by nginx just show "Internal Server Error". If I look at the uWSGI log when it's doing this, I see the following:

[pid: 1580|app: -1|req: -1/37] 69.162.124.228 () {46 vars in 716 bytes} [Sat May 12 10:25:13 2018] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)
--- no python application found, check your startup logs for errors ---

It can be in this state for ages, but if I do something seemingly unrelated, like make an arbitrary change to the flask app, then everything will start working again. It will then keep working until some random amount of time later when it starts giving "Internal Server Error" again, without any code changes being made.

I've tried running the uWSGI application directly, and it runs without any error.

I've tried installing Sentry on my flask app to capture any errors, but nothing shows up when this happens.

  • How do I diagnose this?

  • What other log files can I look at?

  • What is likely to be causing this?

I've been at this for over a week now, and read through almost every related question of SO. I've run out of ideas, and near abandoning this project if I can't figure out what's going on. Any help would be greatly appreciated.

Here are my files:

uWSGI config (mysite.ini)

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = mysite.sock
chmod-socket = 660
vacuum = true

die-on-term = true

logto = /var/log/uwsgi/%n.log

wsgi.py

from tunnelling.python.flask_app import app as application

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

nginx:

server {
    listen 80;
    server_name www.mysite.com;
    server_name mysite.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/dimraft/mysite/mysite.sock;
    }

   listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}

systemd file:

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

[Service]
User=dimraft
Group=www-data
WorkingDirectory=/home/dimraft/mysite
Environment="PATH=/home/dimraft/mysite/mysiteenv/bin"
ExecStart=/home/dimraft/mysite/mysiteenv/bin/uwsgi --ini mysite.ini

[Install]
WantedBy=multi-user.target
like image 622
user1592096 Avatar asked May 12 '18 11:05

user1592096


3 Answers

maybe your uWSGI config (mysite.ini) error:

change:

module = wsgi:app

to:

module = projectName.wsgi:application
like image 75
John Avatar answered Nov 19 '22 07:11

John


@John was essentially correct on this but, I would like to elaborate for clarity.

I was having the same issue with a project that had essentially the same setup and what fixed it was to move my uwsgi.py module to inside my flask application package like so

myprojectname/
    __init__.py
    uwsgi.py

then change the module definition in uwsgi.ini to module=myprojectname.uwsgi:application like shown here:

[uwsgi]
 module=myprojectname.uwsgi:application
 master=true
 processes=2

 socket=myprojectname.sock
 chmod-socket=660
 logto=/var/log/uwsgi/uwsgi.log
 die-on-term=true

Also to make sure to specify that the callable object is named application as was the case in the original posted code sample. uWSGI defaults to looking for this application named object and I've really struggled to get it to work naming it anything else such as app.

like image 43
SciGuyMcQ Avatar answered Nov 19 '22 07:11

SciGuyMcQ


okay sorry this is coming late. i had an error like this too and this is how i solved mine

[uwsgi]
chdir = /home/goodnews/myproject
home = /home/goodnews/myproject/venv
module = wsgi:app
master = true
processes = 5

#socket = myproject.sock
socket = :5000
protocol = http
chmod-socket = 666
vaccum = true
die-on-term = true

in my case i was working on a local virtual machine(running an Ubuntu terminal), that is the reason for

socket = :5000

i was testing a webapp using ssh connection to Ubuntu server. hopes this helps someone

like image 3
goodnews john Avatar answered Nov 19 '22 08:11

goodnews john