Deploy a Django (Python) for free on Oracle cloud [Free Hosting]

This article will provide you with step-by-step instructions on How to Deploy a Django Application for free on the Oracle cloud.

After reading this article, you will have the knowledge and skills necessary to build your own cloud VPS in Oracle Cloud.

How to Install WordPress with OpenLiteSpeed on Ubuntu 20.04

What Are Always FREE Cloud Services?


2 AMD based Compute VMs with 1/8 OCPU and 1 GB memory each.

4 Arm-based Ampere A1 cores and 24 GB of memory usable as one VM or up to 4 VMs.

2 Block Volumes Storage, 200 GB total.

10 GB Object Storage.

10 GB Archive Storage.

Resource Manager: managed Terraform.

5 OCI Bastions.


Your choice of Oracle Autonomous Transaction Processing, Autonomous Data Warehouse, Autonomous JSON Database, or APEX Application Development. Two databases total, each with 1 OCPU and 20 GB storage.

NoSQL Database with 133 million reads per month, 133 million writes per month, 25 GB storage per table, up to 3 tables.

More Details:

Step 1: Sign-Up For A FREE Account?

It takes about 10 minutes to fully provision your account, but once it is completed, you’ll receive an email informing you that it’s ready to go. You can log in with your email and password at the following URL:

Step 2: Login into Oracle Cloud Console?

After you’ve successfully logged in, you will land on the OCI Console:

Step 3: Create A FREE Compute Instance (Linux) [Free Hosting Django, Python]

Go to the Navigation Menu under Core Infrastructure, click on ‘Compute’, and then Instance.

Step 4: Configure the always free instance

Select ‘Compartment’, & click on ‘Create Instance’. Fill out the required information.

You can change the image source by clicking on ‘Change Image Source’. Also, click on ‘Show Shape’, and Network & Storage Options for more options.

Click on “change image” to Change the Linux Distribution of your choice

Create new virtual cloud Network

Generate & download new SSH key pairs

Specify a custom boot volume size and click on Create


A server running Ubuntu 20.04.
A valid domain name pointed with your server IP.
A root password is configured on your server.

Install Required Packages

First, you will need to install Nginx and other Python dependencies on your server. You can install all the packages with the following command:

apt-get install python3-pip python3-dev libpq-dev curl nginx -y

Once all the packages are installed, start the Nginx service and enable it to start at system reboot:

systemctl start nginx
systemctl enable nginx

Install and Configure PostgreSQL

Next, you will need to install the PostgreSQL server on your server. You can install it with the following command:

apt-get install postgresql postgresql-contrib -y

After the installation, log in to the PostgreSQL shell with the following command:

su - postgres

Next, create a database and user for Django with the following command:

	WITH PASSWORD 'password';

Next, grant some required roles with the following command:

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';
	ON DATABASE djangodb 
	TO djangouser;

Next, exit from the PostgreSQL shell using the following command:


Create a Python Virtual Environment

Next, you will need to create a Python virtual environment for the Django project.

First, upgrade the PIP package to the latest version:

pip3 install --upgrade pip

Next, install the “virtualenv” package using the following command:

pip3 install virtualenv

Next, create a directory for the Django project using the command below:

mkdir ~/django_project

Next, change the directory to django_project and create a Django virtual environment:

cd ~/django_project
virtualenv venv

Next, activate the Django virtual environment:

source venv/bin/activate

Next, install the Django, Gunicorn, and other packages with the following command:

pip install django gunicorn psycopg2-binary

Install and Configure Django

Next, run the following command to create a Django project: startproject django_project ~/django_project

Next, edit the and define your database settings:

nano ~/django_project/django_project/

Find and change the following lines:

ALLOWED_HOSTS = ['', 'localhost']
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'djangodb',
'USER': 'djangouser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
STATIC_URL = '/static/'
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Save and close the file then migrate the initial database schema to the PostgreSQL database:

python makemigrations
python migrate
Next, create an admin user with the following command:
python createsuperuser

Set your admin username and password as shown below:

