How to deploy a django application on an Amazon EC2 instance?

Sabeel Km
5 min readFeb 4, 2024

--

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

What is server and client?

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

For eg. You are a web developer and you have built a website that is now only available locally but you need to make it public, To make it public you need a program, that renders you that specific service, we can call that program a server. The main user case here is to serve the web pages so the server is called a web server. When it is public and somebody visits the website they are called a client. This kind of architecture is called client-server architecture. The protocol the client uses here is HTTP(Hyper Text Transfer Protocol)

How to configure a 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. These same steps you can use to configure any web server.

Three steps are:

  1. Install Software
  2. Configure
  3. Execute/Run

Configuring web server on Amazon EC2

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

I hope you guys know how to start an instance in AWS, which is a couple of button clicks, and you're familiar with django concepts. The instance I am configuring here is Ubuntu 20.4. As it is Ubuntu, amazon allows us to connect through a 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 the Apache2 package from the Ubuntu repository.

sudo apt install apache2

This will install apache2 all required dependencies.

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

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

2. Setup firewall

Now you can set up an 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 the port 80 (normal, unencrypted web traffic)
  • Apache Full: This profile opens both ports 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.
  • Now we will enable Apache Full.
sudo ufw allow 'Apache Full'
sudo ufw allow 'OpenSSH'
  • If you allow OpenSSH after your restart you will be able to connect to the server
  • Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
  • OpenSSH: This profile opens the 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 it is started automatically and already 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 apache2Output
● 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

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.

python3 -m venv venv
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

psql

Enter into the Postgres shell and permit the user to access the 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

sudo a2enmod wsgi

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 config file.

sudo nano /etc/apache2/sites-available/bescareer.conf

If you are running on IP, you need to disable the default Apache config file or edit “/etc/apache2/sites-available/000-default.conf” instead of “/etc/apache2/sites-available/bescareer.conf”.

a2dissite 000-default.conf # for disable

next specify the project directory root addresses, here am using mine as an 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

a2ensite name.conf

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 syntax and clear the mistakes.

Then,

service apache2 restart

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

Cheers!

--

--

No responses yet