Gunicorn

How To Install and Configure Django with Postgres, Nginx, and Gunicorn On Ubuntu 24.04

Welcome to the Greenhost.cloud blog! In this post, we will guide you through the process of setting up a Django application using PostgreSQL as the database, Nginx as the web server, and Gunicorn as the application server on Ubuntu 24.04. By the end of this tutorial, you will have a fully functioning web application ready for production.

Prerequisites

Before we start, make sure you have:

  • A server running Ubuntu 24.04
  • Root or sudo privileges on your server
  • Python 3 and pip installed
  • Basic knowledge of the command line

Step 1: Update Your System

First, ensure your system is up to date by running the following commands:

sudo apt update
sudo apt upgrade -y

Step 2: Install PostgreSQL

To install PostgreSQL, run:

sudo apt install postgresql postgresql-contrib -y

After installation, log in to the PostgreSQL shell:

sudo -u postgres psql

Create a new database and user:

CREATE DATABASE myproject;
CREATE USER myuser WITH PASSWORD 'mypassword';
ALTER ROLE myuser SET client_encoding TO 'utf8';
ALTER ROLE myuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE myproject TO myuser;

Exit the PostgreSQL prompt:

\q

Step 3: Install Required Packages

Next, install the required packages including Python, pip, and virtualenv:

sudo apt install python3 python3-pip python3-venv -y

Step 4: Set Up Django Project

Create a directory for your Django project and navigate to it:

mkdir ~/myproject
cd ~/myproject

Create a virtual environment and activate it:

python3 -m venv venv
source venv/bin/activate

Now, install Django and the PostgreSQL adapter:

pip install django psycopg2

Create a new Django project:

django-admin startproject myproject .

Step 5: Configure Database Settings

Open the settings.py file in your Django project:

nano myproject/settings.py

Find the DATABASES section and update it to use PostgreSQL:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'myproject',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Step 6: Run Migrations

Now, apply the migrations to set up your database:

python manage.py migrate

Step 7: Create a Superuser

To manage your Django application, create a superuser:

python manage.py createsuperuser

Step 8: Test the Development Server

Run the development server to ensure everything is working:

python manage.py runserver

Visit http://127.0.0.1:8000 in your browser. You should see the Django welcome page.

Step 9: Install Gunicorn

Stop the development server (Ctrl + C) and install Gunicorn:

pip install gunicorn

You can test Gunicorn by running:

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

Step 10: Install Nginx

Install Nginx to serve your application:

sudo apt install nginx -y

Step 11: Configure Nginx

Create a new Nginx configuration file for your project:

sudo nano /etc/nginx/sites-available/myproject

Add the following configuration:

server {
    listen 80;
    server_name your_domain.com;  # Replace with your domain or IP

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/your_user/myproject;  # Replace with your username
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/your_user/myproject/myproject.sock;  # Replace with your username
    }
}

Enable the new configuration:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Test the Nginx configuration:

sudo nginx -t

If everything is fine, restart Nginx:

sudo systemctl restart nginx

Step 12: Create a Systemd Service for Gunicorn

Create a new service file for Gunicorn:

sudo nano /etc/systemd/system/gunicorn.service

Add the following content:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=your_user  # Replace with your username
Group=www-data
WorkingDirectory=/home/your_user/myproject  # Replace with your username
ExecStart=/home/your_user/myproject/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/your_user/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Enable and start the Gunicorn service:

sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Step 13: Adjust Firewall Settings

If you have a firewall enabled, allow Nginx Full:

sudo ufw allow 'Nginx Full'

Step 14: Test Your Application

Visit your domain or server’s IP address in your browser. You should see your Django application running!

Conclusion

Congratulations! You have successfully installed and configured Django with PostgreSQL, Nginx, and Gunicorn on Ubuntu 24.04. You are now ready to start building your web application. If you have any questions or need further assistance, feel free to reach out!

For more tutorials like this, stay tuned to the Greenhost.cloud blog.