Setting up NGINX + Django + uWSGI (a tutorial that actually works)

So after reading the various tutorials online for setting up NGINX + Django + uWSGI and all of them not working correctly, I decided to write my own.

This tutorial was tested on a blank install of Ubuntu Server 12.04 LTS 64-bit, if you follow the steps carefully in the correct order all should be well :)

1. Add a new user, give them sudo privileges and switch to that user, below I’ve named mine “user”.

sudo adduser user
sudo adduser user sudo
su user

 

2. Ensure your system hostname is set to localhost

sudo echo "localhost" > /etc/hostname
sudo hostname localhost

 

3. Since this is a new install, update the system.

sudo apt-get update
sudo apt-get upgrade

 

4. Install python, virtual environment builder and python dev

sudo apt-get install python
sudo apt-get install python-virtualenv
sudo apt-get install python2.7-dev

 

5. Install and start the NGINX web server

sudo apt-get install nginx
sudo service nginx start

 

6. Install uWSGI

sudo apt-get install uwsgi

 

7. Setup a Django project

sudo mkdir /var/www
sudo mkdir /var/www/example.com
cd /var/www/example.com
sudo mkdir venv conf src logs

 

This will give the below pictured folder structure
folder structure

 

8. Set-up the virtual environment and activate it

sudo virtualenv /var/www/example.com/venv
source /var/www/example.com/venv/bin/activate

 

9. Install Django

sudo pip install django

 

10. Change to the “src” directory, then copy your Django project files into it

cd /var/www/example.com/src

 

10. Create your uwsgi.ini config file, with the below content

sudo nano /var/www/example.com/conf/uwsgi.ini
[uwsgi]
# variables
projectname = example_project
projectdomain = example.com
base = /var/www/example.com

# config
plugins = python
master = true
protocol = uwsgi
env = DJANGO_SETTINGS_MODULE=%(projectname).settings
pythonpath = %(base)/src/%(projectname)
module = %(projectname).wsgi
socket = 127.0.0.1:8889
logto = %(base)/logs/uwsgi.log
#below line runs it as a daemon in background
daemonize = /var/log/uwsgi/example_project.log

 

11. Create an NGINX config file for this domain, with the below content

sudo nano /var/www/example.com/conf/nginx.conf
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/src/example_project;
    access_log /var/www/example.com/logs/access.log;
    error_log /var/www/example.com/logs/error.log;

    location /static/ { # STATIC_URL
        alias /var/www/example.com/src/static/; # STATIC_ROOT
        expires 30d;
    }

    location /media/ { # MEDIA_URL
        alias /var/www/example.com/src/media/; # MEDIA_ROOT
        expires 30d;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8889;
    }
}

 

12. Edit the main nginx.conf to import our domain conf file, see below content as a guide

