In this article, we want to teach you How To Install and Configure Django on AlmaLinux 8.
Django is a high-level Python web framework that enables the rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.
It is free and open-source, has a thriving and active community, great documentation, and many options for free and paid-for support.
How To Install and Configure Django on AlmaLinux 8
Before you start to install Django on Almalinux 8, you need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our article the Initial Server Setup with AlmaLinux 8.
Now you can follow the steps below to install Django on AlmaLinux 8.
Install Python and Pip on AlmaLinux 8
To set up Django on AlmaLinux 8, you need to have Python and Pip installed on your server.
First, update your local package index with the following command:
sudo dnf update -y
Then, you can use the following command to install Python and Pip on AlmaLinux 8:
sudo dnf install python36 python3-pip
When your installation is completed, verify your Python and Pip installation by checking their’s version:
Output Python 3.6.8
Output pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
Set up Django on AlmaLinux 8
At this point, you can use Pip package manager to install Django on your server:
sudo pip3 install Django
When your installation is completed, verify your Django installation by checking its version:
Build a Django sample project
When you have successfully installed Django on AlmaLinux 8, it’s time to build your first project.
First, create a directory for your Django project with the command below:
sudo mkdir project
Then, switch t your Django project directory:
Now you can use the Django admin tool to build your first project, here we named it test_project:
django-admin startproject test_project
Switch to your created project:
In your project directory, there is a manage.py Python file.
Here you can use the following command to apply pending migrations:
sudo python3 manage.py migrate
In your output you will see:
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 auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying auth.0012_alter_user_first_name_max_length... OK Applying sessions.0001_initial... OK
Configure Django on AlmaLinux 8
At this point, you need to create a superuser for Django that you can access the admin panel.
To do this, run the following command:
sudo python3 manage.py createsuperuser
You will be asked some questions. Answer them as shown below:
Output Username (leave blank to use 'root'): admin Email address: [email protected] Password: Password (again): Superuser created successfully.
As you know Django listens on port 8000. We assumed that you have enabled firewalld. Now you need to allow traffic on port 8000 with the command below:
sudo firewall-cmd --add-port=8000/tcp --zone=public --permanent
sudo firewall-cmd --permanent --add-port=80/tcp
Then, reload the firewall to apply the new rules:
sudo firewall-cmd --reload
Here you need to edit the setting.py file to allow Django to be accessible by external users.
Open the file with your favorite text editor, here we use vi:
sudo vi test_project/settings.py
Find the ALLOWD_HOSTS line and specify your server’s IP address. Or you can assign the [‘*’] to the ALLOWED_HOSTS that the application can be accessed from any network.
... # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['your-specified-ip'] # Application definition ...
When you are done, save and close the file.
Access Django Web Interface
At this point, you can start your Django app on AlmaLinux 8 by running the following command:
sudo python3 manage.py runserver 0.0.0.0:8000
In your output you will see:
Output Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). January 12, 2022 - 11:55:43 Django version 3.2.11, using settings 'test_project.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C.
Now you can access the Django web interface that you have successfully installed Django on your server by typing your server’s IP address in your web browser followed by 8000:
You will see:
Then, you can access the Django admin dashboard with:
You will see the Django admin login screen:
Enter your admin user and password that you have created before and you will see your Django dashboard.
At this point, you learn to set up and configure Django on AlmaLinux 8.
Hope you enjoy using it.
May this article about How To Install Django on Debian 11 be useful for you.