Introduction

Django is a powerful web framework that allows developers to create robust web applications quickly. In this guide, you will learn how to install Django with PostgreSQL as the database, Nginx as the web server, and Gunicorn as the application server on Rocky Linux 9. This setup can be efficiently hosted on a Windows VPS UK for optimal performance and reliability.

Prerequisites

  • A Rocky Linux 9 server with root access
  • Basic knowledge of Linux commands
  • An active internet connection
  • A registered domain name pointed to your server's IP address (optional but recommended)

Step 1: Update Your System

Start by updating your package index and upgrading existing packages:

sudo dnf update -y

Step 2: Install Required Packages

Install PostgreSQL, Nginx, and other required packages:

sudo dnf install postgresql postgresql-server nginx python3-pip python3-devel gcc -y

Step 3: Set Up PostgreSQL

Initialize the PostgreSQL database and enable the service:

sudo postgresql-setup --initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql

Secure the PostgreSQL installation by setting a password for the postgres user:

sudo -u postgres psql
ALTER USER postgres PASSWORD 'your_password';
\q

Replace your_password with a strong password of your choice.

Step 4: Create a Database for Django

Log in to PostgreSQL to create a database and user for Django:

sudo -u postgres psql
CREATE DATABASE django_db;
CREATE USER django_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE django_db TO django_user;
\q

Replace your_password with a strong password for the Django user.

Step 5: Install Django

Use pip to install Django and Gunicorn:

pip3 install django gunicorn psycopg2-binary

Step 6: Create a Django Project

Create a new Django project:

django-admin startproject myproject

Change into the project directory:

cd myproject

Step 7: Configure Django for PostgreSQL

Edit the settings.py file in your Django project:

nano myproject/settings.py

Find the DATABASES section and update it as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'django_db',
        'USER': 'django_user',
        'PASSWORD': 'your_password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Replace your_password with the password you set for the Django user.

Step 8: Migrate Database

Run the following commands to apply migrations and create a superuser:

python3 manage.py migrate
python3 manage.py createsuperuser

Follow the prompts to set up the superuser account.

Step 9: Test Django Development Server

Run the Django development server to test your installation:

python3 manage.py runserver 0.0.0.0:8000

Open your web browser and navigate to http://your_server_ip:8000 to see your Django application.

Step 10: Configure Gunicorn

Run Gunicorn to serve your Django application:

gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

Step 11: Configure Nginx for Django

Create a new Nginx configuration file for your Django application:

sudo nano /etc/nginx/conf.d/myproject.conf

Add the following configuration:

server {
    listen 80;
    server_name yourdomain.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /path/to/your/myproject;  # Update with your project path
    }

    location / {
        include proxy_params;
        proxy_pass http://127.0.0.1:8000;
    }
}

Replace yourdomain.com with your actual domain name and update the root path accordingly.

Step 12: Restart Nginx

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 13: Enable SSL with Let's Encrypt (Optional)

Install Certbot for obtaining SSL certificates:

sudo dnf install certbot python3-certbot-nginx -y

Run Certbot to obtain your SSL certificate:

sudo certbot --nginx -d yourdomain.com

Follow the prompts to complete the installation of the SSL certificate.

Step 14: Conclusion

You have successfully installed Django with PostgreSQL, Nginx, and Gunicorn on Rocky Linux 9. This robust web application setup can significantly benefit from being hosted on a Windows VPS. For additional options, explore various VPS UK Windows solutions, including Windows Virtual Private Server Hosting and Windows VPS Hosting UK for optimal performance and security.

© 2024 Django Installation Tutorial. All rights reserved.

Cette réponse était-elle pertinente? 0 Utilisateurs l'ont trouvée utile (0 Votes)