title: Flask - Deployment with Gunicorn, Nginx and Supervisor, Supervisor error log
body: <p>I've deployed a Flask app with Gunicorn + Nginx + Supervisor. It doesn't works.
  Searching online turned up several reports of similar issues, but none with an explanation that fit our circumstances or a fix that resolved the issue.
  <a href="http://harkablog.com/error-starting-gunicorn-in-supervisor.html" rel="nofollow">Here</a> said to turn off daemonize gunicorn. If I'm right, it's not my case.</p>
  
  <p>It seems to be that something start a service that listening 8000 port already.
  Supervisor sends errors to the log file every second.</p>
  
  <p>What can help me?</p>
  
  <p>The error from the Supervisor app-stderr.log:</p>
  
  <pre><code>[2015-02-19 18:56:19 +0300] [964] [INFO] Starting gunicorn 19.2.1
  [2015-02-19 18:56:19 +0300] [964] [INFO] Listening at: http://127.0.0.1:8000 (964)
  [2015-02-19 18:56:19 +0300] [964] [INFO] Using worker: sync
  [2015-02-19 18:56:19 +0300] [1078] [INFO] Booting worker with pid: 1078
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
  [2015-02-19 18:56:20 +0300] [1081] [INFO] Starting gunicorn 19.2.1
  [2015-02-19 18:56:20 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:20 +0300] [1081] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:21 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:21 +0300] [1081] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:22 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:22 +0300] [1081] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:23 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:23 +0300] [1081] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:24 +0300] [1081] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:24 +0300] [1081] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:25 +0300] [1081] [ERROR] Can't connect to ('localhost', 8000)
  [2015-02-19 18:56:25 +0300] [1078] [INFO] Worker exiting (pid: 1078)
  [2015-02-19 18:56:25 +0300] [1122] [INFO] Booting worker with pid: 1122
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
  [2015-02-19 18:56:27 +0300] [1148] [INFO] Starting gunicorn 19.2.1
  [2015-02-19 18:56:27 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:27 +0300] [1148] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:28 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:28 +0300] [1148] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:29 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:29 +0300] [1148] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:30 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:30 +0300] [1148] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:31 +0300] [1148] [ERROR] Connection in use: ('localhost', 8000)
  [2015-02-19 18:56:31 +0300] [1148] [ERROR] Retrying in 1 second.
  [2015-02-19 18:56:32 +0300] [1148] [ERROR] Can't connect to ('localhost', 8000)
  [2015-02-19 18:56:32 +0300] [1122] [INFO] Worker exiting (pid: 1122)
  [2015-02-19 18:56:32 +0300] [1206] [INFO] Booting worker with pid: 1206
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
   * Restarting with stat
  [2015-02-19 18:56:32 +0300] [1211] [INFO] Starting gunicorn 19.2.1
  [2015-02-19 18:56:32 +0300] [1211] [ERROR] Connection in use: ('localhost', 8000)
  </code></pre>
  
  <p>My supervisor.conf:</p>
  
  <pre><code>[program:app]
  command = /home/www/app/flask/bin/gunicorn app:app -b localhost:8000 --preload
  directory = /home/www/app
  user = webhost
  </code></pre>
  
  <p>Netstat -tulpin</p>
  
  <pre><code>Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      930/nginx
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      855/sshd
  tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      1392/python3
  tcp6       0      0 :::22                   :::*                    LISTEN      855/sshd
  </code></pre>
  
  <p>And ps aux | grep python</p>
  
  <pre><code>root       954  0.1  2.4  60564 12440 ?        Ss   18:56   0:01 /usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
  webhost   1392  0.0  3.2  67148 16512 ?        S    19:00   0:00 /home/www/app/flask/bin/python3 /home/www/app/flask/bin/gunicorn app:app -b localhost:8000
  webhost   2124  5.2  4.2  86072 21344 ?        S    19:12   0:00 /home/www/app/flask/bin/python3 /home/www/app/flask/bin/gunicorn app:app -b localhost:8000
  webhost   2126  5.2  3.2  67148 16488 ?        S    19:12   0:00 /home/www/app/flask/bin/python3 /home/www/app/flask/bin/gunicorn app:app -b localhost:8000
  webhost   2130  0.0  0.1  11744   920 pts/0    S+   19:13   0:00 grep --color=auto python
  </code></pre>
  
  <p>UPD.
  supervisor config is above</p>
  
  <p>nginx.conf</p>
  
  <pre><code>server {
      location / {
          proxy_pass http://127.0.0.1:8000;
      }
      location /static {
          alias  /home/www/myapp/app/static/;
      }
  }
  </code></pre>
  
  <p>File sctructure</p>
  
  <pre><code>myapp
    |
    |-app-
    |    |
    |    __init__.py  
    |
    run.py
    |
    config.py
  </code></pre>
  
  <p>__init__.py</p>
  
  <pre><code>from flask import Flask
  
  app = Flask(__name__)
  app.config.from_object('config')
  
  from app import views
  </code></pre>
  
  <p>run.py</p>
  
  <pre><code>#!flask/bin/python
  from app import app
  app.run()
  </code></pre>
  
  <p>config.py</p>
  
  <pre><code># For valid forms
  CSRF_ENABLED = True
  SECRET_KEY = 'never-guess'
  </code></pre>