en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com

How to Install Django on CentOS 7

Django is a robust Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development. Using a full-featured framework like Django allows you to get your applications and sites up and running quickly without having to worry about the standard structural code to tie it together.

Centos Tutorials Sep 07, 16 by admin 10 min Read
How to Install Django on CentOS 7

It is open-source software that lets you focus on the individual portions of the applications while the in-built tools will take care of the heavy lifting. Basically, Django’s primary goal is to ease the creation of complex applications and takes care of the internal structures.

In this tutorial, we will go through various method of installing Django on CentOS 7 server. Throughout this guide, we will cover four main ways of installing Django.

Django installation methods

There are multiple ways to install and set up Django on your CentOS server. Each method will make a different type of configurations in your development environment with its own unique advantages. Here are the four ways that we will discuss in this blog post:

  1. Installation through PIP: The pip tool is a Python package manager. Once you install pip on your system, it’s super easy to install Django on the system level to be used by any user. This method usually always contain the latest stable version release.
  2. Installation through PIP in a Virtualenv: The Python virtualenv package allows you to create self-contained environments for various projects. Using this technology, you can install Django in a project directory without affecting the whole system. This type of installation will provide you with the most flexibility. 
  3. Global installation from packages: You can install Django easily from the EPEL repository. This is very simple but might not be as flexible as other methods. One more thing to note is that the repository might not contain the most recent version of Django. 
  4. Installation from git: If you wish to install the latest development version instead of the stable release, you will have to acquire the code from the git repository. Development versions do not have the same stability, but you can get the latest features and fixes. 

Now let’s get to the installation process.

Prerequisites

Before we get started, you will need the following:

  • A CentOS 7 server (VPS)
  • A basic understanding of Linux commands 
  • A root user or a user with Sudo privileges

Installing the EPEL repository

For all the mentioned installation types, we will need to install the EPEL repository beforehand. The EPEL repository contains extra packages that are not maintained as part of the core distribution. It is effortless to install the EPEL repository. Enter the following command:

sudo yum install epel-release

Once the installation is completed, update the system. We also recommend rebooting the system to have all the configurations in place.

sudo yum update -y && sudo reboot

After the reboot is finished, log in with the same user.

1.   Installation through PIP

When installing Django using pip, we will first install the pip package manager on the system. You can directly install the pip package manager from the EPEL repository by entering the following command:

sudo yum install python-devel python-setuptools python-pip

sudo pip install --upgrade pip

Now that pip is installed on the system, enter the following command to install Django globally:

sudo pip install Django

That’s all! You can verify the installation by entering the following command:

django-admin --version

You will see an output with the Django version that was installed.

2.   Installation through pip in a virtualenv

We believe that the most flexible way to install Django is through the virtualenv tool. With this tool, you can create virtual Python environments where you can install Python packages without affecting the rest of the system. This will allow you to select different Python packages on a project basis which will not pose any conflicts with other projects running on the system.

First, we will install pip from the EPEL repository:

 sudo yum install python-devel python-setuptools python-pip

sudo pip install --upgrade pip

Now, we will install the virtualenv package:

sudo pip install virtualenv

Now, whenever you start a new project, you can create a virtual environment for it. Let’s start this by creating a new directory and entering it (Replace newproject with the name of your project).  

mkdir ~/newproject

cd ~/newproject

To create a virtual environment within the project, enter the following:

virtualenv testenv

This will install a standalone version of Python and pip into an isolated directory structure within your project directory. We chose to call our virtual environment testenv, but you should name it something descriptive.

To install packages into the isolated environment, we must activate it first. To do that, enter the following:

source testenv/bin/activate

Once you enter this command, your shell prompt will change to something like this: (testenv)username@hostname:~/newproject$

Now we can go ahead and install Django. Here you won’t need to use Sudo since you will be installing it locally:

pip install django

To verify the installation, enter the following:

django-admin --version

You will see an output with the Django version that was installed.

If you would like to exit your virtual environment, enter the command from anywhere in the system.

deactivate

If you exited successfully, your prompt should revert to the conventional display. When you wish to work on your project, re-activate the virtual environment by moving into the project directory.

cd ~/newproject

source testenv/bin/activate

3.   Global installation from packages

To install Django from the local repository is straightforward. You can use the yum command to download and install the relevant packages:

sudo yum install python-django

Once the installation is completed, you can test if everything was set successfully by entering the following command:

django-admin --version

You will see an output with the Django version that was installed.

4.   Installation through git

If you are looking to install the development version of Django, you can download it directly from its git repository. To do this, you will need to install git on your system. Once git is installed, we can install the pip Python package manager to handle the installation of Django once it has been downloaded.

 sudo yum install git python-pip

Once you have git on your system, you can clone the Django repository. This repository will have more up-to-date software with a multitude of features but probably with the expense of stability. You can clone the repository to a directory called django-dev within your home directory by typing:

git clone git://github.com/django/django ~/django-dev

Once the repository is cloned, let’s go ahead and install it using the pip command. We will use the –e option to install it in editable mode.

sudo pip install -e ~/django-dev

Once the installation is completed, enter the following command to check the version of Django installed on the system.

django-admin --version

You will see an output with the Django dev version that was installed.

Creating a sample Django project

Now that we covered four different ways of installing Django on your system let’s go ahead and create a sample project.

We will create a directory called tst1 in the working directory and store all the necessary files within.

django-admin startproject tst1

cd tst1

Now run the following commands. First, to bootstrap the database on more recent versions of Django, enter this command:

python manage.py migrate

Now you will need to create an administrative user. This will be done manually using the following command:

python manage.py createsuperuser

You will be prompted to enter a username, an email address and password for the user. Once the user is set in place, we can start a Django development server to see what a new Django project looks like in the browser.

Only use this for development purposes:

python manage.py runserver 0.0.0.0:8000

Since we’re specifying port 8000, we will need to allow access to it from the firewall. To do this, enter the following command and restart the firewall to make the changes permanent.

sudo firewall-cmd --zone=public --permanent --add-port=8000/tcp

sudo firewall-cmd –reload

Now go to your browser and enter your server IP address as shown below:

server_ip_address:8000

If you something like this, congratulations! You have successfully created a Django project.

Now we can go ahead and view the admin side of things by making a small change to the URL as shown below:

server_ip_address:8000/admin

If you enter the admin username and password that you just created, you should be taken to the site’s admin section. Once you finish working on the site, go to your terminal and press CTRL + C to quit the application.

The Django project you’ve created provides the structural basis for designing a complete site. Check out the Django documentation for more information about how to build your applications and customize your site.

Conclusion

Now, after going through the tutorial, you should be able to install and configure Django on CentOS 7 server successfully. Django provides the main tools needed to create and manage powerful web applications. We also covered the procedure to create a sample project and launch the developer server from the web browser.

With proper integration of the complete web framework of Django, it’s possible to make development faster and put more focus on the unique aspects of your applications.

author img

admin

Generic placeholder image
Estrella

2018 Feb 14, 11:02:48

Hi there, I enjoy reading through your post. I wanted to write a little comment to support you.

Leave A Comment