Step-by-Step Guide to Installing Django with PostgreSQL, Nginx, and Gunicorn on Rocky Linux 9

Django is a robust Python framework for building dynamic websites and applications, adhering to the MVC (Model-View-Controller) architecture. By using Django, developers can accelerate the application development process as it handles most underlying tasks efficiently.

This guide provides step-by-step instructions to install Django on a Rocky Linux 9 server, create a demo project, and test its functionality.

Prerequisites

  • A server running Rocky Linux 9.
  • A non-root user with sudo privileges.
  • A fully qualified domain name (FQDN) pointing to your server, e.g., django.example.com.
  • Ensure your server is up to date by executing the following command:
    $ sudo dnf update
  • Install essential utility packages (some may already be installed):
    $ sudo dnf install wget curl nano unzip yum-utils -y
  • Ensure SELinux is disabled.

Step 1: Configure Firewall

Start by configuring the Firewalld firewall provided by Rocky Linux.

$ sudo firewall-cmd --state running

To open necessary ports for Django (HTTP and HTTPS), execute:

    $ sudo firewall-cmd --add-service=http --permanent
    $ sudo firewall-cmd --add-service=https --permanent

Reload the firewall to apply changes.

$ sudo firewall-cmd --reload

Step 2: Install PostgreSQL and Utilities

To use Django with PostgreSQL, install Postgres 14:

    $ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    $ sudo dnf install -y postgresql14-server postgresql14-contrib postgresql14-devel python3-psycopg2

Initialize, enable, and start the PostgreSQL service:

    $ sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
    $ sudo systemctl enable postgresql-14 --now
    $ sudo systemctl status postgresql-14

Step 3: Configure PostgreSQL

Log in to PostgreSQL:

$ sudo -i -u postgres psql

Create a new database and user:

    postgres=# CREATE DATABASE djangoapp;
    postgres=# CREATE USER djangouser WITH ENCRYPTED PASSWORD 'dbpassword';
    postgres=# GRANT ALL PRIVILEGES ON DATABASE djangoapp TO djangouser;

Exit PostgreSQL:

postgres=# \q

Step 4: Install Django

Install using pip

For common installation through pip and using a virtual environment:

    $ mkdir ~/sampleproject
    $ cd ~/sampleproject
    $ python3 -m venv sample_env
    $ source sample_env/bin/activate
    (sample_env) $ pip install django

Verify the installation:

(sample_env) $ django-admin --version

When finished, deactivate the virtual environment:

(sample_env) $ deactivate

Step 5: Create a Sample Project

Setup a sample Django project:

    $ mkdir ~/dj-sample
    $ cd ~/dj-sample
    $ python3 -m venv sample_proj
    $ source sample_proj/bin/activate
    (sample_proj) $ pip install wheel django psycopg2 psycopg2-binary
    (sample_proj) $ django-admin startproject demoproject .

Modify the settings to include a generated SECRET_KEY and update database configurations:

(sample_proj) $ nano demoproject/settings.py

Step 11: Configure Nginx

To finalize the configuration, create and modify appropriate Nginx files:

$ sudo nano /etc/nginx/conf.d/django-gunicorn.conf

Conclusion

In this comprehensive guide, you have successfully installed Django along with Gunicorn and Nginx on a Rocky Linux 9 server. Additionally, you applied an SSL certificate for enhanced security.

FAQ

What is Django?
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.
Why use Gunicorn?
Gunicorn is a Python WSGI HTTP server for UNIX, offering excellent solution to serve Django applications and manage multiple concurrent requests.
Why do we need Nginx with Django?
Nginx acts as a reverse proxy, providing security and performance benefits by handling static files and passing requests to Gunicorn.
Is SSL necessary for Django?
Yes, applying SSL encrypts the data flow between the server and clients, significantly enhancing security.
Can I use another database besides PostgreSQL?
Yes, Django supports multiple databases such as MySQL and SQLite. Configure accordingly in your settings file.