16

I am new to django, i would like to know how to set up my django project with nginx and gunicorn. I read this guide: http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/ but it doesn't work for my project. I think that it is due to the particular structure of my project, that is:

├──icecream
│   ├── settings
│   |    ├── __init.py
│   |    ├── base.py
│   |    ├── local.py
│   |    ├── production.py
│   ├── __init__.py
│   ├── urls.py
│   ├── wsgi.py
├── manage.py

I got this layout from: https://github.com/twoscoops/django-twoscoops-project. Can anyone help me, please? thank you

4
  • 1
    I use a very similar structure and Michał Kurzyński's guide worked for me. Tell us exactly what is wrong. What do you mean by "it doesn't work"? Commented Nov 23, 2013 at 14:02
  • It seems that it does not manage to find the settings of the project. when i digit: (myenv) $ gunicorn_django --bind example.com:8001 does not work at all Commented Nov 23, 2013 at 14:16
  • Please, edit your post ad show us at least your gunicorn_start file and the error message. Commented Nov 23, 2013 at 14:40
  • my gunicorn_start is dpaste.de/xbuQ#L4,10,16 and the error log is: dpaste.de/TITD Commented Nov 23, 2013 at 20:03

1 Answer 1

42

I'll just summarize the steps for deploying a django application with nginx & gunicorn here:

1. Install nginx and add this to /etc/nginx/sites-enabled/default

server {

  server_name 127.0.0.1 [email protected];
  access_log /var/log/nginx/domain-access.log;

  location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For  $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;

    # This line is important as it tells nginx to channel all requests to port 8000.
    # We will later run our wsgi application on this port using gunicorn.
    proxy_pass http://127.0.0.1:8000/;
  }

}

2. Install gunicorn

$ pip install gunicorn

3. Start your django project using gunicorn and the wsgi.py file

$ cd </path/to/djangoproject_subdirectory_with_wsgi.py>

$ gunicorn wsgi -b 127.0.0.1:8000 --pid /tmp/gunicorn.pid --daemon

# --daemon parameter tells gunicorn to run in the background
# So that gunicorn continues to run even if you close your ssh session
# (You cannot remain ssh-ed into your server all the time right!)

Please do not use "wsgi.py"; you just have to use wsgi without the ".py" extension when calling gunicorn. This will start your wsgi application in the background.

4. Visit "[email protected]" in your browser

Now your application must be up and running on your instance. Visit:

http://[email protected]/

and see if your application is running. Do not forget to replce [email protected] in the above and in the nginx configuration file before.

5. (Optional) Additional Notes

  • In Step 1, if confused; remove all existing lines from the /etc/nginx/sites-enabled/default file and put the above code inside it. (Or delete and create a new blank file and add the code)

  • If you are using virtualenv and you did apip install gunicorn inside the virtualenv in Step 2, then run the Step 3 command with respective virtualenv activated.

  • The pid of the gunicorn process is stored in /tmp/gunicorn.pid; incase you want to kill the existing gunicorn process and restart it.

  • supervisord might be used in conjunction which helps in restarting the gunicorn daemon automatically in case it dies due to some reason. This is useful in production environments.

Sign up to request clarification or add additional context in comments.

1 Comment

Why do you point to port 8000 and not the unix socket created by gunicorn?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.