Question

I am serving a Django App on Linux - what now?

Hey! I followed the full and amazing series of articles by Mitchell Anicas, Brian Boucheron, Jeremy Morris, Lisa Tagliaferri:

  1. An Introduction to the Linux Terminal
  2. Initial Server Setup with Ubuntu 20.04
  3. How To Install Python 3 and Set Up a Programming Environment on an 4. Ubuntu 20.04 Server
  4. How To Create a Django App and Connect it to a Database
  5. How To Create Django Models
  6. How to Enable and Connect the Django Admin Interface
  7. How To Create Views for Django Web Development

That helped me learn how to deploy a Django app to a Linux droplet. I got through without a hitch, which is very rare and testifies to excellent articles.

My question is, what now? In article #5, Morris mentions that:

“Note that this will provide you with a development environment in which to work on your blog web app, but you will need to take more steps before you put your blog live on the internet, and will need to set up domain names, and add additional layers of security.”

Do you have any articles about best practices for deploying a web site? For example, my serve command of course cancels whenever I close my SSH connection, and I want it to run continuously - that’s the whole point of a VPS. I also probably should change my Django app’s DEBUG variable to false, and there are probably other things I should know. These Digital ocean writers have already provided me with a lot of great information, but I was hoping that there was more out there for me to continue learning.

Thanks a ton for your time.

best,

Elijah


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
April 25, 2024

Heya,

etting up Nginx as a reverse proxy for a Django application and configuring PostgreSQL as the database backend involves several steps. This tutorial will walk you through installing and configuring these components on a Linux server (Ubuntu as an example).

Prerequisites

  • A Linux server (e.g., Ubuntu 20.04)
  • Root or sudo privileges on the server

Step 1: Install Required Packages

First, update your package list and install Python, pip, virtualenv, PostgreSQL, and Nginx.

sudo apt update
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl

Step 2: Set Up PostgreSQL

Create a PostgreSQL database and user for your Django application.

  1. Log into the PostgreSQL shell:
sudo -u postgres psql
  1. Create a database:
CREATE DATABASE myproject;
  1. Create a user with a password:
CREATE USER myprojectuser WITH PASSWORD 'password';
  1. Set default encoding, transaction isolation scheme (recommended settings):
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myprojectuser SET timezone TO 'UTC';
  1. Grant all privileges on the database:
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
  1. Exit the PostgreSQL shell:
\q

Step 3: Create a Python Virtual Environment

Create a directory for your project, set up a virtual environment, and install Django.

  1. Create and move into a directory:
mkdir ~/myproject
cd ~/myproject
  1. Create a virtual environment:
python3 -m venv myenv
  1. Activate the virtual environment:
source myenv/bin/activate
  1. Install Django and psycopg2:
pip install django psycopg2

Step 4: Start a Django Project

  1. Create a new Django project:
django-admin startproject myproject .
  1. Update the DATABASES setting in myproject/settings.py:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}
  1. Migrate the initial database schema:
python manage.py migrate
  1. Run the Django development server to test:
python manage.py runserver

Step 5: Set Up Nginx as a Reverse Proxy

  1. Deactivate the virtual environment:
deactivate
  1. Create an Nginx server block file:
sudo nano /etc/nginx/sites-available/myproject
  1. Add the following configuration:
server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/username/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/username/myproject/myproject.sock;
    }
}
  1. Link the server block file to sites-enabled:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
  1. Test the Nginx configuration for syntax errors:
sudo nginx -t
  1. Restart Nginx:
sudo systemctl restart nginx
  1. Optionally, secure Nginx with Let’s Encrypt (recommended for production).

Step 6: Collect Static Files

  1. Edit myproject/settings.py to include:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
  1. Collect static files:
python manage.py collectstatic

This setup should get your Django application running with PostgreSQL as the database, served by Nginx as a reverse proxy on a Linux server. Remember to replace placeholders like username, server_domain_or_IP, and paths as necessary to match your actual setup.

Bobby Iliev
Site Moderator
Site Moderator badge
October 6, 2021

Hi there,

Really happy to hear that those tutorials were helpful!

Yes, you can follow the steps here on how to deploy Django with Gunicorn and Nginx:

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

That way when you close your SSH session, the service would still be up and running on the server.

Hope that this helps! Best, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel