Support Online
Skip to main content

Installing Django on Ubuntu: Nginx, Gunicorn and PostgreSQL

Django (PostgreSQL + Gunicorn + Nginx) Installation on Ubuntu

In this guide, you will install the basic components needed to make the Django application production ready.
Aim; To create a secure, high-performance and scalable Django infrastructure.

What Will You Learn in This Guide?

  • Setting up a PostgreSQL database for Django
  • Creating a Python virtual environment (virtualenv)
  • Serving the application with Gunicorn
  • Configuring Nginx as a reverse proxy
  • Solving common errors in the production environment

1. Installing the Required Packages

In this step, the necessary system packages for Django are installed.

sudo apt update
sudo apt install python3-venv python3-dev libpq-dev postgresql postgresql-contrib nginx
  • This command; Installs PostgreSQL, Nginx and Python development tools.

2. PostgreSQL Database and User Creation

  1. First, switch to the PostgreSQL administrator account.

sudo -u postgres psql

  • Create database and user.

CREATE DATABASE myproject;
CREATE USER myprojectuser WITH PASSWORD 'guclu_sifre';
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
  • This structure allows Django to connect to the database securely.

3. Creating a Python Virtual Environment

  1. Prepare a directory for the project files.

mkdir ~/myprojectdir
cd ~/myprojectdir

  1. Create and activate the virtual environment.

python3 -m venv myprojectenv
source myprojectenv/bin/activate
  • This process ensures that dependencies remain project specific.

4. Django and Gunicorn Installation

  1. While the virtual environment is active, install the necessary Python packages.

pip install django gunicorn psycopg2-binary
  • These packages are sufficient to run the Django application.

5. Creating and Configuring the Django Project

  1. Create Django project files.

django-admin startproject myproject ~/myprojectdir

Update the following settings in the settings.py file:

  1. ALLOWED_HOSTS: Domain name, IP and localhost

  2. DATABASES: PostgreSQL connection information

  3. STATIC_ROOT: Static file directory

These settings are mandatory for the production environment.


6. Preparation of Database and Static Files

  1. Create the database tables.

python manage.py migrate

  1. Add admin user.

python manage.py createsuperuser

  1. Collect static files.

python manage.py collectstatic
  • This process enables Nginx to serve static files.

7. Testing the App with Gunicorn

  1. Run Gunicorn manually.

gunicorn --bind 0.0.0.0:8000 myproject.wsgi
  • This step verifies that the application is working properly via WSGI.

8. Creating a systemd Service for Gunicorn

A service is defined for Gunicorn to run stably in the background.


sudo nano /etc/systemd/system/gunicorn.service
  • This service starts Gunicorn via Unix socket.

Activate the service:


sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn

9. Nginx Reverse Proxy Configuration

  1. Create the Nginx server block.

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

Basic structure:


server {
listen 80;
server_name ornek.com;

location /static/ {
root /home/user/myprojectdir;
}

location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
  • This structure redirects Nginx to Gunicorn.

Activate:


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

  1. Use Let's Encrypt for SSL.

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d ornek.com
  • This process makes HTTPS redirection automatic.

Frequently Asked Questions (FAQ)

  1. Why use Gunicorn instead of Django's own server? Django's development server is not suitable for production.

  2. I am getting 502 Bad Gateway error, what is the reason? Gunicorn may not be working or the socket path may be wrong.

  3. Why is PostgreSQL recommended over SQLite? PostgreSQL is more secure on concurrent connections.

  4. Why are static files served via Nginx? Nginx serves static files much faster than Django.


Result

With this guide, your Django app will:

Production ready

safe

Performance

scalable

becomes.

You can immediately publish your professional Django projects on the GenixNode infrastructure with this architecture.