Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

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!
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
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.
Hi @broodforge
There is a very big possibility that it’s the Flask application that is crashing, which is why Nginx is returning 502, since it has nothing to connect to.
You need to enable logging in your application to see what is going on:
ExecStart=/home/blizzard/myproject/myprojectenv/bin/uwsgi --ini myapp.ini --uid blizzard --gid www-data --logto /home/blizzard/myproject/error.log
Otherwise you can enable debug logging if you don’t see enough in the logs: http://stackoverflow.com/questions/32722143/flask-application-traceback-doesnt-show-up-in-server-log
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.