Essential Steps To Install Django on AlmaLinux 9 – OrcaCore

Posted on

Essential Steps To Install Django on AlmaLinux 9 - OrcaCore

Essential Steps To Install Django on AlmaLinux 9 – OrcaCore

This comprehensive guide will walk you through the process to Install Django on AlmaLinux 9. Django, a high-level Python framework, simplifies web development by providing a robust set of tools and conventions. It empowers developers to build sophisticated web applications quickly and efficiently, focusing on the unique features of their projects rather than getting bogged down in repetitive tasks.

Django promotes the principle of DRY (Don’t Repeat Yourself) through component reusability. It offers built-in functionalities such as user authentication, database interaction, and CRUD (Create, Read, Update, Delete) operations, significantly reducing development time and effort.

Follow the detailed steps below to successfully set up a Django Python Web App on your AlmaLinux 9 server, as presented originally on the Orcacore website.

Before beginning, ensure you have access to your AlmaLinux 9 server as a non-root user with sudo privileges and that a basic firewall is configured. Our guide on Initial Server Setup with AlmaLinux 9 provides detailed instructions on how to achieve this.

Since Django is Python-based, you’ll need Python installed. Refer to our guide on Install Python 3.11 on AlmaLinux 9 to install the latest Python 3 version along with Pip, the Python package installer. This is the first step to Install Django on AlmaLinux 9.

1. Install Django Python Web App on AlmaLinux 9

With Python and Pip set up, you can now use Pip to install Django:

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

Now that you’ve successfully installed Django on AlmaLinux 9, let’s create a sample project.

First, create a directory for your Django project:

sudo mkdir project

Navigate into the newly created directory:

cd project

Use the Django admin tool to start your first project, named test_project in this example:

django-admin startproject test_project

Change the current directory to your created project:

cd test_project

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

Apply any pending migrations using the following command:

sudo python3 manage.py migrate

The output should resemble:

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

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

sudo python3 manage.py createsuperuser

You’ll be prompted for information. Example:

**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’re using firewalld, configure it 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

Edit the settings.py file to allow external access to your Django application.

Open the file with your preferred text editor (e.g., vi):

sudo vi test_project/settings.py

Locate the ALLOWED_HOSTS line and specify your server’s IP address. Alternatively, you can set ALLOWED_HOSTS = ['*'] to allow access from any network (use with caution in production environments):

...
# 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 after making the necessary changes.

3. Access the Django Python Web App

Start your Django app on AlmaLinux 9 using the following command:

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

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 default Django welcome page.

Django Admin Dashboard

Access the Django admin dashboard by visiting:

http://server-IP:8000/admin

You will see the Django admin login screen. Enter the superuser credentials you created earlier to access the Django administration interface.

That completes the basic Install Django on AlmaLinux 9 process.

For more in-depth information, refer to the Django Documentation.

Conclusion

You have successfully learned how to Install and Configure Django Python Web App on AlmaLinux 9.

Here are some additional articles you may find useful:

Install Apache Kafka on AlmaLinux 9

Install and Configure WordPress on AlmaLinux 9

How to Edit Sudoers File in Linux

How to use Cron in Linux

Manage users and groups on a Linux system

How to Kill a Process in Linux from the Command Line?

Configuration of ClamAV Antivirus on Linux

Install and Configure Linux Malware Detect in Linux

Alternative Solutions for Installing Django on AlmaLinux 9

While the pip method is straightforward, alternative approaches offer benefits like dependency isolation and environment management. Here are two different methods to Install Django on AlmaLinux 9:

1. Using venv (Virtual Environments)

Virtual environments isolate Python dependencies for different projects, preventing conflicts and ensuring reproducibility.

Explanation: venv creates a self-contained directory that holds a specific Python interpreter and its associated packages. This means that installing Django within a virtual environment won’t affect the system-wide Python installation or other projects. It’s highly recommended for managing multiple Django projects with potentially conflicting dependencies.

Steps:

  1. Create a Virtual Environment:

    python3 -m venv myprojectenv

    This command creates a directory named myprojectenv containing the virtual environment.

  2. Activate the Virtual Environment:

    source myprojectenv/bin/activate

    After activation, your shell prompt will change to indicate that you’re working within the virtual environment (e.g., (myprojectenv) user@host:~$).

  3. Install Django:

    pip install Django

    Django will now be installed within the virtual environment.

  4. Create your Django project:

    django-admin startproject test_project . # Note the '.' to create the project in the current directory
  5. Apply Migrations and Create Superuser (as described in the original guide):

    python3 manage.py migrate
    python3 manage.py createsuperuser
  6. Run the development server (as described in the original guide):

    python3 manage.py runserver 0.0.0.0:8000
  7. Deactivate the Virtual Environment:

    deactivate

    When you’re finished working on the project, deactivate the environment.

Benefits:

  • Dependency Isolation: Prevents conflicts between different projects.
  • Reproducibility: Ensures consistent environments across different machines.
  • Clean System: Keeps the system-wide Python installation clean.

2. Using Anaconda/Miniconda

Anaconda and Miniconda are popular Python distributions that come with their own package manager (conda) and environment management capabilities. They provide a more comprehensive solution for managing Python environments and dependencies.

Explanation: Anaconda is a large distribution that includes many scientific computing packages. Miniconda is a minimal distribution that only includes conda, Python, and a few essential packages. Both can be used to create isolated environments for Django projects. conda handles package dependencies, similar to pip, but with a stronger focus on scientific computing.

Steps (using Miniconda):

  1. Download and Install Miniconda:

    Download the Miniconda installer for Linux from the official Anaconda website and follow the installation instructions.

  2. Create a Conda Environment:

    conda create --name mydjangoproject python=3.9  # Or your preferred Python version

    This creates a new conda environment named mydjangoproject with Python 3.9.

  3. Activate the Conda Environment:

    conda activate mydjangoproject

    Your shell prompt will change to indicate the active environment.

  4. Install Django:

    conda install -c conda-forge django

    conda install installs Django and its dependencies. The -c conda-forge option specifies the conda-forge channel, which provides a wide range of packages.

  5. Create your Django project:

    django-admin startproject test_project . # Note the '.' to create the project in the current directory
  6. Apply Migrations and Create Superuser (as described in the original guide):

    python3 manage.py migrate
    python3 manage.py createsuperuser
  7. Run the development server (as described in the original guide):

    python3 manage.py runserver 0.0.0.0:8000
  8. Deactivate the Conda Environment:

    conda deactivate

Benefits:

  • Comprehensive Environment Management: conda manages both Python packages and system-level dependencies.
  • Cross-Platform Compatibility: Works consistently across different operating systems.
  • Reproducibility: Conda environments can be easily exported and recreated on other machines.

Choosing the right method to Install Django on AlmaLinux 9 depends on your specific needs and preferences. venv is a lightweight and straightforward option for simple projects, while Anaconda/Miniconda provides a more robust and feature-rich solution for complex environments.

Leave a Reply

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