VOOZH about

URL: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu

⇱ Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu | DigitalOcean


Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu

Updated on October 3, 2025

By Anish Singh Walia

Sr Technical Content Strategist and Team Lead

English
👁 Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu

Introduction

Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server is required.

In this tutorial, you will install and configure some components on Ubuntu to support and serve Django applications. You will be setting up a PostgreSQL database instead of using the default SQLite database. You’ll configure the Gunicorn application server to interface with your applications. You will then set up Nginx to reverse proxy to Gunicorn, giving you access to its security and performance features to serve your apps.

For a comprehensive comparison of database options, see our guide on SQLite vs MySQL vs PostgreSQL: A Comparison of Relational Database Management Systems.

Deploy your applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

You will be installing Django within a virtual environment. Installing Django into an environment specific to your project will allow your projects and their requirements to be handled separately. For more detailed information on working with PostgreSQL in Django, see our comprehensive guide on How to Use PostgreSQL with Your Django Application on Ubuntu 22.04.

Once you have your database and application up and running, you will install and configure the Gunicorn application server. This will serve as an interface to our application, translating client requests from HTTP to Python calls that our application can process. You will then set up Nginx in front of Gunicorn to take advantage of its high performance connection handling mechanisms and its easy-to-implement security features.

Let’s get started.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

👁 Anish Singh Walia
Anish Singh Walia
Author
Sr Technical Content Strategist and Team Lead
See author profile

I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer(Team Lead) @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I have trouble loading my static files following this instruction. Am I the only one?

is this suitable for production ?

Great article! Lead me down the esoteric domain of unix sockets though, but I loved every bit.

Is it secure to have the user in ‘/etc/systemd/system/gunicorn.service’ the same as the system user with sudo privileges? Is it better to run gunicorn with a user that has fewer permissions? If so, could you provide some guidance on how to do so?

Is it secure to have the user in ‘/etc/systemd/system/gunicorn.service’ the same as the system user with sudo privileges? Is it better to run gunicorn with a user that has fewer permissions? If so, could you provide some guidance on how to do so?

I am getting 403 permission errors for the static folder. I can’t figure it out. From what I read, i have change the static folder to var/www/html/static. I have chowned the folder to the nginx user www-data. But none of it is working. This article does not mention any of this. Anyone know what I can do?

Solution to: Nginx not serving static files

Just want to leave this here because a lot of people run into this problem. This guide doesn’t seem to include the fact that nginx requires permission to the static folder and the parent folders. Through some tinkering this is what worked.

sudo chown -R www-data:www-data /home/myprojectuser/myproject/static/
sudo chmod -R 755 /home/myprojectuser/myproject/static/
sudo chmod 755 /home/myprojectuser/
sudo chmod 755 /home/myprojectuser/myproject/

I run into this problem every time I deploy a new server using this great guide hope this helps you.

I just wanted to express my appreciation to the author for this excellent article and to all the people who contributed solutions to the issues. After many years as a hobbyist teaching myself Django, SQL, CSS, javascript and more, I’ve finally got a working website on the internet.

Great Article. I am using this to setup my django application. Here is my first correction. There was error (django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (permission denied for schema public LINE 1: CREATE TABLE “django_migrations” (“id” bigint NOT NULL PRIMA…)in running $python manage.py migrate (due to change postgress public schema rules), so i did following to resolve it.

  1. Connect to postgress: $sudo -u postgres psql
  2. Connect to your db (for Django project [‘myproject’ is used in this article): postgres=# \connect myproject
  3. Grant permission of public schema to your db user: myproject=# GRANT ALL ON SCHEMA public TO myprojectuser;
  4. Exit: \q
👁 Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

© 2026 DigitalOcean, LLC.Sitemap.
Dark mode is coming soon.