How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
How to install Django with NGINX,
Gunicorn, and PostgreSQL on
Ubuntu?
By Suryansh Singh Shishodia / May 30, 2021
1 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
In this article we’ll learn how to install Django with NGINX, Gunicorn, and
PostgreSQL on Ubuntu.
Django comes with a lightweight development server that is not suitable
for production as it is less secure, low-performance, and not scalable.
It’s intended to be only used for development purposes so you can rapidly
focus on your application development without having to worry about the
production server. Similarly, if you are deploying an application in
production, it is recommended to use PostgreSQL (or some other popular
database server).
Why use NGINX and Gunicorn with Django?
We can conclude from the above text that we cannot use Django’s
development server in production as it lacks security and performance
features, which is why NGINX and Gunicorn come into play.
2 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Gunicorn is a WSGI (Web Server Gateway Interface) HTTP Server used
with web applications and frameworks wri�en in the Python
programming language.
WSGI descendent of CGI is an interface that sits in between the web
server and Django to allow them to communicate with each other.
NGINX is used to provide additional features like load balancing,
caching, and other features.
Steps for installing Django with NGINX, Gunicorn, &
PostgreSQL
Follow the steps below to install and configure Django with NGINX,
Gunicorn, and PostgreSQL.
Step 1: Installing required packages with the apt package manager
Before we install all the packages, update your apt package manager
index by executing the following a pt command with s u d o :
sudo apt update
To install all the required packages, execute the following command:
3 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
sudo apt install nginx python3-dev python3-pip python3-venv postgresql postgresql-
Now that we have all the required packages installed, we can proceed to
further steps.
Step 2: Create a user and database in PostgreSQL
To enter into PostgreSQL interactive shell execute:
sudo -u postgres psql
Now to create a user and database enter:
CREATE USER djangouser WITH PASSWORD 'mypassword';
CREATE DATABASE djangoproject;
Creating PostgreSQL user and database
Django expects the parameters listed here for its database connections,
enter the following inside PostgreSQL interactive shell to set these
parameters.
4 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
ALTER ROLE djangouser SET client_encoding TO 'utf8';
ALTER ROLE djangouser SET default_transaction_isolation TO 'read committed'
ALTER ROLE djangouser SET timezone TO 'UTC';
Django would work fine without these parameters but, it will require
making additional queries, so Django recommends doing it in hand
before. Now grant all the privileges of the djangoproject database to the
djangouser by entering:
GRANT ALL PRIVILEGES ON DATABASE djangoproject TO djangouser;
and quit the interactive session by entering \q in the PostgreSQL
interactive shell.
Step 3: Creating a Python virtual environment
We will create our virtual environment inside a new directory. To create a
new directory execute the mkdir command:
mkdir ~/newProject
and, change directory by executing the cd command:
5 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
cd ~/newProject
Now we create a new virtual environment inside our directory newProject
called django_venv by executing:
python3 -m venv django_venv
To activate our newly created virtual environment execute:
source django_venv/bin/activate
And your shell prompt should change as shown in the image below.
Activating Virtual Environment
Step 3: Installing required packages with pip and configuring Django
To install Django framework, Gunicorn for WSGI and psycopg2 is required
for PostgreSQL execute the following pip command:
6 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
pip install django gunicorn psycopg2
Now let’s create a sample project called djangoproject with Django.
To create a new project execute:
django-admin startproject djangoproject .
The period at the end of the command will create the project in the same
directory.
You can find the Django se�ings at ~/newProject/djangoproject
/settings.py , you can edit them using your favorite text editor.
nano djangoproject/settings.py
And add your server domain or IP under ALLOWED_HOSTS
1 . . .
2 ALLOWED_HOSTS = ['your_server_domain_or_ip', 'your_server_domain_or_ip', . . .
3 . . .
7 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Add allowed hosts
The following changes are needed to be made in your settings.py of your
project to use the PostgreSQL database:
1 . . .
2 DATABASES = {
3 'default': {
4 'ENGINE': 'django.db.backends.postgresql_psycopg2',
5 'NAME': '<db_name>',
6 'USER': '<db_user>',
7 'PASSWORD': '<db_password>',
8 'HOST': 'localhost',
9 'PORT': '',
10 }
11 }
12 . . .
8 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Make changes in s e � i n g s . p y for Django to make use of PostgreSQL
Next, add the import os at the beginning of settings.py and then define
the STATIC_ROOT path in settings.py, this is where the static files of your
website will be generated and stored. It is necessary for NGINX to
function.
import os
. . .
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
. . .
Add S TAT IC _ RO OT path in s e � i n g s . p y
Save the file. Now to migrate the database, execute:
9 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
1 python manage.py migrate
Migrating Database
You are required to create an administrative user to use the Django web
admin interface, execute the following command to create:
python manage.py createsuperuser
You will be required to answer several prompts (Username, Email,
10 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Password).
Create Superuser
Now allow port 8 0 0 0 in the firewall for the Django Development Server to
be accessible, by executing:
sudo ufw allow 8000
Finally, we can test our new project with the help of the Django
Development Server by executing:
python manage.py runserver server_ip:8000
Django Development Server Running
Now you can visit your browser at h � p : / / s e r v e r _ i p : 8 0 0 0 / to test out your
newly created Django project.
11 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Django project homepage
You can also visit the admin page at h � p : / / s e r v e r _ i p : 8 0 0 0 / a d m i n / and
enter credentials of the admin user we created above.
12 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Django Admin Interface
To test out Gunicorn’s ability to serve pages with your Django application,
execute:
1 gunicorn --bind 0.0.0.0:8000 djangoproject.wsgi
Testing out Gunicorn’s ability to serve webpages
Now go back to your browser and go to h � p : / / s e r v e r _ i p : 8 0 0 0 / to test it
out. You should be able to see the same page.
Press C T R L + C to stop the Gunicorn server and return to the terminal.
Step 4: Create a systemd service file for Gunicorn
Using your favorite text editor, create gunicorn.service file at
/etc/systemd/system/ by executing:
13 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
1 sudo nano /etc/systemd/system/gunicorn.service
Then make the following changes in the gunicorn.service file, and make
sure to replace your details below:
1 [Unit]
2 Description=gunicorn daemon
3 After=network.target
4
5 [Service]
6 User=myusername
7 Group=www-data
8 WorkingDirectory=/home/myusername/newProject
9 ExecStart=/home/myusername/newProject/django_venv/bin/gunicorn --access-logfi
10
11 [Install]
12 WantedBy=multi-user.target
Then execute the following commands to start and enable Gunicorn
service:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
14 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
You can found out the status of Gunicorn service by executing:
sudo systemctl status gunicorn
Gunicorn Service Status
Step 5: Configure NGINX reverse proxy
We have Gunicorn set up, now we need to set up NGINX reverse proxy to
Gunicorn.
Create a new file by executing:
sudo nano /etc/nginx/sites-available/gunicorn
15 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
And then add the following and replace them with your details:
1 server {
2 listen 80;
3 server_name your_server_domain_or_IP;
4
5 location = /favicon.ico { access_log off; log_not_found off; }
6 location /static/ {
7 root /home/myusername/newProject;
8 }
9
10 location / {
11 include proxy_params;
12 proxy_pass http://unix:/home/myusername/newProject/djangoproject.sock
13 }
14 }
NGINX Reverse Proxy Configuration
Save the file.
Then create a symlink to enable NGINX virtual host, by executing:
sudo ln -s /etc/nginx/sites-available/gunicorn /etc/nginx/sites-enabled/
16 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
To test your NGINX configuration for errors, execute:
sudo nginx -t
NGINX configuration test successful
Finally restart the NGINX service by executing:
sudo systemctl restart nginx
You can now close port 8000 and allow NGINX ports, by executing:
sudo ufw delete allow 8000
sudo ufw allow 'Nginx Full'
Now visit h � p : / / y o u r _ s e r v e r _ d o m a i n _ o r _ i p / to test out your Django set
up with NGINX, Gunicorn, & PostgreSQL.
17 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Django Webpage
Congratulations! We have successfully set up Django with NGINX,
Gunicorn, and PostgreSQL.
Conclusion
Django is a great Python web framework for developing web applications,
it comes with a handy development server that shall never be used in a
production environment. In production environments, a setup similar to
what we have shown above must be used. In this article, we have set up a
Django application with PostgreSQL for the database, Gunicorn for WSGI,
and NGINX as a reverse proxy.
We hope you found this tutorial helpful. Thank you for reading! �
18 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
← Previous Post Next Post →
Search …
19 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Recent Posts
Navigating the Linux Landscape: Picking the Right Distribution
Torrhunt – A simple Torrent search Engine & Browser
Distro-Hopping in 2023: Outdated Practice or Open-Source Adventure?
Step-by-Step Guide to Uninstalling Unused Packages on Linux
M a s t e r i n g G N O M E : Re n a m i n g A u d i o D e v i c e s i n t h e Q u i c k S e � i n g s M e n u
Favorite Sites
Python Tutorials
GoLang Tutorials
CodeForGeek
VM-Help
MySQL Tutorials
Excel Tutorials
20 of 21 15/06/2023, 8:13 PM
How to install Django with NGINX, Gunicorn, and PostgreSQL on Ub... https://www.linuxfordevices.com/tutorials/ubuntu/install-django-nginx-...
Copyright © 2023 · LinuxForDevices · LinuxForDevices is part of JournalDev IT Services Private
Limited
Linux® is a registered trademark of Linus Torvalds.
21 of 21 15/06/2023, 8:13 PM