Deploy Django Application In AWS

In my previous articles , I have explained how you can setup Magento stack , LAMP stack and Joomla in AWS.

If you would like to Setup LEMP Stack (Linux , Nginx , MySQL & PHP) on Ubuntu , Refer this Article

 

It will also guides you to setup WordPress website powered by Nginx HTTP Server.

In this guide , We will learn How to setup Django in AWS.

What Is Django?

Django is a free and open source web application framework, written in Python.

It is a web framework that enables developers to rapidly build and scale high performance web applications

Setting UP Django

 

Amazon offers a one click software deployment features along with the Operating system.We can launch an Instance which comes up with a tools such as WordPress , OpenVPN server etc.

 

But In this tutorial , We are going to deploy Django, Which is the combination of App and OS using the Lightsail service.

 

 

What Is Lightsail?

 

If you don’t have much experience with Amazon WEb Services such as VPC , EC2 Instances , Load Balancers , Amazon Lightsail is the best and easy way to start.

 

 

It provides a quick and easy way to deploy and mange instances along with applications such as WordPress , LAMP stack , Drupal and much more.

 

This features is mainly for developers who want to setup a test project to run applications without having much knowledge on managing aws services and also to avoid spending time on installing software or frameworks.

 

Deploying Django

 

To setup Django , Login to Lightsail Console

 

You should see the following page.

 

 

Click Create instance,

 

Choosing Instance Location

 

We have to choose the Instance location , For example , I chose Mumbai

 

 

Also You should choose the Availability Zone.

 

 

Choosing Instance Image

 

Now We have to choose the Operating system , From these two Platforms: Linux and Windows.

 

If you select Linux/Unix , and If you choose OS only ,You have the option to select one of the following operating systems.

 

 

But In this tutorial , As we are Deploying django as a One click software deployment , We choose Apps + OS and then select Django

 

And Optionally You can add the shell scripts which will run while the instance boots up.

 

Under SSH Key pair manager , You can use exiting key pair Or you can create one If you want.

 

To create a New key pair for this instance , Click Create New

 

And Click Create ,

 

 

Type the name for the Key Pair and Download the .pem file.

 

 

 

Choosing Instance Plan

 

You can choose the Instance plan such as RAM , CPU and Hard Disk based on your requirement.

 

 

Provide a name for the Instance , and then add tagging for grouping the resources.

 

 

 

At last , Click Create instance.

 

Once the Instance is Created and Running.

 

If you click the console icon , You will see the below error.

 

 

Lightsail asks us to give a couple of minutes before SSH into the Instance.

 

After a minute or two , If you click the Console icon again , You should be able login to the Instance.

 

Once you login to the instance and check what are the packages and services are installed using netstat command.

You can see that MySQL , Apache2 and PostgreSQL are installed. These are the packages that should be available to run the Django.

 

Deploying Django Project

Lets deploy a sample Helloworld project.

First we need to create new project and new application before deploying the code.

The directory structure of the Django application is as follows.

The Django projects will be created in the following folder:

/opt/bitnami/apps/django/django_projects/{projectname}

The project will be stored in this directory

/opt/bitnami/apps/django/django_projects/{projectname}/{appname}

Lets go to the django_projects directory and create a new project.

/opt/bitnami/apps/django/django_projects

We are going to create a new project named testing.

django-admin.py startproject testing

Go to the project directory and create a new helloworld app.

cd testing
python3 manage.py startapp helloworld

This will create a new folder named helloworld in the current directory.

Then to to the application directory “helloworld” and edit views.py file

Replace the exiting content with the below configurations.

from django.http import HttpResponse


def index(request):
     return HttpResponse("Hello, world")

Save and close the file.

Create urls.py for your application , By creating a new file and adding the below contents.

vi /opt/bitnami/apps/django/django_projects/testing/helloworld/urls.py
from django.urls import path
from . import views

urlpatterns = [
path('', views.index, name='index'),
]

Save and close the file.

Next we are going to modify the urls.py of the project itself. To do that , Go back one folder and open the project folder named testing

cd /opt/bitnami/apps/django/django_projects/testing/testing

Open the urls.py file and replace the existing content with the below.

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('', include('helloworld.urls')),
path('admin/', admin.site.urls),
]

Save and close the file.

By default Django application runs on the port 8000.

To access the Django application We have to allow port 8000 in the firewall , To do so,

Go to the Lightsail console , Select the Django Instance

Choose Networking , Under Firewall,

Click Add rule , Provide the port number : 8000

and then click Create.

Now that we have added all the required ports.

Login to the server and edit the settings.py file.

cd /opt/bitnami/apps/django/django_projects/testing/testing
vi settings.py

In the ALLOWED_HOSTS  ,It will be empty , Replace it with the Public IP address of the Instance.

ALLOWED_HOSTS = ['13.223.204.137']

Save and close the file.

Start The Django Application

To start the Django application “Helloworld”,  Go to the application’s project folder and run the below command.

cd /opt/bitnami/apps/django/django_projects/testing
python3 manage.py runserver 0.0.0.0:8000

To access the application , Open the browser and enter the below url

http://publicipaddress:8000

You should get a response as Hello, world.

Conclusion

 

We have successfully deployed Django on AWS using Lightsail service.

 

 

Hope this article helps you to setup Django at ease and to deploy applications.Thanks for reading this article.

 

Please do check out my other publications.