Install Django on Rocky Linux 9

In this tutorial, we want to show you to Install and Configure Django Python Framework on Rocky Linux 9.

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.

Steps To Install and Configure Django on Rocky Linux 9

To complete this guide, you must 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 guide on Initial Server Setup with Rocky Linux 9.

Because Django is a Python-based framework, you need to have Python installed on your server. To install the latest Python 3 and Pip, you can visit this guide on Install Python 3.11 on Rocky Linux 9.

Install Django Python Framework on Rocky Linux 9

At this point, you can use the 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:

django-admin --version
Output
4.1.5

Create a Django sample project

When you have successfully installed Django on Rocky Linux 9, 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 to your Django project directory:

cd project

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:

cd test_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 Rocky Linux 9

At this point, you need to create a superuser for Django so 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.

Configure Firewall For Django

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 commands 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

Edit setting.py File

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 so 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 Rocky Linux 9 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 30, 2023 - 11:55:43
Django version 4.1.5, 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:

http://server-IP:8000

You will see:

Django welcome screen
Django

Django Admin Dashboard

Then, you can access the Django admin dashboard with:

http://server-IP:8000/admin

You will see the Django admin login screen:

Django admin login screen
Django admin Login

Enter your admin user and password that you have created before and you will see your Django dashboard.

Django admin dashboard
Django Administration

That’s it, you are done.

For more information, you can visit the Django Documentation page.

Conclusion

At this point, you have learned to Install and Configure Django Python Framework on Rocky Linux 9.

Hope you enjoy it. You may be like these articles:

Install Joomla on Rocky Linux 9

Install Ntopng on Rocky Linux 9

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!