Tag Archives: ubuntu

nginx and django

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