Day 1/100 DevOps Projects 1: Deployment Django App and Setting Up on AWS server
Launching an AWS Instance
Log in to your AWS Management Console: Access your AWS account through the web-based management console provided by Amazon Web Services.
Navigate to the EC2 service: EC2 (Elastic Compute Cloud) is a web service that provides resizable compute capacity in the cloud. Navigate to this service to launch virtual servers, known as instances, on the AWS infrastructure.
Click on "Launch Instance" to begin the instance creation process: Begin the process of launching a new virtual server instance.
Choose an Ubuntu Amazon Machine Image (AMI) from the available options : Select an operating system image for your virtual server. In this case, choose an Ubuntu AMI, which is a pre-configured template for an Ubuntu operating system.
Select an instance type based on your requirements and click "Next: Configure Instance Details": Choose the hardware configuration for your instance, including the number of virtual CPUs, amount of memory, and network performance.
Configure instance details such as network settings, subnets, and security groups. Ensure that SSH (port 22) and HTTP (port 80) are open: Customize network settings, such as the virtual private cloud (VPC) and subnet, and define security groups to control inbound and outbound traffic. Ensure that SSH (port 22) and HTTP (port 80) are open to allow remote access and web traffic, respectively.
Continue through the wizard, adding storage, tags, and configuring any additional settings as needed.
Review your instance configuration and click "Launch".
Select an existing key pair or create a new one to secure SSH access to the instance. Locate the .pem key file: Find the private key file (.pem) associated with the key pair selected during instance launch. This key is required for SSH authentication.
- Set the Custum TCP for the internet usage with 0.0.0.0 port of 8000
Accessing the Instance and Setting Up Django
Install Django and its dependencies:
sudo apt update sudo apt install python3-django sudo apt install python3 python3-pip -y
Verify the installation of Python and pip:
python3 -V pip3 -V
Install virtualenv:
sudo pip install virtualenv
Create a directory for your Django project and navigate into it:
mkdir project cd project
Create a virtual environment:
virtualenv newenv
Activate the virtual environment:
source newenv/bin/activate
Install Django within the virtual environment:
pip install django
Perform database migrations:
python manage.py migrate
Run the Django development server:
python manage.py runserver 0.0.0.0:8000
- Verify that the server is running by checking the output of the command
lsof -i:8000
.
Setting Up Django Application with Docker
- Install Docker on the AWS instance: Install Docker Engine, a platform for developing, shipping, and running applications inside containers, on the AWS instance.
sudo apt install docker.io
- Create a Dockerfile:
vi Dockerfile
- Add the following content to the Dockerfile:
FROM python:3
RUN pip install django==5.0
COPY . .
RUN python manage.py migrate
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
- Save and exit the Dockerfile by typing
:wq
and pressing Enter.
Build the Docker image:
- Install Docker Engine: Use the
docker build
command to build a Docker image namedtodo-app
based on the instructions in the Dockerfile.
sudo docker build . -t todo-app
- Check the Docker processes: Use the
docker ps
command to list running Docker containers and verify that the Docker daemon is active.
sudo docker ps
- Run the Docker container: Start a Docker container based on the
todo-app
image, mapping port8000
from the container to port8000
on the host system.
sudo docker run -p 8000:8000 todo-app