Open In App

How to Deploy Django Application in AWS EC2?

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will study how we can deploy our existing Django web application to Windows Server in AWS EC2. We will also see how to use the public IP of the EC2 instance to access the Django application. For this article, you should know about setting up EC2 in AWS. We will see how to deploy applications on Linux As Well as Windows OS.

Introduction to Django

Django is a Python framework for developing and building web applications in Python. It supports rapid development and Scalability. It follows MVC architecture for developing web apps. You can read more about Django in this article what is Django?

Benefits of Deploying Django Applications on EC2

  • Amazon EC2 provides a simple and efficient option for the deployment of applications
  • It supports high scalability and throughput. It can be scaled up or down as per requirement.
  • Supports different OS for deployment.

Steps to Deploy Django Application in AWS EC2 with Ubuntu Server

Step 1: First, we have to create an Amazon EC2 instance running any Linux distribution of your choice. Under launch instance on AWS give your instance a name and select Ubuntu Server as OS. Specify other options as per your choice.

Ubuntu OS

Step 2: Make sure you allow HTTP traffic to the instance . You can also allow HTTPS if you want.

configure ec2

Step 3: After specifying all options correctly click launch instance. Once the instance is started successfully copy the public IP address assigned you will need it later. You may have assigned an elastic ip address which is also fine. Connect to the instance using PuTTY or OpenSSH.Once you connect to the instance download and install Python and other Django requirements according to your project. Ubuntu may have already installed Python. If it is not there install it using the below command.

sudo apt install python3

Step 4: Install nginx using the below command.

sudo apt install nginx
  • After installing Python add your project to the instance or you can create a new project. For the tutorial, I have created a simple Django project that displays “Hello World” on the home page route.
  • Here is the views.py file for the project

hello world

  • Below is urls.py for the project

django frame work

Step 6: Start the server to test the application. Note down the address at which the server is started by default it is 127.0.0.1:8000. You should be able to access the page at 127.0.0.1:8000/home.

GFG-Instance---ec2-3-85-221-32compute-1amazonawscom---Remote-Desktop-Connection-10-08-2023-14_37_08-(2)

Troubleshooting

  • If you can’t access the landing page make sure your Nginx server is allowed on the firewall. If not add Nginx to the allowed apps in the firewall.
  • If you get an access denied error make sure HTTP access is enabled for the EC2 instance.

FAQs On Django Application in AWS EC2

1. Can We Deploy Django On Lambda?

Yes AWS Lambda is a serverless compute service that runs code in response to events. This means that your Django application will only be running when it receives an event, such as a HTTP request. This can be a good thing for cost savings, but it can also mean that your application will have a cold start latency, which is the time it takes for the code to be loaded and executed.

2. What Is Elastic Beanstalk VS EC2?

Both are the services offered by the Amazon Web Serives and both the services will helps you to deploy the applications but some of them had there own weakness and strength.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads