A complete guide to hosting your Django project on AWS EC2 Instance for free for a year


A complete guide to hosting your Django project on AWS EC2 Instance for free for a year. Blockchain world one of the best blogs on blockchain niche.
A complete guide on hosting your Django project on AWS EC2 for free (AWS EC2 + Django + Apache2 + Mod WSGI)

Introduction

We all have wondered how to host our django project for free. Today we are going to host a django project on Amazon web services using their EC2 instance.
We will use the free tier of AWS, which is available for the first year after signing up with AWS. For Server, Apache2 server along with mod wsgi will be used to host our django project and your future blockchain projects based on django.

Steps to Host Django on AWS EC2 (Quick Links)

1. Creating an Instance 🤑

2. Connecting to our Instance 🔐

3. Installing Apache2 server 📤

4. Installing Python and mod wsgi 🐍

5. Creating a Virtual Environment 🌲

6. Creating a Django Project 🦾

7. Running the Django project on Localhost 🧩

8. Hosting on Apache2 Server 📱


1. Creating an Instance 🤑

Our first step would be creating an EC2 instance in amazon web services.


Here I am assuming that you guys have done the signup process. You merely need to verify your payment method and you will be logged into the console of AWS.


After you are on the console page of AWS. You need to search for EC2 in the search bar which is present in the top left part of the page in the navigation bar.

steps on how to launch an instance in AWS EC2. Blockchain world one of the best blogs on blockchain niche
Search EC2 in the top search bar and click on it.

After selecting the EC2 in the search bar, we will be redirected to the EC2 console page.


Now we need to Click on launch Instance to Launch our AWS EC-2 Instance.


Note: If in the future, the contents of the AWS console are changed (UI is changed), we need to select the instances option in the left navigation bar and then click on launch instance which will be present in the top left corner of the page.


Select Launch Instance. Creating an instance in aws ec2 for django project hosting in aws
Select Launch Instance

Now we are on the Launch Instance Page. Here, we are given options to select the OS needed for our EC-2 instance.


In this tutorial, we are selecting Ubuntu 20.04 LTS 64bit (x86) Server to host our Django website.

Select Ubuntu Server 20.04 LTS. Creating a server instance in aws elastic compute 2. Blockchain world
Select Ubuntu Server 20.04 LTS

Next, we are selecting micro instance as it is available in the free tier of AWS.


You can select whichever type of instance you require according to your needs and budget.

Select the Micro Instance which comes under free tier of AWS EC2. blockchain world one of the best blogs on blockchain niche
Select the Micro Instance which comes under free tier of AWS EC2

Now we will directly go to Configure Security Group to edit some inbound configurations for our aws ec2 instance.

Click on Configure Security Group. Blockchain World one of the best blogs on blockchain niche
Click on Configure Security Group

Now we need to add three inbound rules in order for our server to serve the website to us.

  1. HTTP inbound in port 80 (HTTP option)

  2. HTTPS inbound in port 443 (HTTPS option)

  3. Custom TCP Rule inbound in port 8000 to check our hosted django project on localhost. (Custom TCP rule and add 8000 in port range)

Click on add rule and select the HTTP option for the first time, then select the HTTPS option and for the third time, select the Custom TCP Rule


Inbound Rules Setting. Blockchain World one of the best blogs on blockchain niche
Select the HTTP (80), HTTPS (443) AND CUSTOM TCP RULE (8000)

This is the screenshot of what the final configuration should look like. After that, click on Review and Launch.

Configure Security Group. Blockchain World one of the best blogs on blockchain niche
Inbound Rules

Check all the descriptions of the instance selected by us. Your review and launch page should look something like this.

If all the details are correct, you can click on the Launch button at the bottom right corner of the webpage.

AMAZON AWS EC2 instance creation Blockchain world one of the best blogs on blockchain niche
Review the information and Click on Launch
AWS EC2 instance creation for free. Blockchain projects and django projects
Click on Launch

When we will click on the Launch Button, a Create key pair window will open.


Select the following options:

  1. Create a new key pair

  2. Select the RSA option

  3. Type a name for the key

Click on the Download key pair to download the .pem file.

Type the name in create new key pair and Click on Download key pair. Blockchain world. How to create an ec2 instance and host django on ec2
Type the name in create new key pair and Click on Download key pair

Click on Launch Instances after typing the information mentioned above to start the AWS EC2 instance.

