Nginx – “Connection in use” running Gunicorn using VirtualEnv

djangogunicornnginxvirtualenv

I am trying to set up a development environment using Gunicorn and NGINX.

While following this tutorial: http://ijcdigital.com/blog/django-gunicorn-and-nginx-setup/

I get the following error:

(WebApp)sl@cker:~/MyApps/WebApp$ gunicorn_django --bind=127.0.0.1:8001
2012-07-12 14:04:30 [5997] [INFO] Starting gunicorn 0.14.5
2012-07-12 14:04:30 [5997] [ERROR] Connection in use: ('127.0.0.1', 8001)
2012-07-12 14:04:30 [5997] [ERROR] Retrying in 1 second.
2012-07-12 14:04:31 [5997] [ERROR] Connection in use: ('127.0.0.1', 8001)
2012-07-12 14:04:31 [5997] [ERROR] Retrying in 1 second.
2012-07-12 14:04:32 [5997] [ERROR] Connection in use: ('127.0.0.1', 8001)
2012-07-12 14:04:32 [5997] [ERROR] Retrying in 1 second.
2012-07-12 14:04:33 [5997] [ERROR] Connection in use: ('127.0.0.1', 8001)
2012-07-12 14:04:33 [5997] [ERROR] Retrying in 1 second.
2012-07-12 14:04:34 [5997] [ERROR] Connection in use: ('127.0.0.1', 8001)
2012-07-12 14:04:34 [5997] [ERROR] Retrying in 1 second.
2012-07-12 14:04:35 [5997] [ERROR] Can't connect to ('127.0.0.1', 8001)

My webapp.sh file looks like this:

#!/bin/bash
set -e
LOGFILE=/home/sl/MyApps/WebApp/logs/webapp.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=3
# user/group to run as
USER=sl
GROUP=sl
ADDRESS=127.0.0.1:8001
cd /home/sl/MyApp/WebApp
source /home/sl/VirtualEnvs/WebApp/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec gunicorn_django -w $NUM_WORKERS --bind=$ADDRESS \
  --user=$USER --group=$GROUP --log-level=debug \
  --log-file=$LOGFILE 2>>$LOGFILE

When browsing to http://127.0.0.1:8001/, the "It worked!
Congratulations on your first Django-powered page."
is displayed correctly.

Can I just leave it like this and continue the tutorial or did I do something wrong here?

Best Answer

I know this is an old question, but I think I have an alternative answer to this.

I was experiencing the same problem. The log would show gunicorn trying to start and then lots of entries showing the port is already in use.

I realised, that I had entered the virtualenv BEFORE running my script. Which then activated the virtualenv again. This seems to run the script twice on the trot. As soon as I deactivated my console virtualenv and let the script handle the virtualenv itself, everything worked as expected.

Hope this helps someone else.