Did You Know?

How Can We Help?

Special Offer

Deploy your Cloud Server now and get $100 in Free Credits!

Get Started

Deploy Django Applications Using Nginx and uWSGI on Ubuntu 14.04

Table of Content

Django is a Python Web Platform of high standards that promotes fast development and clean, pragmatic design. A description on using uWSGI and nginx on Ubuntu 14.04 is given in this document.

Before You Begin

1. Get acquainted with the starting guide and finalize the steps for determining the hostname and timezone of your cloud server.

2. This guide uses an example of django. Complete securing your Server Guide sections to create a django account, improve access to SSH and disable unwanted network services. With your particular program, you need to build additional firewall rules.

3. update your system: upgrade your system

[command]sudo apt-get update && sudo apt-get upgrade[/command]

Install nginx, Python Tools and uWSGI

1. Install the required framework packages for SQLite Python and Python Tools: SQLite Python bindings

[command]sudo apt-get install build-essential nginx python-dev python-pip python-sqlite sqlite[/command]

2. Now Install virtualenv and virtualenvwrapper:

[command]sudo pip install virtualenv virtualenvwrapper[/command]

Virtualenv and virtualenvwrapper are instruments for building Python environment in isolated environments. These help handle dependencies, versions and permissions easier. To function correctly with virtualenvwrapper, execute the following commands:

[command]echo "export WORKON_HOME=~/Env" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc[/command]

3. Enable virtualenvwrapper in the current session:

[command]source ~/.bashrc[/command]

4. Using pip to install uWSGI:

[command]sudo pip install uwsgi[/command]

Set up a Sample Django Application

1. Make sure you are in the home directory of Django users and build the application's virtual environment for the application :

[command]cd /home/django && mkvirtualenv sample[/command]

Once this command has been executed, your prompt will turn to something like [email protected] which means you are using the virtual setup sample. Type deactivate to exit the virtual environment.

2. Set up the framework for Django:

[command]pip install Django[/command]

3. Build a new preview of the Django app on /home/django/sample:

[command]django-admin.py startproject sample[/command]

4. Switch to the directory of the Django application and launch the database of SQLite:

[command]cd ~/sample && ./manage.py migrate[/command]

5. It is essential to configure Django when running Django with Nginx to place all the static assets in the static folder of your application in settings.py , define your location:

[command]echo 'STATIC_ROOT = os.path.join(BASE_DIR, "static/")' >> sample/settings.py[/command]

6. To transfer all static assets in the above directory, run the following command:

[command]./manage.py collectstatic[/command]

7. To check the sample code, open a development server:

[command]./manage.py runserver[/command]

Visit http:/abc.com:8080 on your computer browser to check the right setup and functionality of the sample program. See the check page for Django:

8. If you will get the message "It Worked,Congratulations on your first Django-powered page"

Then stop the development server with Ctrl-C.

Configure uWSGI

1. Create an uWSGI file settings directory:

[command]sudo mkdir -p /etc/uwsgi/sites[/command]

2. Create the sample.ini configuration file with the contents below:

[filecode file="/etc/nginx/conf.d/abc.com.conf"]
project = sample
base = /home/django

chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 2

socket = %(base)/%(project)/%(project).sock
chmod-socket = 664
vacuum = true

3. Create an uWSGI upstart job:

[filecode file="/etc/init/uwsgi.conf"]
description "uWSGI"
start on runlevel [2345]
stop on runlevel [06]

env UWSGI=/usr/local/bin/uwsgi
env LOGTO=/var/log/uwsgi.log

exec $UWSGI --master --emperor /etc/uwsgi/sites --die-on-term --uid django --gid www-data --logto $LOGTO

In Emperor mode, this job will start uWSGI to track the directory of /etc/uwsgi/sites  and to create instances (vasals) for any configuration file it finds. The Emperor will immediately restart the vassals if a config file is updated.

4. Start the service uwsgi:

[command]sudo service uwsgi start[/command]

Configure nginx

1. Delete the default nginx site configuration:

[command]sudo rm /etc/nginx/sites-enabled/default[/command]

2. Creates your Django application's nginx site configuration file:

[filecode file="/etc/nginx/sites-available/sample"]
server {
listen 80;
server_name abc.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/django/sample;

location / {
    include         uwsgi_params;
    uwsgi_pass      unix:/home/django/sample/sample.sock;


3. To allow your site configuration file, set up a symlink to the nginx sites-enabled directory:

[command]sudo ln -s /etc/nginx/sites-available/sample /etc/nginx/sites-enabled[/command]

4. Check and restart nginx settings:

[command]sudo service nginx configtest
sudo service nginx restart[/command]

5. By now visiting a hostname or IP address on port 80 you should be able to access your Django program.

Special Offer

Deploy your Cloud Server now and get $100 in Free Credits!

Get Started
Table of Contents