Home > Enterprise >  Trying to set up flask with nginx and gunicorn
Trying to set up flask with nginx and gunicorn

Time:05-08

I have followed a simple guide, and trying to show my flask application, but it wont show. I am running everything through ssh on a ubuntu server.

Have my flask app and wsgi.py in /var/www/application/

My nginx config is:

server {
        listen 80;
        server_name application;
 
        access_log /var/log/nginx/application.access.log;
        error_log /var/log/nginx/appliation.error.log;
 
        location / {
                include proxy_params;
                proxy_pass http://unix:/var/www/application/application.sock;
        }
}

I have symlinked the file to /etc/nginx/sites-enabled/

Then here is my gunicorn application config located in: /etc/systemd/system/application.service

[Unit]
Description=application.service - A Flask application run with Gunicorn.
After=network.target
 
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/application/
ExecStart=/usr/bin/gunicorn --workers 3 --bind unix:/var/www/application.sock wsgi:app
 
[Install]
WantedBy=multi-user.target

Have checked all processes are running, and systemd status ok, but when i try to curl

http://application

I get nothing?

My app.py

from flask import Flask
 
app = Flask(__name__)
@app.route("/")
def home():
    return "<h1>Nginx & Gunicorn</h1>"

wsgi.py

from main import app
 
if __name__ == "__main__":
    app.run(debug=True)

What am I missing here, how to get it working?

CodePudding user response:

ExecStart=/usr/bin/gunicorn --workers 3 --bind unix:/var/www/application.sock wsgi:app

the problem is the path /var/www/application.sock, it should be: /var/www/application/application.sock

You can still view the status of the service with the command: sudo systemctl status <service_name>

To understand if the command specified in the service file is correct, or if the application has problems starting, try to start it directly from the command line:

cd /var/www/application/ && /usr/bin/gunicorn --workers 3 --bind unix:/var/www/application/application.sock wsgi:app
  • Related