How to Deploy a Full Django Stack on Ubuntu: From Packages to Production

This step‑by‑step guide shows how to set up a production‑ready Django environment on an Ubuntu VPS, covering system updates, Python and virtualenv installation, Django, database, caching, asynchronous tasks, Gunicorn, Nginx, and process supervision with Supervisor.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Deploy a Full Django Stack on Ubuntu: From Packages to Production

Prerequisites

Linux distribution: Ubuntu 12.04 (or similar). You will need root or sudo access and a VPS or cloud server (e.g., Linode, Alibaba Cloud).

1. Update System and Apply Patches

# Update package index
$ sudo apt-get update

# Upgrade all packages
$ sudo apt-get dist-upgrade

# Remove unnecessary packages
$ sudo apt-get autoremove

# Reboot if needed
$ sudo reboot

2. Install Python and Package Tools

$ sudo apt-get install build-essential python-dev

Install distribute and pip:

# Download distribute installer (use curl or wget)
$ curl -O http://python-distribute.org/distribute_setup.py

# Install distribute
$ sudo python distribute_setup.py

# Clean up installer
$ rm distribute*

# Install pip via distribute
$ sudo easy_install pip

3. Set Up Virtualenv

# Install virtualenv and virtualenvwrapper
$ sudo pip install virtualenv virtualenvwrapper

# Add virtualenvwrapper to shell startup
$ vim ~/.bashrc
# Append:
source /usr/local/bin/virtualenvwrapper.sh

# Reload shell or source the file
$ source ~/.bashrc

Create and use a virtual environment:

# Create a new environment (replace <VIRTUALENV_NAME> with your project name)
$ mkvirtualenv <VIRTUALENV_NAME>

# Deactivate the environment
$ deactivate

# Activate an existing environment
$ workon <VIRTUALENV_NAME>

# List installed packages
$ pip freeze

4. Install Django

# Install the latest Django
$ pip install django

# Install docutils (required by Django admin)
$ pip install docutils

# Verify installation
$ django-admin.py startproject <APP_NAME>
$ cd <APP_NAME>
$ chmod +x manage.py
$ ./manage.py runserver 0.0.0.0:8000

5. Install Pillow (Image Support)

# System libraries needed for image processing
$ sudo apt-get install libjpeg8-dev libfreetype6-dev zlib1g-dev

# Install Pillow (or PIL)
$ pip install pillow   # or $ pip install pil

6. Install MySQL and Python Connector

# MySQL server and development headers
$ sudo apt-get install mysql-server libmysqlclient-dev

# Python MySQL driver
$ pip install MySQL-Python

7. Install South for Schema Migrations

$ pip install south
# Add to Django settings.py
INSTALLED_APPS = [
    ...
    'south',
    ...
]

8. Install Memcached and Python Client

# Install memcached and development files
$ sudo apt-get install memcached libmemcached-dev

# Install Python client (pylibmc recommended)
$ pip install pylibmc

# Add cache configuration to settings.py
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

9. Install RabbitMQ and Celery for Asynchronous Tasks

# RabbitMQ server
$ sudo apt-get install rabbitmq-server

# Create RabbitMQ user and vhost
$ sudo rabbitmqctl add_user <RABBIT_USER> <RABBIT_PASSWORD>
$ sudo rabbitmqctl add_vhost <RABBIT_VHOST>
$ sudo rabbitmqctl set_permissions -p <RABBIT_VHOST> <RABBIT_USER> ".*" ".*" ".*"

# Install Celery and Django integration
$ pip install django-celery

# Add to settings.py
INSTALLED_APPS = [
    ...
    'djcelery',
    ...
]
BROKER_URL = "amqp://<RABBIT_USER>:<RABBIT_PASSWORD>@localhost:5672/<RABBIT_VHOST>"
CELERY_RESULT_BACKEND = "database"
CELERY_RESULT_DBURI = "mysql://<DB_USER>:<DB_PASSWORD>@localhost/<DB_NAME>"
import djcelery
djcelery.setup_loader()

10. Install Gunicorn Web Server

$ pip install gunicorn
# Add to Django INSTALLED_APPS
INSTALLED_APPS = [
    ...
    'gunicorn',
    ...
]
# Run Gunicorn with 4 workers and gevent
$ ./manage.py run_gunicorn -w 4 -k gevent

11. Install Supervisor for Process Management

# Install Supervisor
$ sudo apt-get install supervisor

# Example program config (place in /etc/supervisor/conf.d/)
[program:celeryd]
command = /home/<USERNAME>/.virtualenvs/<VIRTUALENV_NAME>/bin/python /home/<USERNAME>/<APP_NAME>/manage.py celeryd -B -E
directory = /home/<USERNAME>/<APP_NAME>
user = <USERNAME>
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/celeryd.log
stderr_logfile = /var/log/supervisor/celeryd_err.log

# Similar configs for celerycam and gunicorn omitted for brevity

# Restart Supervisor to apply changes
$ sudo service supervisor restart
$ sudo supervisorctl restart all

12. Install Nginx and Configure Static/Media Files

# Create directories for static and media
sudo mkdir -p /var/www/static
sudo mkdir -p /var/www/media
sudo chown -R <USERNAME>:www-data /var/www

# Update Django settings.py
MEDIA_ROOT = '/var/www/media/'
MEDIA_URL = '/media/'
STATIC_ROOT = '/var/www/static/'
STATIC_URL = '/static/'

# Collect static files
$ ./manage.py collectstatic

# Install Nginx
$ sudo apt-get install nginx

# Remove default site and create a new config
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo touch /etc/nginx/sites-available/<YOUR_APP>
$ sudo ln -s ../sites-available/<YOUR_APP> /etc/nginx/sites-enabled/
$ sudo vim /etc/nginx/sites-available/<YOUR_APP>

# Example Nginx config
upstream gunicorn {
    server localhost:8000;
}
server {
    listen 80;
    server_name <YOUR_DOMAIN>.com www.<YOUR_DOMAIN>.com;
    root /var/www/;
    access_log /var/log/nginx/<YOUR_APP>.access.log;
    error_log /var/log/nginx/<YOUR_APP>.error.log;
    client_max_body_size 0;
    try_files $uri @gunicorn;
    location @gunicorn {
        proxy_pass http://gunicorn;
        proxy_redirect off;
        proxy_read_timeout 5m;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

# Restart Nginx
$ sudo service nginx restart

After completing these steps, your Django application should be fully operational on the VPS, served by Gunicorn behind Nginx, with MySQL, Memcached, Celery, and Supervisor handling persistence, caching, asynchronous tasks, and process supervision.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DeploymentLinuxDjangoUbuntuvirtualenvGunicorn
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.