Django Deployment on AWS
1. Introduction
Deploying a Django application on AWS can be a highly rewarding experience. AWS offers a robust and scalable infrastructure that can handle applications of any size. This tutorial will guide you through the process of deploying a Django application on AWS, from start to finish.
2. Prerequisites
Before you begin, ensure you have the following:
- An AWS account
- Basic understanding of Django
- Basic understanding of AWS services such as EC2 and RDS
- Python and Django installed on your local machine
3. Setting up AWS EC2 Instance
First, we need to set up an EC2 instance which will host our Django application.
Step-by-step guide:
- Log in to the AWS Management Console.
- Navigate to the EC2 Dashboard.
- Click on "Launch Instance".
- Select an Amazon Machine Image (AMI). For this tutorial, we will use the Ubuntu Server 20.04 LTS.
- Choose an Instance Type. The t2.micro instance is sufficient for this tutorial.
- Configure Instance Details. Default settings are fine for now.
- Add Storage. The default 8 GB is sufficient.
- Tag Instance (optional).
- Configure Security Group. Allow SSH, HTTP, and HTTPS traffic.
- Review and Launch. Ensure you download the key pair for SSH access.
4. Connecting to Your EC2 Instance
Once your EC2 instance is running, connect to it via SSH from your local machine.
Example Command:
5. Setting up the Environment
Next, we need to set up the environment on the EC2 instance to run Django.
Install Required Packages:
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
Create a Virtual Environment and Install Django:
sudo -H pip3 install virtualenv
mkdir ~/myproject
cd ~/myproject
virtualenv myprojectenv
source myprojectenv/bin/activate
pip install django gunicorn psycopg2-binary
6. Setting up PostgreSQL
We will use PostgreSQL as our database. Set it up as follows:
Access PostgreSQL:
Create Database and User:
CREATE USER myprojectuser WITH PASSWORD 'password';
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myprojectuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
\q
7. Configuring Django
Modify the Django settings to connect to the PostgreSQL database and prepare for deployment.
Update settings.py:
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
Collect Static Files:
Apply Migrations:
python manage.py migrate
8. Setting up Gunicorn
We will use Gunicorn as the WSGI server to serve the Django application.
Create Gunicorn Systemd Service:
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/myproject
ExecStart=/home/ubuntu/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
Start and Enable Gunicorn:
sudo systemctl enable gunicorn
9. Setting up Nginx
Nginx will act as a reverse proxy to forward requests to Gunicorn.
Create Nginx Configuration File:
listen 80;
server_name your_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
}
}
Enable Nginx Site and Restart Service:
sudo nginx -t
sudo systemctl restart nginx
10. Configuring the Domain Name
If you have a domain name, you can configure it to point to your EC2 instance.
Update DNS Settings:
Log in to your domain registrar's website and update the DNS settings to point to the public IP address of your EC2 instance.
11. Conclusion
Congratulations! You have successfully deployed your Django application on AWS. You can now access your application through the domain name or the public IP address of your EC2 instance.
This tutorial covered the basics of deploying a Django application on AWS. There are many other configurations and optimizations that you can explore to further enhance your deployment.