Django Application Deployment

29 / Sep / 2016 by Parag Gupta 0 comments

Django is a high-level Python Web framework that can help you get your Python application or website off the ground. It includes simple development server for testing the code locally, but in the production environment, a more secure and powerful web server is required.

uWSGI is an application server container that aims to provide a full stack for developing and deploying web applications and services. It communicates with a variety of other protocols with the application using the methods defined by the WSGI spec. It is the piece that translates requests from a web server into a format that the application can understand.

virtualenv  is a tool to create isolated Python environments. It creates a folder which contains all the necessary executables to use the packages that a Python project would need. It keeps the dependencies required by different projects in separate places, by creating virtual Python environments for them.

Use Case

Deploying Django application and configuring the uWSGI application container server to interface with our application and setting up Nginx to reverse proxy to uWSGI in order to achieve greater security and performance features to serve our apps. We will be installing our Django application in its own virtual environment in order to isolate the requirements for the project.

Prerequisites

An Ubuntu 14.04 server instance with a non-root user with Sudo privileges configured.

Install and configure virtualenv & Django application

1.We will be installing virtualenv using pip, the Python package manager:

sudo apt-get update
sudo apt-get install python-pip
sudo pip install virtualenv

2. We will be creating a folder that will contain our virtual environment and virtualenv command is used to create a virtual environment:

mkdir sample_virtual_env
cd sample_virtual_env
virtualenv myapp_virtual_env

3. Once the environment is created then it is activated. With this environment active, any software installed through pip will now be installed into the virtual environment. This allows us to isolate our packages on a per-project basis:

source myapp_virtual_env/bin/activate

4. Now we will install Django using pip as we are installing it locally in our virtual environment:

pip install django

5. Now we will create our first sample project. The below-mentioned command will create a directory called sample_django_app within our home directory. It contains a management script for handling various aspects of the project and another directory with the same name used to contain the actual project code:

django-admin.py startproject sample_django_app

6. We will be setting up minimum requirements for our project  i.e migrating the database to initialize the SQLite database that our project will use. We can set up an alternative database for our application also:

cd sample_django_app
./manage.py migrate

7. We should now have a database file called db.sqlite3 in our project directory. Now, we will create an administrative user. We have to provide a username, email-address and password:

./manage.py createsuperuser

8. Since we will be setting up Nginx to serve our site, we need to configure a directory which will contain our site’s static content. This will allow Nginx to serve these directly, which will improve the performance. We will tell Django to place these into a directory called static in our project’s base directory. Add the line to the bottom of the settings.py file to configure this behavior:

vi sample_django_app/settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

9. Now, we have to collect our site’s static elements and place them within that directory. We have to type “yes” to confirm the action and collect the static content. There will be a new directory called static in our project directory:

./manage.py collectstatic

10. We can test our project by starting the development server. This will start up the development server on port 8726:

./manage.py runserver 0.0.0.0:8726

11.Visiting the browser domain name or IP address followed by the respective port will show a page like this:
sample_site
Adding /admin to our browser address bar will take us to the admin login page: admin_login

Using the login credentials that we created with createsuperuser command will take us to the admin login page:
admin_interface
12.We can  now deactivate our virtual environment as we have completed our Django portion:

deactivate

Install and configure uWSGI Application Server

1.In order to install uWSGi, we need Python development files on which software relies on and then we will install uWSGI:

sudo apt-get install python-dev
sudo pip install uwsgi

2. We can test our application through uWSGI. Successful execution of the below command requires that we should be running it inside our project directory i.e sample_django_app:

uwsgi --socket 0.0.0.0:8123 --protocol=http --venv /home/ubuntu/sample_virtual_env/myapp_virtual_env -w sample_django_app.wsgi

3. We will be running uWSGI in Emperor mode, which will allow a master process to manage separate applications automatically given a set of configuration files. So we will be creating a directory to hold our configuration file and then we will place our configuration file inside the created directory:

sudo mkdir -p /etc/uwsgi/project
cd /etc/uwsgi/project

4. Now, we have to create our configuration file:

sudo vi sample_django_app.ini
[uwsgi]
project = sample_django_app
base = /home/ubuntu

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

master = true
processes = 5

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

5. We will now create an Upstart script to automatically start uWSGI at boot:

sudo vi /etc/init/uwsgi.conf
description "uWSGI application server in Emperor mode"

start on runlevel [2345]
stop on runlevel [!2345]

setuid ubuntu
setgid www-data

exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/project

 

Install and configure Nginx as Reverse Proxy

1. First of all, we will be installing Nginx and then we will be making changes to default server configuration file:

sudo apt-get install nginx
sudo vi /etc/nginx/sites-enabled/default

2. In the server block add the following lines:

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/sample_django_app;
}

In the default location block add the following line:

include uwsgi_params;
uwsgi_pass unix:/home/ubuntu/sample_django_app/sample_django_app.sock;

Check Nginx configuration file and restart Nginx and uWSGI application server:

sudo nginx -t
sudo service nginx restart
sudo service uwsgi start

Now, we can test our application by going to the domain name or public IP address. We set up Nginx to act as a reverse proxy to handle client connections and serve the  Django application running on uWSGI application server.

FOUND THIS USEFUL? SHARE IT

Leave a comment -