20.8 C
Guru Unix /Linux and GNU supporter

How to install Django Framework on CentOS 7?

In a world where the premise is that time is money, you need tools that allow you to develop applications or faster. The Frameworks exist to save you from having to reinvent the wheel and help you ease the burden when you build a site.

Django is an open source web development framework, written in Python, that respects the design pattern known as Model-view-template. Developed by experienced programmers, Django handles much of the complexity of web development, so you can concentrate on writing your application without reinventing the wheel.

Django is now a thriving open source collaborative project, with thousands of users and contributors. While it still has some features that reflect its origin, Django has evolved into a versatile framework that is capable of developing any type of website.

Django: a versatile framework

Apart from the main features that should be used in almost all web applications: URL mapping, views, templates and templates, Django also provides:

  • Forms: Django simplifies the creation, validation and processing of forms.
  • User authentication and permissions: Django includes a robust authentication and permissions system
  • Data Serialization: Django makes it easy to serialize and serve your data as XML or JSON.
  • An extensible template system based on labels, with template inheritance.
  • Internationalization support, including built-in translations of the administration interface.
- Advertisement -

And of course many more things.

Let’s go for it. Installing Apache web server

We will see how to install Django in CentOS, and we have several ways to do it, we will use pip for it because it guarantees us flexibility and having the latest stable version of the framework.

For this tutorial we must have root access. To have it we must write:

          sudo -i

1.- Root Access
1.- Root Access

Obviously if we are going to install Django, it is because we must have a web server running, together with the database manager.

Installing the apache web server can be done with the command:

           yum install httpd

2.- Installing httpd
2.- Installing httpd

Once installed, we must initialize the service and enable it so that when starting the system, it also initializes:

          systemctl start httpd.service

          systemctl enabled httpd.service

4.- Starting httpd
4.- Starting httpd

It is a good idea to go to your web browser and verify that the web server is running correctly:


3.- Checking web server
3.- Checking web server

If we see the image above, it means that the web server is running and there are no problems so far, that is, we can continue.

Installing MariaDB

MariaDB is one of the most reliable and secure open source database managers available. It stands out for its robustness, ease of use and extensive existing documentation. We must install it:

          yum install mariadb mariadb-server

5.- Installing mariadb
5.- Installing mariadb

And then, we must start the service and enable it to run at system startup.

           systemctl start mariadb.service

           systemctl enable mariadb.service

6.- Starting mariadb
6.- Starting mariadb

Then we’ll invoke the mysql script to quickly configure mariadb. We’ll answer the questions like this: Y,Y,N,Y,Y.


7.- Configuring mariadb
7.- Configuring mariadb

And now we log in to mariadb to execute a statement and test the installation we just did.

            mysql -u root -p

After entering the root password, we will be in the mariadb console. We’ll write on it:

           SHOW DATABASES;

8.- Showing databases
8.- Showing databases

By showing us the created databases we confirm the success of the process.

It’s Django’s turn

CentOS does not provide, by default, some packages we need, so we will add the EPEL repository. EPEL is open source and free community based repository project from Fedora team which provides 100% high quality add-on software packages for CentOS, Red Hat Linux and others:

           wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

And then:

          rpm -ivh epel-release-latest-7.noarch.rpm

9.- Adding EPEL repository
9.- Adding EPEL repository

We check if the repository was added correctly:

           yum repolist

10.- Showing repositories list
10.- Showing repositories list

As we see in the image, the EPEL repository was added and is part of the system’s software sources.

Now we proceed to install python-pip that will allow us to install Django.

           yum install python-pip

11.- Installing python-pip
11.- Installing python-pip

Now we can install Django with the command:

            pip install django

That command will install pip, but we need to update it to be compatible with the updated version of Django. That’s why we’re writing:

            python -m pip install –upgrade pip setuptools

Upgrading PIP
12.- Upgrading PIP

And now, yes, we proceed to install it:

          python -m pip install django

13.- Installing Django
13.- Installing Django

We check which version is installed:

          django-admin –version

12.- Cheking the version installed
12.- Cheking the version installed

The installed version is LTS which guarantees stability and security updates for a long time.

Creating a simple project in Django

To create a simple Django project we can do it with the following command:

           django-admin startproject test1

Crating a project
13.-Creating a project

And now we access the folder:

          cd test1

Accessing to project folder
14.- Accessing to project folder

Then we start the database configuration instructions with the following command:

           python manage.py migrate

15.- Executing migrations
15.- Executing migrations

The next step is to create an administrative user to manage the project:

          python manage.py createsuperuser

You will ask us for information such as username, email and password (it should not be too short):

16.- Creating root user
16.- Creating root user

Now we must modify the options file to define the hosts allowed to manage the project.

          cd test1

          nano manage.py

And in it we must modify the host by the ip address of our computer. Once modified, press crt + x and say yes

18.- Modifying settings
17.- Modifying settings

17.- Modifing settings
18.- Modifing settings

Now head back to the parent directory with the command below:

           cd ../

And we run the framework as such. Don’t forget to replace the ip I put in with yours.:

         python manage.py runserver

20 .- Running the server
19 .- Running the server

Now with the server running, we proceed to go to our web browser and place the ip of the web server and the port selected for Django.

20.-Running the project
20.-Running the project

Unlocking the port on CentOS

It is possible that even if you do these steps, you may still get an error when accessing from your browser. The port probably needs to be opened in the CentOS firewall.

To do this, we must enter the following commands:

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

           firewall-cmd –reload

Django is a powerful framework for web technologies, flexible, free and with a large community behind its development, intended for perfectionist web developers.

Its installation is not complex but it requires several steps for its operation. All that’s left now is to start coding.

Please share this article through your social networks.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"


Please enter your comment!
Please enter your name here

Latest article