Deploy Django with Virtualenv on CentOS 7

By | October 29, 2016


Django is a Python web framework that allows for rapid development of primarily database-driven web applications. Virtual Environments, or virtualenv, is a Python package that allows development within isolated Python environments. Installing or updating Python packages within one environment will have no impact on other environments. Combining Django and virtualenv provides a solid environment for both development and production deployment.


  • CentOS 7 or RHEL 7

Install Virtualenv

Many distributions include virtualenv and virtualenvwrapper as part of the default distribution repository. However, these version can become outdated and it is usually recommended installing from a repository containing the latest release.

One common repository added to CentOS and Red Hat Enterprise Linux (RHEL) servers is the Extra Packages for Enterprise Linux (EPEL) repository. The following command will add the EPEL repository to CentOS 7 or RHEL 7.

sudo rpm -Uvh

Now virtualenv, and dependencies, can be installed using the YUM package manager.

sudo yum -y install python-virtualenv

Install Virtualenvwrapper

The virtualenvwrapper package is a popular extension to virtualenv that increases usability; particularly when managing multiple virtual environments. While completely optional, it is recommend virtualenvwrapper be installed as this tutorial makes use of virtualenvwrapper features.

sudo yum -y install python-virtualenvwrapper

Configure Virtual Environment

The first step is to define the location of your virtual environments and load virtualenvwrapper.

export WORKON_HOME=~/.virtualenvs
source /usr/bin/

The ~/.virtualenvs directory is where the above virtual environments will reside. This directory path was chosen because the EPEL python-virtualenvwrapper package will set this path globally for all users in /etc/profile.d/

Your first virtual environment can now be created.

mkvirtualenv myapp

The shell prompt will change to reflect that the myapp virtual environment is loaded. You can leave the virtual environment at any time with the following command.


The prompt will change to reflect the virtual environment is no longer loaded. The following will return to, or “activate”, the virtual environment that was previously created.

workon myapp

Other useful virtualenvwrapper commands include:

  • lsvirtualenv – List available virtual environments.
  • cdvirtualenv – Change your current directory into the activated virtual environment.
  • rmvirtualenv – Remove a virtual environment.

The Python pip package manager should be installed before using the virtual environment.

sudo yum -y install python-pip

Install Django

Before you install Django, be sure to activate the new virtual environment if not already activated.

workon myapp

Now Django can be installed.

pip install django

The Django package files will be installed under ~/.virtualenvs/myapp.

Any further required Python packages should be installed in the same manner while the virtual environment is activated.

workon myapp
pip install <package_name>

Create Django Project

You are ready to create a Django project. startproject myproject

This will create a ./myproject directory containing several projects directories and files. Navigate into the project directory.

cd myproject

The ./myproject/ file can be modified and a preferred database backend configured. Other settings can be modified as necessary. If no database backend is specified, then Django will use the default sqlite3 backend.

You can populate with database with some default Django tables.

python migrate

The project can now be verified by launching the Django web service.

python runserver

The service should now be accessible at http://localhost:8000/. This can be verified with curl.

curl http://localhost:8000/

The default IP address and port can easily be changed on launch. Simply add the following parameter to bind to all available IP addresses and port 8080.

python runserver

Note: The built-in Django web server is NOT meant to be used in a production environment. It is a tool for development only. Once the application is ready to be deployed into production, consider using a web server such as Apache or Nginx with uWSGI.

Create Django Application

Once the project is created and operation, it is time to create an application. An application can be created in the same directory as

python startapp myapp

This will create a ./myapp directory with base files ready for your code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.