sudo nano /etc/nginx/nginx.conf
user    www-data;
# ...
http {
    # ...
    include /var/www/*/conf/nginx.conf;
    # ...
}

 

13. Restart NGINX (to load apply our config changes)

sudo service nginx restart

 

14. Install MySQL and secure it

sudo apt-get install mysql-server
sudo mysql_secure_installation

 

15. Install Python MySQL and uWSGI plugins

sudo apt-get install python-mysqldb
sudo apt-get install uwsgi-plugin-python

 

16. Install south (optional)

sudo pip install south

 

17. Test that uWSGI is working

sudo uwsgi --ini /var/www/example.com/conf/uwsgi.ini

If you visit your site it should now show django. If it doesn’t common causes are:

  • ALLOWED_HOSTS in settings.py isn’t set
  • DEBUG isn’t off in settings.py
  • Database isn’t configured

 

18. Setup uWSGI to run on system boot

Create the following file, with the below content

sudo nano /etc/init/uwsgi.conf
# Emperor uWSGI script

description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [06]

exec uwsgi --master --die-on-term --emperor /var/www/example.com/conf/uwsgi.ini

 

19. Now reboot the server and navigate to your website

sudo reboot

28 thoughts on “Setting up NGINX + Django + uWSGI (a tutorial that actually works)

  1. Joel

    Hey great blog!

    I was able to get everything working perfectly except for outside access to my webserver which sits behind a router. To the best of my knowledge port 80 is forwarded corrently and is open and I saw when I attempted to hit it via it’s local network IP everything worked perfectly but when I tried to get to it from an external IP the request would just hang and time out and the logs showed something like this:

    192.168.0.4 – – [01/May/2013:22:12:16 -0500] “-” 400 0 “-” “-”

    Is there further nginx config needed or is this likely an issue with my router config?

    Reply
    1. Joel

      ah nevermind it seems to only do that when I access the outside IP from within my network. The outside IP seems to resolve just fine to the rest of the world.

      Reply
    2. Dairon Medina

      Ho did u solved it Joel, im having same issue with my API in Django behind uwsgi and nginx, im calling froma mobile app

      Reply
  2. Michael Fillier

    I am getting a uWSGI Error “Python application not found”. Is pythonpath supposed to point to the django project folder? Or the virtualenv?

    Reply
    1. Michael Fillier

      I just fixed the pythonpath issue. But now I am just getting 502 Bad Gateway error. I used the same port as in the example. My error.log reads:

      [error] 3787#0: *5 connect() failed (111: Connection refused) while connecting to upstream … request: “GET / HTTP/1.1”, upstream: “uwsgi://127.0.0.1:8889”

      I am very new to server configuration, do I have to open or listen to port 8889 somehow or do the nginx/uwsgi settings take care of it?

      Reply
      1. richard Post author

        I can’t be sure but have you tried rebooting? (incase another instance is running already) Also do you have debug=false in your settings.py and static file serving paths all set in your nginx.conf?

        Reply
        1. abhishek

          Hi it works great with an application i tried. Now i wanted to know if the same method works with multiple applications? If so how do i change the settings?

          Reply
  3. platform

    To the best of my knowledge port 80 is forwarded corrently and is open and I saw when I attempted to hit it via it’s local network IP everything worked perfectly but when I tried to get to it from an external IP the request would just hang and time out .

    nginx hosting

    Reply
  4. Olga

    Hello.

    I’ve django’s “Internal Server Error” page, so I think that everything fine with nginx, but I need to get some logs and my /var/www/example.com/logs/error.log is completely clean. I’m working on server with root user, so I’ve created this log folder by root. Is it possible, that django can’t write in it? What is the best way for me to solve this problem? (debian is my operation system, if it matter)

    Reply
  5. SunJoo

    Helllo Richard, many thanks indeed and after such much of googling around and trials I finally got things in good shape. You just rock !

    Reply
  6. Redmoses

    I found this tutorial very helpful! Worked perfectly for me! Keep up the great work! Someone had to come up with a proper tutorial for deploying django apps, as the django documentation lacks heavily in that area.

    Reply
  7. ..

    Bad gateway. Tutorial #789543 doesn’t work. Seriously nginx is providing the most newbie-hostile experience I have ever seen. I am seriously tired and frustrated at this point.

    Reply
  8. Alan Sawyer

    I used a different tutorial and got close but couldn’t get it to work, so I’m thinking of reimaging my server and trying yours.
    I do have one question. I will need Emperor mode as I want to run more than one site on nginx.
    As a side note, I also need to run php apps, but I think I can deal with getting that to work with nginx.

    But setting up Emperor mode after I’ve completed your tutorial is needed. Can anyone help?

    Reply
    1. Alan Sawyer

      I think you may have addressed Emperor mode, but I’m used to seeing something like a vassals directory that it keeps monitoring. Instead of do that, do you just add additional sites to the conf file that you’re running at startup?

      Reply
  9. nicorellius

    Thanks for the resource… I think this tutorial is great. I’m running Python 3 and I had to install the uwsgi plugin:

    sudo apt-get install uwsgi-plugin-python3

    Best,
    Nick

    Reply
  10. jingles

    Hey — this saved my ass after hours of looking around. The problem was apparently ALLOWED_HOSTS ( I didn’t now this had been added to django since I had last used it) but in general your description and explanation of the variables involved was a huge help! Thank you!

    Reply

Leave a Reply to php tutorials Cancel reply

Your email address will not be published. Required fields are marked *