Ceej
Ceej

Reputation: 125

uWSGI configuration with FlaskApp

In effort to switch to Nginx, I'm running into configuration problems and getting a 502 Gateway error. Here is error log on connection:

tail -f error.log
2016/10/08 16:09:31 [crit] 21682#21682: *29 connect() to unix:/var/www/FlaskApp/FlaskApp/runserver.sock failed (13: Permission denied) while connecting to upstream, client: 73.188.249.47, server: ceejcalvert.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/var/www/FlaskApp/FlaskApp/runserver.sock:", host: "website.com"

If I'm in the terminal on the server I can get the site up by manually directing the socket via:

This command will get everything working if run:

uwsgi -s /var/www/FlaskApp/FlaskApp/runserver.sock -w runserver:app --chmod-socket=666

The issue is I cannot get it working in daemon mode. My configuration is as follows:

$ cat /etc/systemd/system/runserver.service 

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

[Service]
User=username
Group=www-data
WorkingDirectory=/var/www/FlaskApp/FlaskApp
Environment="PATH=/var/www/FlaskApp/FlaskApp/venv/bin"
ExecStart=/var/www/FlaskApp/FlaskApp/venv/bin/runserver.sock --ini runserver.ini

[Install]
WantedBy=multi-user.target

...

cat /var/www/FlaskApp/FlaskApp/runserver.ini 

[uwsgi]
module = wsgi:app

master = true
processes = 5

logto = /home/jmc856/error.log

socket = runserver.sock
chmod-socket = 666
vacuum = true

die-on-term = true

Assume site-available is linked to sites-enabled

cat /etc/nginx/sites-available/runserver 

server {
    listen 80;
    server_name website.com;

location / {
    include uwsgi_params;
    uwsgi_pass unix:/var/www/FlaskApp/FlaskApp/runserver.sock;
    }
}

Anything obvious I'm missing?

When I run the following, I get exit code 3.

sudo systemctl status runserver

runserver.service - uWSGI instance to serve runserver
Loaded: loaded (/etc/systemd/system/runserver.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2016-10-08 16:08:45 EDT; 20min ago
Main PID: 22365 (code=exited, status=203/EXEC)

Oct 08 16:08:45 FirstNameLastName.com systemd[1]: Stopped uWSGI instance to serve runserver.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: Started uWSGI instance to serve runserver.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Main process exited, code=exited, status=203/EXEC
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Unit entered failed state.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Failed with result 'exit-code'.

Upvotes: 1

Views: 2108

Answers (2)

Ceej
Ceej

Reputation: 125

Solved my issue. For the most part my configurations were fine. Here is a checklist of things to ensure if you get 502 gateway errors.

1) I first added all absolute paths to config files. For example I changed my systemd config to:

$ cat /etc/systemd/system/runserver.service 

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

[Service]
User=username
Group=www-data
WorkingDirectory=/var/www/FlaskApp/FlaskApp
Environment="PATH=/var/www/FlaskApp/FlaskApp/venv/bin"
ExecStart=/var/www/FlaskApp/FlaskApp/venv/bin/runserver.sock --ini    /var/www/FlaskApp/FlaskApp/runserver.ini

[Install] WantedBy=multi-user.target

2) Changed .ini file to directly call uWSGI app:

 cat /var/www/FlaskApp/FlaskApp/runserver.ini 

[uwsgi]
chdir=/var/Webpage/
wsgi-file = wsgi.py
callable = app

master = true
processes = 5

logto = /home/error.log

socket = runserver.sock
chmod-socket = 666
vacuum = true

die-on-term = true

3) Ensure FlaskApp host is 0.0.0.0:

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

4) Use these commands to try to find out where things are failing.

Make sure configs have proper syntax

$ sudo nginx -t

Make sure nginx daemon running properly

$ systemctl status nginx.service

Ensure uWSGI instance to serve {app} is running.

$ systemctl

If all is good and still finding errors, search for failure in

$ sudo journalctl

And

$ sudo tail -f /var/log/nginx/error.log

If everything is running properly, make sure you perform the following:

$ sudo systemctl restart {app}
$ sudo start {app}
$ sudo enable {app}

That last command was something I forgot and prevented me from realizing my configuration was fine for a long time. In my case {app} was 'runserver'

Upvotes: 1

Connor Wilkins
Connor Wilkins

Reputation: 23

I've been using this Docker image for all my nginx and flask apps https://github.com/tiangolo/uwsgi-nginx-flask-docker

Upvotes: 0

Related Questions