Day 11 || Deploying a Dockerized Django Application on AWS EC2 with Nginx

Day 11 || Deploying a Dockerized Django Application on AWS EC2 with Nginx

Introduction

The project we'll be working with is a Django application hosted on GitHub under the repository notesapp. This Django app is designed to manage notes, and we'll containerize it using Docker for easier deployment and scaling.

Step 1: Clone the Repository

The first step is to clone the repository to your local development environment. You can use the following Git command:

git clone https://github.com/Hitstar53/notesapp.git
  1. Change Directory to the Cloned Repository:

    Navigate to the folder that was created by the Git clone command. In this case, it's the "notesapp" folder:

     cd notesapp
    
  2. Create a Dockerfile:

    Inside the "notesapp" folder, create a Dockerfile using your preferred text editor or code editor. For example, if you have a text editor like Nano installed, you can use:

     vi Dockerfile
    

    This will open a blank file named "Dockerfile" in the terminal.

Step 2: Create a Dockerfile

To containerize the Django application, we need a Dockerfile. A Dockerfile is a script that defines the environment and instructions for building a Docker container. For this project, we've created a Dockerfile tailored to Django and PostgreSQL. Here's a snippet:

DockerfileCopy code# Use an official Python runtime as a parent image
FROM python:3.8

# Set environment variables for Python
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container at /app
COPY requirements.txt /app/

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Copy the rest of the application code into the container
COPY . /app/

# Expose port 8000 for the Django development server
EXPOSE 8000

# Start the Django development server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

This Dockerfile sets up a Python environment, installs dependencies, and starts the Django server.

Step 3: Build and Run the Docker Container

With the Dockerfile in place, you can now build a Docker image and run a container from it:

bashCopy codedocker build -t my-django-app .
docker run -p 8000:8000 my-django-app

Step 4: Access the Django App

The Django application is now running inside a Docker container. You can access it in your web browser at http://localhost:8000/.

check local host app is running or not

curl -L http://127.0.0.1:8000

nginx web server guide

In this guide, I'll walk you through the process of deploying a Dockerized Django application with Nginx on an EC2 instance. By following these steps, you'll have your Django app up and running behind an Nginx reverse proxy, making it accessible to the web.

Install Nginx on your EC2 Instance

Connect to your EC2 instance via SSH, and then install Nginx using the following commands:

sudo apt update
sudo apt install nginx

Once Nginx is installed, you can start it with:

sudo service nginx start

Copy Static Files to the Web Server Directory

Assuming your Django application's static files are located in a "static" directory within your project folder, you can copy them to the web server's directory using the following commands:

sudo cp -r /path/to/your/django/app/static /var/www/html/

Replace "/path/to/your/django/app/static" with the actual path to your Django app's static files.

Configure Nginx for Reverse Proxy

Next, you'll need to configure Nginx to act as a reverse proxy to your Django application. Open the Nginx default configuration file for editing:

sudo nano /etc/nginx/sites-available/default

Inside the server block, add a location block for your Django application like this:

location / {
    proxy_pass http://127.0.0.1:8000; # Assuming your Django app is running on port 8000

}

location /api {
    proxy_pass http://127.0.0.1:8000/api; #  Django app backend database api

}

Save the file and exit the text editor.

Below this configuration, I'll mention that you should insert a screenshot

Step 4: Test Nginx Configuration and Restart Nginx

Before restarting Nginx, it's a good practice to test the configuration to ensure there are no syntax errors:

sudo nginx -t

If the test is successful, you can safely restart Nginx to apply the changes:

sudo service nginx restart

Step 5: Access Your Django App via Nginx

Your Django application should now be accessible via Nginx at your EC2 instance's public IP or domain name. Open a web browser and enter the following URL:

http://your_ec2_instance_ip_or_domain

Replace "your_ec2_instance_ip_or_domain" with the actual IP address or domain name of your EC2 instance.

That's it! Your Django application is now deployed and accessible through an Nginx reverse proxy on your EC2 instance.

Note:

If you encounter any errors or have questions while practicing this project, please don't hesitate to drop a comment below for assistance.

Did you find this article valuable?

Support Aqib Hafeez(DevOps enthusiast) by becoming a sponsor. Any amount is appreciated!