How to deploy Flask app with Supervisor/Nginx/Gunicorn on Ubuntu server
<p>I am trying to deploy a Flask app on an Ubuntu server. I referenced <a href="http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html?highlight=flask" rel="nofollow">this</a>, <a href="https://realpython.com/blog/python/kickstarting-flask-on-ubuntu-setup-and-deployment/" rel="nofollow">this</a> and <a href="http://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/" rel="nofollow">this</a> and found a lot of similar questions on SO, but I still can't figure it out. </p>

<p>I can run it manually from the source directory by doing <code>uwsgi siti_uwsgi.ini</code> and navigating to <code>http://server_IP_address:8080/</code>. But when I try <code>uwsgi --socket 127.0.0.1:3031 --wsgi-file views.py --master --processes 4 --threads 2</code> and navigate to <code>http://server_IP_address:3031</code>, I get nothing. </p>

<p>If I go to <code>siti.company.loc</code> (the DNS name I set up), there is a standard Nginx 502 error page.</p>

<p>When I try to restart the supervisor process, it dies with a FATAL error:</p>

<blockquote>
  <p>can't find command "gunicorn"</p>
</blockquote>

<p>What am I doing wrong? Let me know if I need to provide more info or background.</p>

<hr>

<p>/webapps/patch/src/views.py (Flask app):</p>

<pre><code>from flask import Flask, render_template, request, url_for, redirect
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {'origins': '*'}})

@app.route('/')
def home():
    return 'Hello'

@app.route('/site:&lt;site&gt;/date:&lt;int:day&gt;-&lt;month&gt;-&lt;int:year&gt;')
def application(site, month, day, year):
    if request.method == 'GET':
        # Recompile date from URL. todo: better way
        dte = str(day) + "-" + str(month) + "-" + str(
        print('about to run')
        results = run_SITI(site, dte)
        return results

def run_SITI(site, dte):
    print('running SITI')
    return render_template('results.html', site=site, dte=dte, results=None)  # todo: Show results

if __name__ == '__main__':
    app.run(debug=True)
</code></pre>

<p>/webapps/patch/siti_wsgi.ini (uWSGI ini):</p>

<pre><code>[uwsgi]
http = :8008
chdir = /webapps/patch/src
wsgi-file = views.py
processes = 2
threads = 2
callable = app
</code></pre>

<p>/etc/nginx/sites-available/siti (Nginx config):</p>

<pre><code>upstream flask_siti {
        server 127.0.0.1:8008 fail_timeout=0;
}
server {
        listen 80;
        server_name siti.company.loc;
        charset utf-8;
        client_max_body_size 75M;

        access_log /var/log/nginx/siti/access.log;
        error_log /var/log/nginx/siti/error.log;

        keepalive_timeout 5;

        location /static {
                alias /webapps/patch/static;
        }
        location /media {
                alias /webapps/patch/media;
        }
        location / {
                # checks for static file, if not found proxy to the app
                try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
                proxy_redirect off;
                proxy_set_header Host $http_host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://flask_siti;
        }
}
</code></pre>

<p>/etc/supervisor/conf.d/siti.conf (Supervisor config):</p>

<pre><code>[program:webapp_siti]
command=gunicorn -b views:app
directory=/webapps/patch/src
user=nobody
autostart=true
autorestart=true
redirect_stderr=true
</code></pre>

<p>/var/log/nginx/siti/error.log (Nginx error log):</p>

<pre><code>2016/08/30 11:44:42 [error] 25524#0: *73 connect() failed (111: Connection refused) while connecting to upstream, $
2016/08/30 11:44:42 [error] 25524#0: *73 connect() failed (111: Connection refused) while connecting to upstream, $
2016/08/30 11:44:42 [error] 25524#0: *73 no live upstreams while connecting to upstream, client: 10.1.2.195, serve$
</code></pre>