Django is an opensource, fully loaded, and versatile Python-based framework that enables developers to build and deploy scalable and robust web applications that can meet the high demands of end-users. The main objective of Django is to simplify the deployment of complex web applications and take care of crucial aspects of application development such as content administration, authentication, and security.
With Django, you can build any sort of web and chat applications such as social networking sites Some of the popular applications built on Django include Instagram, Spotify, and Eventbrite. In this guide, we will walk you through the installation and configuration of Django on CentOS 8.
Step 1) Install Python3 and pip on CentOS 8
Given that Django is a Python-based toolkit, we need to ensure that Python is installed. Right off the bat, we are going to install Python3 and pip package manager. To achieve this, run the command below on the terminal.
$ sudo dnf install -y python36 python3-pip
Once the installation is completed successfully, you can verify the version of Python3 installed by running:
[linuxtechi@centos-8 ~]$ python3 -V Python 3.6.8 [linuxtechi@centos-8 ~]$
To check the pip version, execute:
[linuxtechi@centos-8 ~]$ pip3 -V pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6) [linuxtechi@centos-8 ~]$
Step 2) Install Django via pip3
With the prerequisites installed, proceed and install Django web framework using the command:
$ sudo pip3 install django
This takes a few seconds and in no time, the installation of Django will be complete. To check the version of Django installed on your system, run the command below:
[linuxtechi@centos-8 ~]$ django-admin --version 3.0.6 [linuxtechi@centos-8 ~]$
From the output, you can see that we have installed Django version 3.0.6
Now copy django-admin binary from /usr/local/bin to /usr/bin,
[linuxtechi@centos-8 ~]$ sudo cp /usr/local/bin/django-admin /usr/bin/
Step 3) Creating a Django project
We have just installed Django in the previous and in this step, we are going to create a sample Django project. For this guide, we will name our project linuxtechi.
We are going to create a new project within the /opt directory using the django-admin command. So switch to root user and run the commands below:
$ cd /opt $ sudo django-admin startproject linuxtechi
Output of above commands would be the following
[linuxtechi@centos-8 ~]$ cd /opt/ [linuxtechi@centos-8 opt]$ sudo django-admin startproject linuxtechi [linuxtechi@centos-8 opt]$ ls -l linuxtechi/ total 4 drwxr-xr-x. 2 root root 89 May 18 07:07 linuxtechi -rwxr-xr-x. 1 root root 630 May 18 07:07 manage.py [linuxtechi@centos-8 opt]$
Inside the newly created project directory, be sure to find another directory with the same name and a manage.py Python file. Once you have created the new project folder, navigate into it, and migrate the changes using the command:
$ cd linuxtechi $ sudo python3 manage.py migrate
Your output should be similar to what we have below:
Step 4) Create a Django Admin account user
Next, we need to create an admin user account for logging in to Django on the front-end. Therefore, run the command below:
$ sudo python3 manage.py createsuperuser
You will be prompted for the username, email address, and the password which you will thereafter confirm.
[linuxtechi@centos-8 linuxtechi]$ sudo python3 manage.py createsuperuser Username (leave blank to use 'root'): sysadmin Email address: [email protected] Password: Password (again): Superuser created successfully. [linuxtechi@centos-8 linuxtechi]$
Step 5) Configure the firewall for application
Before deploying our application, we need to allow port 8000, which the default port that our application will be listening to. Proceed and open the port on firewalld as shown:
[linuxtechi@centos-8 ~]$ sudo firewall-cmd --add-port=8000/tcp --zone=public --permanent success [linuxtechi@centos-8 ~]$
For the changes to register on the firewall reload the firewall as shown:
[linuxtechi@centos-8 ~]$ sudo firewall-cmd --reload success [linuxtechi@centos-8 ~]$
To verify that port 8000 is open on the firewall, use the command below to view open ports:
[linuxtechi@centos-8 ~]$ sudo firewall-cmd --list-ports 8000/tcp [linuxtechi@centos-8 ~]$
Great! The output confirms that port 8000 is open on the firewall.
Though we have allowed port 8000 on the firewall, Django still cannot be accessed by external users. We need to modify the settings.py file inside the sample folder and specify the server’s address or asterisk sign “*” in the ALLOWED_HOSTS parameter.
Note: If we specify the ip address of server then we can access our application from that IP only and if you want your access application from all the networks then specify “*”
$ sudo vim /opt/linuxtechi/linuxtechi/settings.py
Save and close the file
Step 6) Start the Django application
With all settings and configurations in place, power on the Django application using the commands below:
$ cd /opt/linuxtechi $ sudo python3 manage.py runserver 0.0.0.0:8000
To access the application on a browser, browse the URL shown:
The webpage confirms that Django has been successfully installed and configured. To access the admin dashboard, append the /admin at the end of the URL.
Provide the username of the Django admin user that you created and the password and press ENTER to access the dashboard.
Your dashboard should look as shown below.
As you have seen, the installation of Django framework on CentOS 8 is quite a breeze. Django is a leading versatile and powerful web framework that can help you create a myriad of Python web applications without worrying much about the complexities of deployment. We hope you found this informative, Feel free to share this article with your friends on your social media platforms.