Django is a powerful Python framework for building dynamic websites and applications. It follows the Model-View-Controller (MVC) architecture, which simplifies the development process by handling many underlying tasks. In this tutorial, we will guide you through the process of installing Django on a Rocky Linux 9 server, along with Postgres, Nginx, and Gunicorn. This combination of technologies provides a robust and scalable environment for hosting your Django projects.
Before we begin, let’s ensure that you have the necessary prerequisites:
- A server running Rocky Linux 9.
- A non-root user with sudo privileges.
- A fully qualified domain name (FQDN) pointing to your server. For the purposes of this tutorial, we will use
django.example.com
as the domain name.
Step 1: Configure Firewall
The first step is to configure the firewall on your Rocky Linux server to allow incoming HTTP and HTTPS traffic. By default, Rocky Linux uses Firewalld as the firewall management tool. To check the firewall’s status, run the following command:
$ sudo firewall-cmd --state
If the firewall is running, you will see running
as the output. Next, check the list of active services and ports:
$ sudo firewall-cmd --permanent --list-services
This command will display the services currently allowed through the firewall. By default, the cockpit
,dhcpv6-client
, and ssh
services are active. However, Django requires HTTP and HTTPS ports to function properly. To open these ports, run the following commands:
$ sudo firewall-cmd --add-service=http --permanent $ sudo firewall-cmd --add-service=https --permanent
Once the ports are added, reload the firewall to apply the changes:
$ sudo firewall-cmd --reload
Step 2: Install PostgreSQL and Utilities
Rocky Linux 9 ships with an older version of PostgreSQL. In this tutorial, we will install PostgreSQL 14, the latest stable version, for our Django project. To install PostgreSQL, follow these steps:
- Install the repository RPM for PostgreSQL:
$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
- Install PostgreSQL 14 server, along with additional packages:
$ sudo dnf install -y postgresql14-server postgresql14-contrib postgresql14-devel python3-psycopg2
- Initialize the PostgreSQL database:
$ sudo/ usr/pgsql-14/bin/postgresql-14-setup initdb
- Enable and start the PostgreSQL service:
$ sudo systemctl enable postgresql-14 --now
To verify that the service is running, use the following command:
$ sudo systemctl status postgresql-14
Step 3: Configure PostgreSQL
Once PostgreSQL is installed, we need to create a new database and user for our Django project. Follow these steps to configure PostgreSQL:
- Log in to the PostgreSQL shell as the
postgres
user:
$ sudo -i -u postgres psql
- Create a new database for your Django project:
postgres=# CREATE DATABASE djangoapp;
- Create a new database user with a strong password:
postgres=# CREATE USER djangouser WITH ENCRYPTED PASSWORD 'dbpassword';
- Grant the necessary privileges to the user:
postgres=# GRANT ALL PRIVILEGES ON DATABASE djangoapp TO djangouser;
- Exit the PostgreSQL shell:
postgres=# q
Step 4: Install Django
There are several methods to install Django, but the most common one is using pip, the Python package installer. We recommend creating a virtual environment to isolate your Django project from the system environment. Follow these steps to install Django using pip:
- Create a directory for your Django project:
$ mkdir ~/sampleproject $ cd ~/sampleproject
- Create a virtual environment:
$ python3 -m venv sample_env
- Activate the virtual environment:
$ source sample_env/bin/activate
- Install Django using pip:
(sample_env) $ pip install django
You can also specify a particular version of Django to install by adding the version number after django
. For example, to install Django version 3.2.1, run:
(sample_env) $ pip install django==3.2.1
To verify the installation, run:
(sample_env) $ django-admin --version
Step 5: Create a Sample Project
Now that Django is installed, let’s create a sample project to test our setup. Follow these steps:
- Create a directory for your sample project:
$ mkdir ~/dj-sample $ cd ~/dj-sample
- Create a Python virtual environment:
$ python3 -m venv sample_proj
- Activate the virtual environment:
$ source sample_proj/bin/activate
- Install the required packages:
(sample_proj) $ pip install django psycopg2
- Generate a Django project:
(sample_proj) $ django-admin startproject demoproject.
- Generate a secret key for your project:
(sample_proj) $ python3 -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
Copy the generated secret key and replace the value of SECRET_KEY
in the settings.py
file:
(sample_proj) $ nano demoproject/settings.py
- Configure the database settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'djangoapp',
'USER': 'djangouser',
'PASSWORD': 'dbpassword',
'HOST': 'localhost',
'PORT': '',
}
}
- Apply the database migrations:
(sample_proj) $ python manage.py migrate
- Create a superuser for the admin interface:
(sample_proj) $ python manage.py createsuperuser
- Collect the static files:
(sample_proj) $ python manage.py collectstatic
- Test the development server:
(sample_proj) $ python manage.py runserver 0.0.0.0:8000
- Open your web browser and access your Django project at
http://<your_server_ip>:8000
.
Congratulations! You have successfully installed and configured Django with Postgres, Nginx, and Gunicorn on your Rocky Linux 9 server. You can now start developing your Django applications and deploy them to your production environment.
Conclusion
In this tutorial, we have covered the step-by-step process of setting up Django with Postgres, Nginx, and Gunicorn on a Rocky Linux 9 server. By following these instructions, you have created a robust and scalable environment for hosting your Django projects. Remember to keep your server and Django project secure by using SSL certificates and following best practices for server administration.
If you are looking for a reliable cloud hosting provider to deploy your Django applications, consider Shape.host. They offer Cloud VPS solutions with excellent performance, scalability, and security. Visit Shape.host to explore their hosting options and start hosting your Django projects with confidence.