Deploy Django Applications Using uWSGI and Nginx on Ubuntu 14.04
Updated by Sergey Pariev Contributed by Sergey Pariev
DeprecatedThis guide has been deprecated and is no longer being maintained.
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. This guide provides an introduction to deploying Django applications using uWSGI and nginx on Ubuntu 14.04.
Before You Begin
Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.
This guide will use an example account named
django
. Complete the sections of our Securing Your Server guide to create thedjango
user, harden SSH access and remove unnecessary network services. You may need to create additional firewall rules for your specific application.Update your system:
sudo apt-get update && sudo apt-get upgrade
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.
Install nginx, Python Tools and uWSGI
Install the system packages required for nginx, the SQLite Python bindings, and managing Python Tools:
sudo apt-get install build-essential nginx python-dev python-pip python-sqlite sqlite
Note
If your application uses another database, skip installingpython-sqlite
andsqlite
.Install virtualenv and virtualenvwrapper:
sudo pip install virtualenv virtualenvwrapper
virtualenv
andvirtualenvwrapper
are tools to create isolated Python environments. They help better manage application dependencies, versions and permissions. Forvirtualenvwrapper
to function correctly, run the following commands:echo "export WORKON_HOME=~/Env" >> ~/.bashrc echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
Activate
virtualenvwrapper
in the current session:source ~/.bashrc
Install uWSGI using
pip
:sudo pip install uwsgi
Set up a Sample Django Application
Be sure that you’re in the
django
user’s home directory and create the virtual environment for the application:cd /home/django && mkvirtualenv sample
After executing this command your prompt will change to something like
(sample)django@example.com:~$
indicating that you are using the sample virtual environment. To quit the virtual environment, enterdeactivate
.Install the Django framework:
pip install Django
Create the new Django application sample, located at
/home/django/sample
:django-admin.py startproject sample
Switch to the Django application’s directory and initialize SQLite database:
cd ~/sample && ./manage.py migrate
When running Django with nginx, it’s necessary to configure Django to put all static assets in your application’s
static
folder. Specify its location insettings.py
:echo 'STATIC_ROOT = os.path.join(BASE_DIR, "static/")' >> sample/settings.py
Run the following command to move all static assets into the directory mentioned above:
./manage.py collectstatic
Start a development server to test the sample application:
./manage.py runserver 0.0.0.0:8080
Visit
http://example.com:8080
in your browser to confirm that the sample application is set up correctly and working. You should see the Django test page:Then stop development server with Ctrl-C.
Configure uWSGI
Create a directory with uWSGI configuration files:
sudo mkdir -p /etc/uwsgi/sites
Create configuration file
sample.ini
with the following contents:- /etc/uwsgi/sites/sample.ini
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[uwsgi] 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
Create an Upstart job for uWSGI:
- /etc/init/uwsgi.conf
-
1 2 3 4 5 6 7 8 9
description "uWSGI" start on runlevel [2345] stop on runlevel [06] respawn 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
This job will start uWSGI in Emperor mode, meaning that it will monitor
/etc/uwsgi/sites
directory and will spawn instances (vassals) for each configuration file it finds. Whenever a config file is changed, the emperor will automatically restart its vassals.Start the
uwsgi
service:sudo service uwsgi start
Configure nginx
Remove the default nginx site configuration:
sudo rm /etc/nginx/sites-enabled/default
Create an nginx site configuration file for your Django application:
- /etc/nginx/sites-available/sample
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14
server { listen 80; server_name example.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; } }
Create a symlink to nginx’s
sites-enabled
directory to enable your site configuration file:sudo ln -s /etc/nginx/sites-available/sample /etc/nginx/sites-enabled
Check nginx’s configuration and restart it:
sudo service nginx configtest && sudo service nginx restart
You should now be able to reach your Django application by visiting your Linode’s hostname or IP address on port 80 in your browser.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
- Writing your first Django app Tutorial
- virtualenvwrapper Documentation
- WSGI/Python Quickstart Guide
- nginx Configuration
Join our Community
Find answers, ask questions, and help others.
This guide is published under a CC BY-ND 4.0 license.