how to launch an ec2 aws instance for hosting django
Click on Launch Instances

The Following Screen would be shown to us.

Our Instance is being Created.
Our Instance is being Created

2. Connecting to our Instance 🔐


We have now created our instance. Now we need to connect to our server which we have created.

When the Instance state turns into a running state, We need to Click on the Instance ID of our instance to which we want to connect.

Connecting to the aws ec 2 instance. Blockchain world
Click on the instance ID

Click on the Connect Button.

Note: Connect button will only turn clickable when the instance state is running

Click on the Click Button. Blockchain world one of the best blogs on blockchain niche
Click on the Click Button

Again Click on the Connect Button.

Click on the Connect Button
Click on the Connect Button

In a new tab, a terminal of the instance will open. From this tab, we can easily control our AWS EC2 instance.

If you have this tab open, Cheers to you. We have successfully connected to our aws ec2 instance from our pc.

Tab which can be used to control our instance
Tab which can be used to control our instance

3. Installing Apache2 server 📤


To install the apache2 server on the AWS ec2 instance, we need to type the following commands. You can directly copy these commands and paste them on your terminal.



sudo su
apt update 
apt install apache2
 

  • The sudo su command makes us the root user.

  • apt update is used to update the applications

  • The last command would be used to install apache2 server on our instance.


Installing apache2 server on the ubuntu 20.04 LTS
Installing apache2 server on the ubuntu 20.04 LTS

Now we will allow Apache Full profile to enable HTTP and HTTPS protocols on the server.


sudo ufw app info "Apache Full" 
sudo ufw allow in "Apache Full" 

Allowed Apache Full Profile in ubuntu blockchain world
Allowed Apache Full Profile

Now we will go to the public IP address of our instance which can be accessed from the aws ec2 instance console. Click on the open address hyperlink.


This link can be accessed by anyone from any part of the world (provided that they are connected to the internet).

Click on open address hyperlink. Blockchain world one of the best blogs on blockchain niche
Click on the open address hyperlink

Go to the http://your_public_ip_address/.


Note: remove s from the https in https://your_public_ip_address/


We don't have an SSL certificate yet so we are only using HTTP protocol right now. Hence, we can only access the website in the non secure HTTP mode.


go to http address and not https. Installing apache in aws
go to http address and not https

We must get this Apache2 Ubuntu default page. If this page is shown, we have successfully installed apache2 in our instance.

Apache2 ubuntu default page. Blockchain world
Apache2 ubuntu default page

4. Installing Python and mod wsgi 🐍

After installing the Apache2 server, we need to install python and mod wsgi in our ec2 instance.

We need to follow the following steps.

  1. We will change our directory to /var/www

  2. Create a directory named blockchainworld

  3. Change our directory to blockchainworld

Paste the commands given below to do so.


Note: you can follow my naming conventions for future ease in copying codes


cd /var/www
mkdir blockchainworld
cd blockchainworld

We need to be in the /var/www/blockchainworld folder

To install Python 3, use the following command:


sudo apt install python3-pip

To install mod wsgi use the following command:


sudo apt install libapache2-mod-wsgi-py3

Installing python 3 and mod wsgi in aws ec2 instance
Installed python 3 and mod wsgi

kudos to you guys🎈, We have completed step 4. Let's move on step 5.


5. Creating a Virtual Environment 🌲

Now we have installed python 3 on our server, we need to create a virtual environment in our ec2 instance so that the future libraries installed on the server don't affect our project.


To install virtualenv type the following command.


I am assuming you are using the root user. If not use sudo su to login as the root user.



apt install python3-virtualenv

Installing virtualenv in our server. Creating a virtual environment in aws EC2 instance.
Installing virtualenv in our server

Next, we will create a virtual environment named blockchainworld in the /var/www/blockchainworld directory.


virtualenv blockchainworld

Blockchain world one of the best blogs on blockchain niche
Creating a Virtual Environment named blockchainworld

Use the following mentioned command to activate the blockchainworld environment.


source blockchainworld/bin/activate

Activate the blockchainworld Environment

6. Creating a Django Project 🦾

After creating the virtual Environment let's create a Django Project.

I am naming my project blockchainworldproject


To install Django on our ec2, we need to type the following command:


pip3 install django

command to install django in ubuntu 20.04 lts. aws ec2 instance
pip3 install django

To create the project in the /var/www/blockchainworld folder type the following command:


django-admin startproject blockchainworldproject . 

