Question

Flask+nginx+uwsgi+Ubuntu tutorial

My hopes are lost and so is my temper. I have followed the tutorial found here - https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04

I have reinstalled and started all over 10 times and still same PROBLEM. Is there another tutorial available because this only works to 80%. All works fine up to a certain point but I always get a 502 gateway problem.

I created a new user belonging to sudo group. Ssh:ed to server with this new user – all good. Set up domain also and if I ping domainname.com the correct droplet ip gives reply – all good.

python myproject.py (to start flask) works perfect

uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

Works perfect and site is accessible with IP http://server_IP:5000

Content of wsgi.py-

from myproject import app

if name == “_main”: app.run()

Content of myproject.ini looks like -

[uwsgi]

module = wsgi:app

master = true processes = 5

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

die-on-term = true

Content of /etc/systemd/system/myproject.service:

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

[Service] User = blizzard Group = www-data WorkingDirectory = /home/blizzard/myproject Environment = “PATH=/home/blizzard/myproject/myprojectenv/bin” #IS THIS LINE WRONG?? ExecStart=/home/blizzard/myprojectenv/bin/uwsgi --ini myproject.ini

[Install] WantedBy = multi-user.target

Content of /etc/nginx/available-sites/myproject

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

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

}

If this tutorial is wrong why dont staff delete it or at least the author updates it since I am not the ONLY ONE having problems. If there is a helpful soul who could guide me I would be more than happy.

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

@broodforge

Here’s what I did, step-by-step, on a test Droplet with Ubuntu 16.04 64bit.

Update, Upgrade, Install

apt-get update \
&& apt-get -y dist-upgrade \
&& apt-get -y install python3-pip python3-dev nginx \
&& pip3 install virtualenv

Create New User + Directories

mkdir -p /home/sammy/myproject \
&& useradd -d /home/sammy sammy

Setup VirtualEnv

cd /home/sammy/myproject \
&& virtualenv myprojectenv

Source Env File

source /home/sammy/myproject/myprojectenv/bin/activate

Install Flask and UWSGI

pip install uwsgi flask

Create Project File

nano /home/sammy/myproject/myproject.py

Within it, I pasted:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

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

Testing myproject.py

python myproject.py

I see the Hello There as I should, so let’s move on…

Create the WSGI Entry Point

nano /home/sammy/myproject/wsgi.py

Within it, I pasted:

from myproject import app

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

Testing UWSGI

uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

I see the Hello Three as I should, so let’s move on.

Deactivate

deactivate

Creating a uWSGI Configuration File

nano /home/sammy/myproject/myproject.ini

Within it, I pasted:

[uwsgi]
module = wsgi:app

master = true
processes = 5

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

die-on-term = true

Creating systemd Service File

nano /etc/systemd/system/myproject.service

Within it, I pasted:

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

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

[Install]
WantedBy=multi-user.target

Setting Proper Permissions

Add sammy to www-data group:

usermod -aG www-data sammy
chown -R sammy:www-data /home/sammy/*

Starting and Enabling the Service

systemctl start myproject
systemctl enable myproject

Setting Up NGINX

nano /etc/nginx/sites-available/myproject

Within it, I pasted:

server {
    listen 80;
    server_name MY_DROPLET_IP;

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

Created a symlink using:

ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Restart NGINX:

systemctl restart nginx

For this particular setup, passing --uid and --gid isn’t required and systemctl does handle the user and group permissions correctly.

I just want to Thank You for this post and to all who helped solve these issues. It really helped me out BIG TIME. I like you was stuck and very frustrated, but this thread was a godsend.

@broodforge

Starting a new reply as the other was at its max.

You could very well use the same user and group, though from a security standpoint, it’s not the best method. Ideally, each project should run as an individual user (that isn’t root or a sudo user).

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel