Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Deploying django application with gunicorn nginx mysql

Step One: Update Packages

sudo apt-get update
sudo apt-get upgrade

Step Two: Install and Create Virtualenv

sudo apt-get install python-virtualenv
sudo virtualenv /opt/myenv

Step Three: Install Django

pip install django

Step Four: Install MySQL

sudo apt-get install build-essential git mysql-server mysql-client libmysqlclient-dev python-dev

Step Five: Install NGINX

sudo apt-get install nginx

Step Six: Install Gunicorn

source /opt/myenv/bin/activate
pip install gunicorn

Step Seven: Configure MySQL

Step Eight: Clone the Django Project

cd /opt/myenv
source /opt/myenv/bin/activate
git clone git-url myproject
cd /opt/myenv/myproject/myproject
Edit the file with your editor of choice:
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myproject',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
cd /opt/myenv/myproject
pip install -r requirements.txt
python syncdb
python collectstatic

Step Nine: Configure Gunicorn

gunicorn_django --bind
cd /opt/myenv
sudo nano
Add the following contents to the file:
command = '/opt/myenv/bin/gunicorn'
pythonpath = '/opt/myenv/myproject'
bind = ''
user = 'nobody'
/opt/myenv/bin/gunicorn -c /opt/myenv/ myproject.wsgi

Step Ten: Configure NGINX

sudo service nginx start
sudo nano /etc/nginx/sites-available/myproject
Add following lines in nginx Configure
server {

    access_log off;

    location /static/ {
        alias /opt/myenv/static/;

    location / {
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
nginx restart
cd /etc/nginx/sites-enabled
sudo ln -s ../sites-available/myproject
sudo rm default
sudo service nginx restart

This comment has been minimized.

Copy link

ghost commented Apr 15, 2016

Thanks. Will try it out on Digital Ocean. Is this suitable for a production environment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.