lucy
lucy

Reputation: 4506

Unable to run flask wsgi app with nginx

I have tried this blog to deploy flask app using wsgi server on production

I have followed all the steps which are mentioned

sudo apt-get install build-essential python-dev python-pip

sudo pip install virtualenv uwsgi

bash virtualenv env source env/bin/activate pip install flask pip install -r requirements.txt

touch /home/lucy/myproject.sock

created wsgi.py

from myproject import app

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

created a systemd Unit File.

sudo vi /etc/systemd/system/myproject.service




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

[Service]
User=lucy
Group=www-data
WorkingDirectory=/home/lucy/
Environment="PATH=/home/lucy/env/bin"
ExecStart=/home/lucy/env/bin/uwsgi --ini myproject.ini

[Install]
WantedBy=multi-user.target

We can now start the uWSGI service we created and enable it so that it starts at boot:

bash sudo systemctl start myproject sudo systemctl enable myproject

Connecting uWSGI to Nginx

First, install Nginx (if you haven't already).

bash sudo apt-get install nginx

sudo vi /etc/nginx/sites-available/myproject

server {
    listen 80;
    server_name server_domain_or_IP;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/lucy/myproject.sock;
    }
}

bash sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled sudo systemctl restart nginx

still, project is not run nginx gives connection refuse error but when I tried

uwsgi -s /home/lucy/myproject.sock -w wsgi:app -H /home/lucy/env --http-processes=10 --chmod-socket=666 --master 

project is running sucessfully.

Can anyone please tell what I am doing wrong here?

Upvotes: 0

Views: 785

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174614

Your problem is that uwsgi is not installed in the virtual environment, yet in your service file you are asking it to load from the virtual environment path:

ExecStart=/home/lucy/env/bin/uwsgi --ini myproject.ini

You should either install uwsgi in your virtual environment, or give the path to the global uwsgi in your service description; you can get that by typing which uwsgi in your shell.

It is recommended to install uwsgi in your virtual environment as it will contain the latest updates and features.

To do this run:

env/bin/pip install uwsgi

Make sure in all cases to restart the service (if you change the service definition file, you'll have to reload the service as well).

Upvotes: 3

Related Questions