Methods of payment Abuse

Simple Guide to Deploying Django on Ubuntu

25.02.2025, 20:00

Django is one of the most popular frameworks for building web applications in Python. It simplifies working with databases, handling HTTP requests, and managing users. However, for production use, Django requires proper configuration and integration with other tools. In this guide, we will cover the full process of deploying Django on an Ubuntu 24.04 server using PostgreSQL for data storage, Gunicorn for request handling, and Nginx as the web server.

Key Technologies Used in This Guide

Django
Django is a powerful web development framework that allows you to quickly build complex web applications. It includes built-in tools for database management, routing, templating, and more. Django follows the "Don't Repeat Yourself" (DRY) principle, making code more readable and maintainable.

PostgreSQL
PostgreSQL is a modern relational database management system (RDBMS) that supports transactions, advanced SQL queries, and a high level of security. Unlike SQLite, which is the default database in Django, PostgreSQL is more suitable for scalable, multi-user applications.

Gunicorn
Gunicorn (Green Unicorn) is a WSGI server for Python that sits between Django and the web server, processing HTTP requests and forwarding them to the application. Gunicorn supports multi-threaded request execution, making it a good choice for production environments.

Nginx
Nginx is a high-performance web server that acts as a reverse proxy in front of Gunicorn. It handles HTTP requests, serves static files (images, CSS, JS), and forwards dynamic requests to the application via Gunicorn.

1. Installing Required Packages

Before installation, make sure your server is updated. This helps prevent potential conflicts between packages.

Update the package list:

sudo apt update

Now install all necessary dependencies:

sudo apt install python3-pip python3-venv libpq-dev postgresql postgresql-contrib nginx

This command will download and install:
→ Python 3 and pip – tools for managing Python dependencies.
→ venv – a built-in mechanism for creating virtual environments.
→ PostgreSQL – a powerful database system.
→ libpq-dev – supporting libraries for PostgreSQL.
→ Nginx – the web server that will be used in production.

After installation, check that everything is working:

python3 --version
postgres --version
nginx -v

If the program versions are displayed without errors, proceed to the next step.

2. Configuring the PostgreSQL Database

Now, let's create a database and user for Django. First, log in to the PostgreSQL console:

sudo -u postgres psql

Create a new database and user:
CREATE DATABASE myproject;
CREATE USER myprojectuser WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;

Exit the PostgreSQL console:

\q

Now PostgreSQL is ready for use.

3. Creating a Virtual Environment and Installing Django

For better project management in Python, virtual environments are used. They allow each project to have isolated dependencies.
Navigate to your home directory and create a project folder:

mkdir ~/myproject && cd ~/myproject

Create and activate a virtual environment:

python3 -m venv venv
source venv/bin/activate

Now install the required dependencies:

pip install django gunicorn psycopg2

Create a new Django project:

django-admin startproject myproject 

4. Configuring Django

Now configure the database settings in Django. Open settings.py:

nano myproject/settings.py

Find the DATABASES section and modify it:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Add your domain or server IP to the list of allowed hosts:

ALLOWED_HOSTS = ['your_domain_or_IP']

Apply migrations and create an admin user:

python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic

5. Configuring Gunicorn

Gunicorn will help Django run in production. Test it by running:

gunicorn --bind 0.0.0.0:8000 myproject.wsgi

Now, create a systemd service:

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

Add the following content:

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

[Service]
User=your_user
WorkingDirectory=/home/your_user/myproject
ExecStart=/home/your_user/myproject/venv/bin/gunicorn --workers 3 --bind unix:/home/your_user/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Start and enable the service:

sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl status gunicorn

6. Configuring Nginx

Create a configuration file for Nginx:

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

Add the following content:
server {
    listen 80;
    server_name your_domain_or_IP;

    location /static/ {
        root /home/your_user/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/your_user/myproject/myproject.sock;
    }
}

Activate the configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

Conclusion

Now your Django website is running on Ubuntu with PostgreSQL, Gunicorn, and Nginx. To update the code, simply replace the project files and restart Gunicorn:

sudo systemctl restart gunicorn

Your site is now ready for use!