Important: We have created the project in the same directory, so we have used . at the end of the command.

creating a django blockchain project in same directory
Creating the project named blockchainworldproject in the same directory

Once the project is created, we need to make some changes in the settings.py file. To access the settings.py file in vim editor, type the following command.

cd blockchainworldproject
vi settings.py 

# press i to enter insert mode and type

Now we need to make three changes in the settings.py file:

  1. add import os at the top (as shown in the image)

  2. add the public IP address of our instance in the allowed hosts

  3. add static root path

how to install django project on ec2 instance
1. adding import os

Next, add the public IP address in quotes of allowed hosts.


This public IP address is the same IP address that we have put in the URL to access the apache default ubuntu page.

2. Adding the IP address in allowed hosts. Make django project in aws ec2 instance
2. Adding the IP address in allowed hosts

At the bottom below the STATIC_URL, we need to paste the following code.


STATIC_ROOT = os.path.join(BASE_DIR, "static/") 

3. Adding the static root. creating a django project in aws ec2 instance.
3. Adding the static root

Press ESC + :wq to exit the vim editor. Now we are done with our 6th step.


7. Running the Django project on Localhost 🧩


Our settings are complete in the Django project.


Now we just need to launch our project in localhost to check if everything is working alright.


So let's just go to the /var/www/blockchainworld directory



cd ..

Now we will write the following commands to run the Django project on the localhost



 python manage.py makemigrations
 python manage.py migrate
 python manage.py collectstatic
 python manage.py runserver 0.0.0.0:8000
 
# you can copy all 4 of them directly (no need to paste one by one) 
hosting django on aws ec2. blockchain world
makemigrations and migrate in django
we are hosting on localhost our django project in aws ec2 instance
Collectstatic and runserver on 8000 port.

Now in the tab where we had opened the apache2 server default ubuntu page, we will type :8000 in the url.

This will show the output of port 8000 where our django project is hosted.

change the :8000 to url. hosting django on localhost
change the :8000 in url

If you are getting the below-mentioned screenshot as the result, then VOILA 🎇your Django is now currently hosted on the localhost.


You can use this for testing purposes but this won't be able to handle much traffic. So for that purpose, we will be using an apache server to handle a big user base.

Django is hosted on localhost successfully. hosting django on aws ec 2 instance
Django is hosted on localhost successfully

8. Hosting on Apache2 Server 📱


To Start hosting on the django project on the apache server, we first need to stop the localhost.


Use ctrl + c to stop the localhost of django


now go to


cd /etc/apache2/sites-available 

use vim editor to edit the 000-default.conf file to edit some configconfigurations. We will delete all the contents of the file using (esc+dd) repeatedly.


vi 000-default.conf 

# delete the file contents using esc + dd repeatedly
completely delete the file contents using esc+dd repeatedly. hosting django on apache server in aws ec2 instance
completely delete the file contents using esc+dd repeatedly

You can directly copy the contents of 000-default.conf file from the code given below.

This will only work if you have used naming conventions same as mine.

 
<VirtualHost *:80>

# The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    #DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn


      Alias /static /var/www/blockchainworld/static
    <Directory /var/www/blockchainworld/static>
        Require all granted
    </Directory>

    <Directory /var/www/blockchainworld/blockchainworldproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess blockchainworld python-path=/var/www/blockchainworld python-home=/var/www/blockchainworld/blockchainworld
    WSGIProcessGroup blockchainworld
    WSGIScriptAlias / /var/www/blockchainworld/blockchainworldproject/wsgi.py




    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


After saving the following contents,


we need to save and exit vim editor using ESC + :wq


Now we need to type the following three commands:


sudo a2dissite 000-default.conf
sudo a2ensite 000-default.conf
sudo service apache2 restart 

saving the 000-default.conf and restarting the server. AWS ec2 hosting django
saving the 000-default.conf and restarting the server

We have finally hosted our Django project. Check on the public IP address. This time, we don't need to check on the port 8000. we can directly check on the ip address.


Still we need to go to the http address and not the https address.

Hurray we have successfully hosted our django project. AWS DJANGO EC2 hosting apache 2
Hurray we have successfully hosted our django project


Conclusion


Congratulations 🎊, We have successfully deployed our Django project on was ec2 instance using apache + python mod wsgi.


If you have any doubts, feel free to ask your doubts in the comment section.







172 views0 comments

Recent Posts

See All