title: Running Gunicorn behind chrooted nginx inside virtualenv
body: <p>I can this setup to work if I start gunicorn manually or if I add gunicorn to my django installed apps. But when I try to start gunicorn with systemd the gunicorn socket and service start fine but they don't serve anything to Nginx; I get a 502 bad gateway.</p>
  
  <p>Nginx is running under the "http" user/group, chroot jail. I used pythonbrew to setup the virtualenvs so gunicorn is installed in my home directory under .pythonbrew. The vitualenv directory is owned by my user and the adm group.</p>
  
  <p>I'm pretty sure there is a permission issue somewhere, because everything works if I start gunicorn but not if systemd starts it. I've tried changing the user and group directives inside the gunicorn.service file, but nothing worked; if root start the server then I get no errors and a 502, if my user starts it I get no errors and 504.</p>
  
  <p>I have checked the Nginx logs and there are no errors, so I'm sure it's a gunicorn issue. Should I have the virtualenv in the app directory? Who should be the owner of the app directory? How can I narrow down the issue? </p>
  
  <p><strong>/usr/lib/systemd/system/gunicorn-app.service</strong></p>
  
  <pre><code>#!/bin/sh
  
  [Unit]
  Description=gunicorn-app
  
  [Service]
  ExecStart=/home/noel/.pythonbrew/venvs/Python-3.3.0/nlp/bin/gunicorn_django
  User=http
  Group=http
  Restart=always
  WorkingDirectory = /home/noel/.pythonbrew/venvs/Python-3.3.0/nlp/bin
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p><strong>/usr/lib/systemd/system/gunicorn-app.socket</strong></p>
  
  <pre><code>[Unit] 
  Description=gunicorn-app socket 
  
  [Socket] 
  ListenStream=/run/unicorn.sock 
  ListenStream=0.0.0.0:9000 
  ListenStream=[::]:8000 
  
  [Install] 
  WantedBy=sockets.target
  </code></pre>
  
  <p>I realize this is kind of a sprawling question, but I'm sure I can pinpoint the issue with a few pointers. Thanks.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I'm starting to narrow this down. When I run gunicorn manually and then run <code>ps aux|grep gunicorn</code> then I see two processes that are started: master and worker. But when I start gunicorn with systemd there is only one process started. I tried adding <code>Type=forking</code> to my <code>gunicorn.services</code> file, but then I get an error when loading service. I thought that maybe gunicorn wasn't running under the virtualenv or the venv isn't getting activated? </p>
  
  <p>Does anyone know what I'm doing wrong here? Maybe gunicorn isn't running in the venv?</p>