How to host a django application on Amazon EC2 instance?
By: Sarath Kumar R S
Tags: Django, Ec2 Instance, AWS, Hosting, Ubuntu Server

So in this blog we will be hosting a django application with postgreSQL database on Amazon EC2 instance. So lets get the basics right!

What is server and client?

In simple terms, A program that gives you some kind of service are known as server and the one who takes the service is called client.

For eg. You are a webdeveloper and you have build the website and it is now only available locally but you need to make it public, for making it public you need a program, which renders you that specific service, we can call that program a server. The main user case here is to serve the webpages so the server is called web server. And when it is public and somebody visit the website they are called client. This kind of architecture is called client-server architecture. The protocol client uses here is HTTP(Hyper Text Transfer Protocol)

How to configure web server?

I have created a thumb rule which we can use to configure any server in this world which am going to share here. It is 3 steps process. If you follow this process your server will be configured. Before saying the 3 step process, lemme give you an example. You guys know the vlc player, the audio video player, think that vlc is one kind of server, why? because they are giving service to you. we are the client for the vlc player. If you want to use the vlc player, what all we have to do? first we need to download it, then after you need to set and change the look and feel, you need to set some shortcuts, in short you need to configure it and lastly if you want you want to double click the video/audio to play, it means you need to execute. This same steps you can use to configure any webserver.

Three steps are:

Install Software Configure Execute/Run

Configuring web server on Amazon EC2

Now you guys know how things are working behind the scene and that is enough theory, now let’s get our hands dirty.

I hope you guys know how to start an instance in AWS, which is some couple of button clicks and youare familiar with django concepts. The instance I am configuring here is ubuntu 20.4. As it is ubuntu, amazon allows us to connect through browser, so connect it and follow along with me.

1. Install Apache

Let’s start by updating the local package index with the following command.

sudo apt update

sudo apt upgrade

Install Apache 2 package from the Ubuntu repository.

sudo apt install apache2

This will install apache2 and all required dependencies.

So the apache2 is installed, the first step is finished. Installation of software!

Now we proceed to next step, that is configuring part!yeeesss!

2. Setup firewall

Now you can set up Uncomplicated Firewall (UFW) with Apache to allow public access on default web ports for HTTP and HTTPS

sudo ufw app list

You will see all listed applications.

Output

Available applications:

Apache

Apache Full

Apache Secure

OpenSSH

Apache : This profile opens port 80 (normal, unencrypted web traffic)

: This profile opens port (normal, unencrypted web traffic) Apache Full : This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)If you are not going to use SSL you need to enable only the Apache profile.

: This profile opens both port (normal, unencrypted web traffic) and port (TLS/SSL encrypted traffic)If you are not going to use SSL you need to enable only the Apache profile. Now we will enable Apache Full.

sudo ufw allow 'Apache Full'

Apache Secure : This profile opens only port 443 (TLS/SSL encrypted traffic)

: This profile opens only port (TLS/SSL encrypted traffic) OpenSSH: This profile opens port 22 for SSH access.

With this command you can view the status of UFW.

sudo ufw status

You will see the output as follows.

Output

Status: active

To Action From

-- ------ ----

Apache Full ALLOW Anywhere

OpenSSH ALLOW Anywhere

Apache Full (v6) ALLOW Anywhere (v6)

OpenSSH (v6) ALLOW Anywhere (v6)

If you didn’t see active and seeing inactive, type this command

sudo ufw enable

Once Apache is installed is is started automatically and already be up and running.

Every process in Apache is managed with the systemctl command. Check the status of Apache with the following command.

sudo systemctl status apache2 Output

● apache2.service - The Apache HTTP Server

Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)

Drop-In: /lib/systemd/system/apache2.service.d

└─apache2-systemd.conf

Active: active (running) since Tue 2020-01-06 03:59:34 UTC; 5min ago

Main PID: 10617 (apache2)

Tasks: 55 (limit: 667)

CGroup: /system.slice/apache2.service

├─10617 /usr/sbin/apache2 -k start

├─10619 /usr/sbin/apache2 -k start

└─10620 /usr/sbin/apache2 -k start

Jan 06 03:59:34 apache systemd[1]: Starting The Apache HTTP Server…

Jan 06 03:59:34 apache systemd[1]: Started The Apache HTTP Serve

Now visit the External IP address of your VM instance, you will see the default Apache welcome page.

Cloning the django application on the server

We need to first download all the python packages and configure our webserver

sudo apt install python3-virtualenv

This command will install the required packages for creating our virtual environment.

Next am going to create a django project directory on /srv

cd /srv/

mkdir django

cd django

mkdir projectname

After that create a virtualenv and clone the project file there.

virtualenv venv -p python3

git clone <repository code>

After we clone our repository, we need to configure the postgreSQL database.

sudo apt update

sudo apt install postgresql postgresql-contrib

After that create a db according to the database configured on the settings.py or create a new database and user and mention it on the settings.py file

sudo su postgres

Then i created the database and user there

psql

Enter in to the postgres shell and gave permission for the user to database.

grand all privileges on database <database name> to <database user>;

GRANT- This message will be shown,if it is successful.

So after that you can migrate all your tables and run the application. The application will run on the local server, but to access this app publically we need to configure apache

Configuring Apache

To get everything we need, update your server’s local package index and then install the appropriate packages

sudo apt-get update

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

Then mention the ip address in the allowed host in settings.py

ALLOWED_HOSTS = ["server_domain_or_IP"]

next we need to configure the apache default config file.

sudo vim /etc/apache2/sites-available/000-default.conf

next specify the project directory root addresses, here am using mine as example

<VirtualHost *:80>

. . .



Alias /static /srv/django/bestcareer/src/bescareer/static

<Directory srv/django/bestcareer/src/bescareer/static>

Require all granted

</Directory>



<Directory srv/django/bestcareer/src/bescareer/bestcareer>

<Files wsgi.py>

Require all granted

</Files>

</Directory>



WSGIDaemonProcess myproject python-home=/ srv/django/bestcareer/venv python-path=/srv/django/bestcareer/src/bestcareer

WSGIProcessGroup myproject

WSGIScriptAlias / /srv/django/bestcareer/src/bestcareer/bestcareer/wsgi.py



</VirtualHost>

After this , save the file and run a config test to verify every syntax is clear. To check that use the command

sudo apache2ctl configtest

If it returns syntax ok, then the syntax is okay, if not, check for syantax and clear the mistakes.

So by doing this you have successfully configured a webserver and hosted your first application on AWS.

Cheers!

Sarath Kumar RS

Efuturex Technologies Private Limited.