How to Install Django on CentOS 7

By 

Updated on

6 min read

Install Django on CentOS 7

Django is a free and open-source high-level Python web framework designed to help developers build secure, scalable and maintainable web applications.

There are different methods to install Django, depending on your needs. It can be installed system-wide or in a Python virtual environment using pip. Django packages are also included in the CentOS repositories and can be installed using the yum package manager but they are outdated.

In this tutorial, we will be installing and configuring the latest stable version of Django on a CentOS 7 machine inside a Python virtual environment.

The main purpose of Python virtual environments is to create an isolated environment for different Python projects. This way you can have multiple different Django environments on a single computer and install a specific version of a module on a per project basis without worrying that it will affect your other Django installations. If you install Django into the global environment then you can install only one Django version on your computer.

Installing Django on CentOS 7

The following sections provide a step by step instructions about how to install Django in a Python virtual environment on CentOS 7.

1. Installing Python 3

We’ll be installing Python 3.6 from the Software Collections (SCL) repositories.

CentOS 7 ships with Python 2.7.5 which is a critical part of the CentOS base system. SCL will allow you to install newer versions of python 3.x alongside the default python v2.7.5 so that system tools such as yum will continue to work properly.

Start by enabling SCL by installing the CentOS SCL release file which is included in the CentOS extras repository:

Terminal
sudo yum install centos-release-scl

Once the repository is enabled install Python 3.6 with the following command:

Terminal
sudo yum install rh-python36

Once Python 3.6 is installed we are ready to create a virtual environment for our Django application.

2. Creating a Virtual Environment

Starting from Python 3.6, the recommended way to create a virtual environment is to use the venv module.

Navigate to the directory where you would like to store your Python 3 virtual environments. It can be your home directory or any other directory where your user has read and write permissions.

Create a new directory for your Django application and cd into it:

Terminal
mkdir my_django_app
cd my_django_app

To access Python 3.6 you need to launch a new shell instance using the scl tool:

Terminal
scl enable rh-python36 bash

Run the following command to create a new virtual environment:

Terminal
python3 -m venv venv

The command above creates a directory called venv, which contains a copy of the Python binary, the Pip package manager , the standard Python library and other supporting files. You can use any name you want for the virtual environment.

To start using this virtual environment, you need to activate it by running the activate script:

Terminal
source venv/bin/activate

Once activated, the virtual environment’s bin directory will be added at the beginning of the $PATH variable. Also your shell’s prompt will change and it will show the name of the virtual environment you’re currently using. In our case that is venv.

3. Installing Django

Now that the virtual environment is activated, you can use the Python package manager pip to install Django:

Terminal
pip install django
Tip
Within the virtual environment, you can use the command pip instead of pip3 and python instead of python3.

To verify the installation use the following command which will print the Django version:

Terminal
python -m django --version

At the time of writing this article, the latest official Django version is 2.1.2

output
2.1.2

Your Django version may differ from the version shown here.

4. Creating a Django Project

To create a new Django project named mydjangoapp use the django-admin command-line utility:

Terminal
django-admin startproject mydjangoapp

The command above will create a mydjangoapp directory in your current directory.

Terminal
tree  mydjangoapp/
output
mydjangoapp/
|-- manage.py
`-- mydjangoapp
    |-- __init__.py
    |-- settings.py
    |-- urls.py
    `-- wsgi.py

Inside that directory, you will find the main script for managing projects named manage.py and another directory including database configuration, and Django and application-specific settings.

Let’s migrate the database and create an administrative user.

Start by navigating to the mydjangoapp directory:

Terminal
cd mydjangoapp

By default, Django uses a SQLite database. For production applications, you can use PostgreSQL , MariaDB , Oracle or MySQL Database.

Run the following command to migrate the database:

Terminal
python manage.py migrate

The output will look something like the following:

output
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

Once the database is migrated, create an administrative user so that you can use the Django admin interface:

Terminal
python manage.py createsuperuser

The command will prompt you for a username, an email address, and a password for your administrative user.

output
Username (leave blank to use 'linuxize'): admin
Email address: admin@linuxize.com
Password: 
Password (again): 
Superuser created successfully.

5. Testing the Development Server

Start the development web server using the manage.py script followed by the runserver option:

Terminal
python manage.py runserver

You’ll see the following output:

output
Performing system checks...

System check identified no issues (0 silenced).
October 20, 2018 - 11:16:28
Django version 2.1.2, using settings 'mydjangoapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Info
If you installed Django on a virtual machine and you want to access Django development server then you’ll need to edit the settings.py file and add the server IP address inside the ALLOWED_HOSTS list.

Open http://127.0.0.1:8000 in your web browser and you will be presented with the default Django landing page:

CentOS 7 django landing page

You can access the Django admin interface, by adding /admin/ to the end of the URL (http://127.0.0.1:8000/admin/). This will take you to the admin login screen:

CentOS 7 django login page

Enter your username and password and you will be redirected to the Django admin page:

CentOS 7 django admin page

To stop the development server type CTRL-C in your terminal.

6. Deactivating the Virtual Environment

Once you are done with your work, deactivate the environment, by typing deactivate and you will return to your normal shell.

Terminal
deactivate

Conclusion

You have learned how to create a Python virtual environment and install Django on your CentOS 7 machine. To create additional Django development environments repeat the steps we outlined in this tutorial.

If you are new to Django, visit the Django documentation page and learn how to develop your first Django app.

If you are facing any problems, feel free to leave a comment.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page