opened image

How to install Django on CentOS 7: A Step-by-Step Guide

 


Step 1: Installing Python and Creating a Virtual Environment on CentOS 7

 

To work successfully with Django on a CentOS 7 server, the first thing you need to do is install Python 3 and set up a virtual environment. Here's how you can do it.

 

Updating System Packages:

 

Before installing new software, it's important to update the system package list. This ensures that you get the latest updates and security fixes. Open the terminal and execute the command:

 

sudo yum update

 

This command will update your package list and ensure that the system uses the latest versions of programs.

 

Installing the EPEL Repository:

To access additional packages in CentOS 7, you need to install the EPEL (Extra Packages for Enterprise Linux) repository.

 

Execute the following command:

 

sudo yum install epel-release

 

 

The EPEL repository contains additional packages not included in the standard CentOS set, including some versions of Python 3.

 

Installing Python 3:

 

Now you're ready to install Python 3 and the necessary development tools. Enter the following commands:

 

sudo yum install python34 python34-devel

 

 

python34 installs Python version 3. 4, and python34-devel installs the development tools necessary for compiling Python modules.

 

Installing virtualenv Tool:

After installing Python, the next step is to install the virtualenv tool, which is used to create isolated Python environments.

 

Install virtualenv using pip:

 

sudo yum install python34-pip
sudo pip3 install virtualenv

 

 

python34-pip installs the Python package management tool for version 3.4, and pip3 install virtualenv installs virtualenv.

 

If you get an error:

-bash: pip3: command not found

 

This indicates that pip for Python 3 was not installed or not added to the PATH environment variable. In CentOS 7, installing pip3 may require a few additional steps.

 

First, download the get-pip.py version compatible with Python 3.4 using the following command:

 

curl "https://bootstrap.pypa.io/pip/3.4/get-pip.py" -o "get-pip.py"

 

Now install pip for Python 3.4 by running the script:

 

python3.4 get-pip.py

 

 

This command will install pip, which will be specific to your installed version of Python 3.4.

After installation, check that pip is installed correctly:

 

pip3.4 --version

 

 

Using pip3.4, install virtualenv:

 

pip3.4 install --upgrade "virtualenv<20"

 

  • virtualenv is used for creating isolated Python environments, which allows you to manage project dependencies and avoid conflicts between different projects.

 

 

Creating a Virtual Environment:

 

After installing virtualenv, you can create a virtual environment for your Django project. This allows you to manage project dependencies and avoid conflicts with other projects. Create a virtual environment using the command:

 

python3.4 -m venv myprojectenv

 

myprojectenv is the name of your virtual environment. This command will create a new myprojectenv directory in the current directory with a separate Python installation and a place to store Python dependencies.

 

Activating the Virtual Environment:

 

To start working in the created virtual environment, activate it:

 

source myprojectenv/bin/activate

 

After activating the virtual environment, your command line will change, showing the name of the virtual environment, indicating that all subsequent Python commands will be executed in this isolated environment.

 

Now your CentOS 7 server is ready for Django installation and web application development.

 


Step 2: Installing Django

 

Installing Django in a Virtual Environment:

After activating your virtual environment, you can safely install Django within it. Django will be installed only within this virtual environment and will not affect other projects or system settings. Install Django using pip, the Python package manager:

 

pip install django

 

This command will find and install the latest version of Django, along with all necessary dependencies.

 

Verifying Django Installation:

After installation, check the Django version to ensure that the installation was successful. This will also help you to make sure that you are using the latest version of Django:

 

django-admin --version

 

This command will display the installed version of Django.

 

 

Step 3: Creating a New Django Project

 

Creating a Django Project:

Now that Django is installed, create a new project. This can be done using the following command:

 

django-admin startproject myproject

 

This command creates a new directory myproject with the basic structure of a Django project.

 

Setting Up the Project:

After creating the project, go to its directory:

 

cd myproject

 

Then perform database migrations. Django uses these migrations to set up the database for your project:

 

python manage.py migrate

 

This command will set up the database using the default migrations provided by Django.

 

 

Step 4: Starting the Django Development Server

 

Starting the Server:

Start the built-in Django development server to verify that your project is working:

 

python manage.py runserver 0.0.0.0:8000

 

Here 0.0.0.0 tells Django to accept connections from all IP addresses, and 8000 is the port on which the server will run.

 

Accessing the Web Application:

& nbsp;

Open port 8000:

 

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

 

Now you can open a web browser and enter your server's IP address with port 8000 to access your Django application.

 

If you can't access the site at IP:8000, or you encounter webpage errors like DisallowedHost, you need to add your server's IP address to the ALLOWED_HOSTS list in the settings.py file of your Django project. After that, you can restart the server:

 

python manage.py runserver 0.0.0.0:8000

 

 

Step 5: Creating a Superuser

 

Creating an Administrator Account:

To access the Django admin panel, you will need a superuser. Create one using the following command:

 

python manage.py createsuperuser

You will be prompted to enter a username, email address, and password for the superuser.

After completing all these steps, you will have a fully configured Django project on your CentOS 7 server, ready for further development and use.


Conclusion

 

Congratulations! You have successfully installed Django on CentOS 7 and created a basic project. This is just the beginning of your journey in web application development using Django. Continue learning to expand your knowledge and skills in working with this powerful tool.