Install Django on Rocky Linux 9: Best Python Web Framework

Posted on

Install Django on Rocky Linux 9: Best Python Web Framework

Install Django on Rocky Linux 9: Best Python Web Framework

In this tutorial, brought to you by Orcacore, we will guide you through the process of installing Install Django on Rocky Linux 9. Django is a powerful, high-level Python web framework designed for rapid development and clean, pragmatic design. It emphasizes reusability and "pluggability" of components, rapid development, and the principle of Don’t Repeat Yourself (DRY). With Django, you can focus on creating your application rather than reinventing the wheel. It’s free and open-source, backed by a vibrant community, comprehensive documentation, and ample support options. Let’s explore how to Install Django on Rocky Linux 9.

Before we begin, ensure you have the following prerequisites in place: a server running Rocky Linux 9, a non-root user with sudo privileges, and a basic firewall setup. You can achieve this by following our guide on Initial Server Setup with Rocky Linux 9.

As Django is a Python framework, you’ll need Python installed on your server. Follow our guide on Install Python 3.11 on Rocky Linux 9 to install Python 3 and Pip.

1. Install Django Python Framework on Rocky Linux 9

With Python and Pip ready, you can now install Django using the Pip package manager:

sudo pip3 install Django

Once the installation is complete, verify it by checking the Django version:

django-admin --version
**Output**
4.1.5

Create a Django Sample Project

After successfully Install Django on Rocky Linux 9, let’s create a sample project.

First, create a directory for your Django project:

sudo mkdir project

Navigate to your project directory:

cd project

Use the Django admin tool to start a new project, named test_project in this example:

django-admin startproject test_project

Switch to your newly created project directory:

cd test_project

Inside this directory, you’ll find the manage.py Python file.

Apply any pending migrations using the following command:

sudo python3 manage.py migrate

This should produce output similar to:

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

2. Configure Django on Rocky Linux 9

Now, create a superuser account for accessing the Django admin panel:

sudo python3 manage.py createsuperuser

You will be prompted for a username, email address, and password:

**Output**
Username (leave blank to use 'root'): admin
Email address: sam@orcacore.com
Password:
Password (again):
Superuser created successfully.

Configure Firewall For Django

Django, by default, listens on port 8000. Assuming you have firewalld enabled, you need to allow traffic on this port:

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

Reload the firewall to apply the changes:

sudo firewall-cmd --reload

Edit setting.py File

Modify the setting.py file to allow external access to your Django application.

Open the file using your favorite text editor (e.g., vi):

sudo vi test_project/settings.py

Locate the ALLOWED_HOSTS line and add your server’s IP address or use ['*'] to allow access from any network:

...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['your-specified-ip']
# Application definition
...

Save and close the file.

3. Access Django Python Framework

Start your Django application on Rocky Linux 9:

sudo python3 manage.py runserver 0.0.0.0:8000

You should see output similar to:

**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.

Access the Django web interface by navigating to your server’s IP address followed by port 8000 in your web browser:

http://server-IP:8000

You should see the Django welcome page.

Django Admin Dashboard

Access the Django admin dashboard with:

http://server-IP:8000/admin

You will see the Django admin login screen.

Enter the admin username and password you created earlier to access the Django Python Framework dashboard.

That’s it, you’re done! You have successfully Install Django on Rocky Linux 9.

For more information, refer to the Django Documentation.

Conclusion

You have now successfully learned how to Install Django on Rocky Linux 9. Django is a versatile framework for building robust web applications with ease. Its built-in features like authentication, database management, and an admin panel significantly streamline the development process.

Here are some related articles you might find interesting:

Install Joomla on Rocky Linux 9

Install Ntopng on Rocky Linux 9

How to Install Grafana on Rocky Linux 8

Install Joomla With LAMP Stack On Rocky Linux 8

Install Caddy on Rocky Linux 8

Set Up SSH Keys on Rocky Linux 8

Alternative Installation Methods

While the pip method is straightforward, let’s explore two alternative ways to install and manage Django on Rocky Linux 9: using a virtual environment and utilizing a system package manager (if available).

1. Installing Django within a Virtual Environment

A virtual environment isolates your project’s dependencies, preventing conflicts between different projects. This is considered best practice for Python development.

Explanation:

Virtual environments create isolated spaces for each project, containing its specific Python interpreter and installed packages. This ensures that dependencies for one project don’t interfere with others. Using a virtual environment enhances project portability and maintainability.

Steps:

  1. Install venv: If not already installed, install the venv module:

    sudo dnf install python3-venv
  2. Create a Virtual Environment: Navigate to your project directory and create a virtual environment:

    cd project
    python3 -m venv venv
  3. Activate the Virtual Environment:

    source venv/bin/activate

    Your shell prompt will change to indicate that the virtual environment is active (e.g., (venv) [user@rockylinux project]$).

  4. Install Django: Install Django using pip within the activated virtual environment:

    pip install Django
  5. Proceed with Project Creation and Configuration: Follow the remaining steps in the original guide (creating a project, migrating, creating a superuser, configuring the firewall, and editing settings.py) while ensuring the virtual environment remains activated.

  6. Deactivate the virtual environment When you are done with the project, you can deactivate the virtual environment:

    deactivate

Code Example (Demonstrating Virtual Environment Creation and Activation):

# Create a directory for the project
mkdir my_django_project
cd my_django_project

# Create a virtual environment named "myenv"
python3 -m venv myenv

# Activate the virtual environment
source myenv/bin/activate

# The shell prompt will now indicate the active environment (e.g., (myenv))

# Install Django within the virtual environment
pip install Django

# Check the Django version
django-admin --version

# Deactivate the virtual environment
deactivate

2. Installing Django using dnf (If Available)

Some Linux distributions provide Django packages through their system package managers. While this is less common for the latest Django versions, it can simplify installation and updates in some cases. Rocky Linux does not directly provide Django through dnf. However, you can enable EPEL (Extra Packages for Enterprise Linux) repository and then check if it provides Django.

Explanation:

System package managers like dnf handle dependencies and updates automatically. Installing Django this way integrates it with the operating system’s package management system. However, the available version might not be the latest, and you might encounter dependency issues.

Steps (with a caveat about EPEL):

  1. Enable EPEL Repository (if not already enabled):

    sudo dnf install epel-release
  2. Search for Django Package:

    sudo dnf search django
  3. Install Django (if available): If a Django package is found, install it:

    sudo dnf install python3-django
  4. Verify Installation: Check the Django version:

    django-admin --version
  5. Proceed with Project Creation and Configuration: Follow the remaining steps in the original guide (creating a project, migrating, creating a superuser, configuring the firewall, and editing settings.py). Note that the location of Django-related files might differ slightly compared to the pip installation.

Caveats:

  • The dnf method might install an older Django version.
  • Dependencies might not be resolved automatically, requiring manual installation of missing packages.
  • EPEL doesn’t guarantee the latest Django version.

Important Note: If you choose the dnf method, thoroughly test your Django application to ensure compatibility with the installed version. Using a virtual environment as described above is generally the preferred method for most Django projects because it provides greater control over the Django version and its dependencies.

Leave a Reply

Your email address will not be published. Required fields are marked *