How to host a Django website with Apache, Postgres and Ubuntu.

As we all know django is a python web framework which comes with inbuilt server which we can use to test our code locally on where will redirect the browser request to our local machine and 8000 is the port number on which our django inbuilt server is running by default. But once our code is ready for production, we need a more secure and powerful web server to host our website. In this tutorial we will help you to host a django website with Apache web server software and Postgres database on Ubuntu. Let's get started.


Step 1. Before moving into any software installation on our server, it's a good practice to update all the inbuilt components.

Run the following command.

sudo apt-get update


Step 2. Now we will install python pip, apache and mod_wsgi for Apache using following command

sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

libapache2-mod-wsgi-py3 will help to host our django application using Apache.


Step 3. Install Postgresql

sudo apt install postgresql postgresql-contrib


Step 4. Create Database in postgresql.

i) Login to Postgres environment

sudo -u postgres psql

ii) To created database

CREATE DATABASE databasename;

iii) Create a user

CREATE USER username WITH PASSWORD 'password';

iv) Set client encoding to utf8

ALTER ROLE username SET client_encoding TO 'utf8';

v) Setting the default transaction isolation scheme to “read committed”

ALTER ROLE username SET default_transaction_isolation TO 'read committed';

vi) Setting time zone to 'Asia/Kolkata'

ALTER ROLE username SET timezone TO 'Asia/Kolkata';

vii) Grant all database privileges to the user created


Remember: replace username with your username and databasename with the database you have created.


Step 5. Installing a virtual environment

sudo pip3 install virtualenv


Step 6. Make a directory for this project. here we are naming the directory as django

mkdir django


Step 7. Move into the new directory and create a virtualenv.

virtualenv myprojectenv

and activate this environment

source myprojectenv/bin/activate


Step 8. In this virtual environment, install django

pip install django


Step 9. Clone your project from git by using below command

git clone "<username>/<repository name>.git"

if you want to create a new project, you can follow the same django commands to create django project and apps.


Step 10. Changes to do in your file.

i) Change your database settings as below - type in your databasename, username and password created in step Step 4.


    'default': {

        'ENGINE': 'django.db.backends.postgresql_psycopg2',

        'NAME': 'databasename',

        'USER': 'username',

        'PASSWORD': 'password',

        'HOST': 'localhost',

        'PORT': '',




ii) Change the allowed host settings.

ALLOWED_HOSTS = ['', 'or you can give your host ip']


iii) Securing our project Secret_key

Create a file in your directory where is located


once the file opens paste your project secret_key in below format

SECRET_KEY = 'copy your secret key from and paste here'

Now open your and at the bottom of this file add the below code


    from .local import *

except ImportError:



it will import the Secret_key from file in the


Step 11. Run your server on port 8000

python runserver

Check in browser your website must be running on port 8000.


Step 12. Now we will run our website on port 80 (all http request search for port 80 by default) using libapache2-mod-wsgi-py3

Run the below command and copy the content inside this file and keep it safe in textpad for your future reference.

sudo vi /etc/apache2/sites-available/000-default-le-ssl.conf

edit this file as below


<VirtualHost *:80>


DocumentRoot /root/django/ your-django-projectname

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /root/django/your-django-projectname>


Require all granted



WSGIDaemonProcess myproject python-path="path where python is installed" python-home="path of your virtualenv"

WSGIProcessGroup your-django-projectname

WSGIScriptAlias / /root/django/your-django-projectname/



Now your website is up and running on port 80. Please leave a comment below if you have any questions.

Published : July 5, 2020