Deploy Django Application with PostgreSQL, Nginx, Gunicorn, Virtualenv and Supervisor
Deploy Django Application project for Production Environment
Deploy Django Application on Linux Machine
Django is a powerful python web framework build for rapid development. It takes care of much of problems of web development, so you can only focus on writing your app rather than reinventing the wheel. The most interesting thing about Django is that it is free and open source.
So lets begin with the deployment procedure for any Django based application. The recommended configuration for running a Django application is a bit complex. It includes tools like: PostgreSQL, Nginx, Gunicorn, Virtualenv, Supervisord. This are advanced tools that makes the Django application more efficient.
There are some prerequisites for this tutorial, you must be ready with:
- A Debian based Linux machine with sudo privileges.
- A domain name server that will point to your server’s IP.
Lets start the deployment process.
1. First start with the updating packages that are already available on server. So that server will be up to date. For this:
2. Now, install postgreSQL and its required package.
Lets create a database user and database for our Django application. For this first login as a postgres user, as by default only this user has privileges to create database in postgreSQL.
Now, you are logged in as postgres user. lets create database user and assign necessary privileges to it.
In above example, database user name is db_user. You can give the name as per your wish. Create database now. Give a appropraite name to database as well, as per your Django application.
here db_user is a databse user and django_db is a database name. Now logout from postgres user.
3. Till now our database setup is done. Now create a virtual environment for our Django application. For this we need to install, virtualenv package.
Now create a virtual environment.
Note: I am going to keep my django application in default folder that is /home/ubuntu/, So i will create my virtual environment here. If you want to keep your application in some other directory like /var/www/ then change your directory accordingly.
Here djano_env is the virtual environment name. Change it as per you, if required. To activate this environment, run
Now, install all the requirements of your project here, including django. Like:
4. Now, clone your project directory, if you have already created project. Here I am creating a sample project for demo.
Here project name is sample_project. Now check whether all your project requirement is setup or not by running it on development server. For this first navigate into your project and run the server.
Our project is running on Server’s public IP with port 8000. Now quit the server by pressing CONTROL-C.
5. Now, we need to configure postgreSQL so that it can communicate with our Django application. For this, install psycopg2 database adapter. But this adapter have some package dependencies, so first install them.
Now, configure database section in your project’s settings.py file.
Also add, your server’s IP or domain name in settings.py as follow:
Without above setting, Django will not be accessible from your domain name or IP address. This is the security feature of Django.
Now, synchronise the database
Now our database setup is fully done.
6. Now we will setup our app server that is Gunicorn rather than using Django’s by default single threaded development server.
Now you have gunicorn working, test it by running
Now, you can access gunicorn from your server’s public IP with port 8001. Now to make gunicorn more useful for our django application, we need to configure its option. For this make a bash script called gunicorn.bash. You can change file name as per your choice.
now add following configurations into file
Here, all the variables are well explained in their comment part respectively. Change the variables value according to your configuration. Like NAME variable will define how you application will be identified in programs like ps, top etc. The recommended way to define no of workers is equals to 2*CPUs+1. For example, for a single CPU machine should be set with 3 workers.
Now make this script executable.
Lets test this script by running it.
7. Now, its time to setup supervisor so that it can supervise our application. If system reboots or application quits unexpectedly, supervisor will take care of its restart. For this, first install it.
To supervise any program through supervisor, you need to create configuration file for that program inside /etc/supervisor/conf.d/ directory. For our Django application that is sample_project, we will create sample_project.conf
Now, write following content into the opened file.
Change the above configuration value according to your setup. As we mentioned in above file that the logs will be stored at /home/ubuntu/logs/gunicorn_supervisor.log, we need to make this directory and file.
After this done, we will ask supervisor to reread configuration files and update it so the our newly configuration file get add.
For Ubuntu 14.04:
As you can see, our sample_project configuration file get added to supervisor process group. Now, start our app through it. For this
For Ubuntu 16.04:
To check status:
Now, our application will get automatically restart after system gets boot up or our application gets crashed.
8. Now, we need to setup last thing that is Nginx. So lets get start. Nginx will act as a server for our application. So first install it.
Now, we will need to create configuration file for our application inside /etc/nginx/sites-available/ directory. After this we will need to create symbolic link for it, in the /etc/nginx/sites-enabled directory. Lets do it one by one. First create configuration file.
Now, put following content into the opened file.
Change the configuration value according to your setup. Now create the symbolic link for it.
Now, start the Nginx.
Now, if you browse your server’s domain name into browser, you will get your site running. Congrats, your production ready django application is set. 🙂