Deploying Django 1.7 with python3 using virtualenv in Ubuntu

This is a tutorial to deploy a django application created in django 1.7 and python 3 using virtualenv in Ubuntu server. It assumes that you have already setup initial server configuration in your server like creating a new user, setting up security measures, etc.

First of all, update your packages.

sudo apt-get update
sudo apt-get upgrade

Then, install python3 and python3-pip in your server (if they doesn’t exist already)
sudo apt-get install -y python3
sudo apt-get install -y python3-pip

Next, install virtualenv
sudo apt-get install python-virtualenv

Let’s assume that you want to install your application in ~/djangoapps/ directory. Then, create a python virtual environment in the directory as
virtualenv –no-site-packages –distribute -p /usr/bin/python3 ~/djangoapps/myappname

Now, let’s get inside that virtual environment

source ~/djangoapps/myappname/bin/activate

Install django and any other requirements for your applications inside the environment

pip3 install django

Next step is to install gunicorn server. You can install gunicorn as

pip3 install gunicorn

That’s all for now. Deactivate the virtual environment as

deactivate

Next step is to install nginx server.

sudo apt-get install nginx

Go to ~/djangoapps/myappname/ and create a file rungunicorn.sh . Replace the project name and path in the code below and copy the content to that file.

#!/bin/bash
NAME="yourappname" # Name of the application
DJANGODIR=/path/to/your/app # Django project directory

# bind gunicorn to this port. Make sure that you use different
# port for different django projects
BINDIP=127.0.0.1:8214
USER=root # the user to run as
GROUP=root # the group to run as
NUM_WORKERS=9 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=yourappname.settings # which settings file should Django use
DJANGO_WSGI_MODULE=yourappname.wsgi # WSGI module name
 
echo "Starting $NAME"
 
cd $DJANGODIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=$BINDIP \
--log-file=-

Now, finally we have to configure nginx to serve static files and django website. go to /etc/nginx/sites-available/ and create a file named mysite. Place the following inside that file

    server {
        server_name yourdomainorip.com;

        access_log off;

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

        location / {
                proxy_pass http://127.0.0.1:8214;
                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"';
        }
    }

Make sure to change the server_name to your domain name and the port number to the port number you specified earlier.

After this, go to /etc/nginx/sites-enabled and create a link to the previous file using this command:

sudo ln -s ../sites-available/mysite

Now, go to ~/djangoapps/myappname/ and start the startgunicorn.sh script as

./startgunicorn.sh

Test if the website is running from your browser. If it is working, cancel the gunicorn process using ctrl-c and type “bg” and hit enter. This should keep the gunicorn server running in background.

Leave a Reply

avatar
  Subscribe  
Notify of