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:
[[email protected] ~]$ python3 -V Python 3.6.8 [[email protected] ~]$
To check the pip version, execute:
[[email protected] ~]$ pip3 -V pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6) [[email protected] ~]$
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:
[[email protected] ~]$ django-admin --version 3.0.6 [[email protected] ~]$
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,
[[email protected] ~]$ 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
[[email protected] ~]$ cd /opt/ [[email protected] opt]$ sudo django-admin startproject linuxtechi [[email protected] 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 [[email protected] 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.
[[email protected] linuxtechi]$ sudo python3 manage.py createsuperuser Username (leave blank to use 'root'): sysadmin Email address: email@example.com Password: Password (again): Superuser created successfully. [[email protected] 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:
[[email protected] ~]$ sudo firewall-cmd --add-port=8000/tcp --zone=public --permanent success [[email protected] ~]$
For the changes to register on the firewall reload the firewall as shown:
[[email protected] ~]$ sudo firewall-cmd --reload success [[email protected] ~]$
To verify that port 8000 is open on the firewall, use the command below to view open ports:
[[email protected] ~]$ sudo firewall-cmd --list-ports 8000/tcp [[email protected] ~]$
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.
from Linuxtechi https://www.linuxtechi.com/setup-django-python-framework-centos-8/