Username (leave blank to use 'root'): admin
Email address:
Password (again):
Superuser created successfully.
Next, gather all the static content into the directory
python collectstatic

Test the Django Development Server
Now, start the Django development server using the following command:

python runserver

You should see the following output:

Watching for file changes with StatReloader
Performing system checks…
System check identified no issues (0 silenced).
June 22, 2021 - 11:15:57
Django version 3.2.4, using settings 'django_project.settings'
Starting development server at
Quit the server with CONTROL-C.
Enable firewalld

This makes sure that firewalld will be started automatically with the server.

sudo apt install firewalld
systemctl enable firewalld
Start firewalld

After the firewalld service is enabled, you’ll need to start it manually the first time. This is how you would manually start firewalld if it were not already running.

systemctl start firewalld

systemctl status firewalld

Add a Port for Firewall

You do have to specify TCP or UDP and open a port for both. You will need to add rules for each protocol.

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=8000/tcp
Restart firewalld

If for some reason, you need to restart the service, you can do that with the systemctl restart command.

systemctl restart firewalld

Open your browser and browse to the server IP on port 8088 to view the default page: https://your-ip-address:8000

Now, open your web browser and access your Django app using the URL http://ip_address:8000/admin/.

You will be redirected to the Django login page:


django admin login page
Provide your admin username, password and click on the Login. You should see the Django dashboard on the following page:

django admin dashboard
Now, go back to your terminal and press CTRL + C to stop the Django development server.

Test Gunicorn
Next, you will need to test whether the Gunicorn can serve the Django or not. You can start the Gunicorn server with the following command:

gunicorn --bind django_project.wsgi

If everything is fine, you should get the following output:

[2021-08-09 10:20:02 +0000] [12820] [INFO] Starting gunicorn 20.1.0
[2021-08-09 10:20:02 +0000] [12820] [INFO] Listening at: (11820)
[2021-08-09 10:20:02 +0000] [12820] [INFO] Using worker: sync
[2021-08-09 10:20:02 +0000] [12822] [INFO] Booting worker with pid: 12822
Press CTRL + C to stop the Gunicorn server.

Next, deactivate the Python virtual environment with the following command:

Create a Systemd Service File for Gunicorn
It is a good idea to create a systemd service file for the Gunicorn to start and stop the Django application server.

To do so, create a socket file with the following command:

nano /etc/systemd/system/gunicorn.socket

Add the following lines:

Description=gunicorn socket

Save and close the file then create a service file for Gunicorn:

nano /etc/systemd/system/gunicorn.service

Add the following lines that match your Django project path:

Description=gunicorn daemon
ExecStart=/root/django_project/djangoenv/bin/gunicorn --access-logfile - --workers 3 --bind
unix:/run/gunicorn.sock django_project.wsgi:application

Save and close the file then set proper permission to the Django project directory:

chown -R www-data:root ~/django_project

Next, reload the systemd daemon with the following command:

systemctl daemon-reload

Next, start the Gunicorn service and enable it to start at system reboot:

systemctl start gunicorn.socket
systemctl enable gunicorn.socket

To check the status of the Gunicorn, run the command below:

systemctl status gunicorn.socket

You should get the following output:

● gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-08-09 10:05:05 UTC; 3min 7s ago Triggers: ● gunicorn.service
Listen: /run/gunicorn.sock (Stream)
CGroup: /system.slice/gunicorn.socket
Aug 09 10:05:05 django systemd[1]: Listening on gunicorn socket.

Configure Nginx as a Reverse Proxy to Gunicorn Application
Next, you will need to configure Nginx as a reverse proxy to serve the Gunicorn application server.

To do so, create an Nginx configuration file:

nano /etc/nginx/conf.d/django.conf

Add the following lines:

server {
listen 80;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/django_project;
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;

Save and close the file then verify the Nginx for any configuration error:

nginx -t


nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx service to apply the changes:

systemctl restart nginx

Now, you can access the Django application using the URL


In the above guide, you learned how to deploy a Django application with Gunicorn and Nginx as a reverse proxy. You can now use this setup in the production environment for deploying secure and high-performance Python applications.

Leave a Comment