How to Deploy a Django App on AWS EC2 | Django Deployment Tutorial (2025)
Автор: ProgrammingKnowledge
Загружено: 2025-02-16
Просмотров: 2841
Описание:
🚀 Learn how to *deploy a Django app on AWS EC2 using GitHub and Nginx* in this step-by-step tutorial! We’ll go through setting up an *AWS EC2 instance**, pulling code from **GitHub**, and configuring **Gunicorn & Nginx* to serve your Django app in a production environment.
By the end of this video, you’ll have a *fully deployed Django application* running on AWS EC2 with **a custom domain, HTTPS, and Nginx as a reverse proxy**.
---
*🔹 What You'll Learn in This Video:*
✅ How to launch an *AWS EC2 instance* (Free Tier)
✅ How to *clone a Django project from GitHub* to EC2
✅ How to *set up a virtual environment & install dependencies*
✅ How to configure *Gunicorn to serve the Django app*
✅ How to install & configure *Nginx as a reverse proxy*
✅ How to set up a *firewall & security groups* for secure access
✅ How to run Django *with a PostgreSQL/MySQL database (optional)*
---
*🔹 Prerequisites*
✔️ *AWS Account* ([Sign Up Here](https://aws.amazon.com/free/))
✔️ *GitHub Repository with Django Project*
✔️ Basic knowledge of *Django & Linux Commands*
✔️ *SSH Access to EC2 Instance*
---
*🔹 Step 1: Launch an AWS EC2 Instance*
1️⃣ Log in to *AWS Console* → Go to *EC2*
2️⃣ Click *Launch Instance*
3️⃣ Select *Ubuntu 22.04 LTS* as your AMI
4️⃣ Choose *Instance Type* → Select *t2.micro* (Free Tier)
5️⃣ Configure **Security Group**:
Allow *SSH (port 22)*
Allow *HTTP (port 80)* for Nginx
Allow *HTTPS (port 443)* for SSL
6️⃣ Click *Launch* and *download the key pair (`.pem` file)*
---
*🔹 Step 2: Connect to AWS EC2 via SSH*
🔹 *For Mac/Linux Users:*
```bash
chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@your-ec2-public-ip
```
🔹 *For Windows Users (Using PuTTY):*
1️⃣ Convert `.pem` to `.ppk` using *PuTTYgen*
2️⃣ Open **PuTTY**, enter **EC2 Public IP**, and load the `.ppk` key
3️⃣ Click *Open* and log in as `ubuntu`
---
*🔹 Step 3: Install Required Packages on EC2*
Once connected, update the system and install dependencies:
```bash
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-venv nginx git -y
```
---
*🔹 Step 4: Clone Django Project from GitHub*
Navigate to the home directory and **clone your GitHub repository**:
```bash
cd /home/ubuntu
git clone https://github.com/your-username/your...
cd your-django-project
```
---
*🔹 Step 5: Set Up Virtual Environment & Install Dependencies*
Create a virtual environment and install requirements:
```bash
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
---
*🔹 Step 6: Configure Django Settings for Production*
1️⃣ *Update `ALLOWED_HOSTS` in `settings.py`*
```python
ALLOWED_HOSTS = ["your-ec2-public-ip", "your-domain.com"]
```
2️⃣ *Migrate the Database:*
```bash
python manage.py migrate
```
3️⃣ *Create a Superuser:*
```bash
python manage.py createsuperuser
```
---
*🔹 Step 7: Set Up Gunicorn to Serve Django App*
1️⃣ Install Gunicorn:
```bash
pip install gunicorn
```
2️⃣ Run Gunicorn to test:
```bash
gunicorn --bind 0.0.0.0:8000 your_project.wsgi
```
✅ Now your Django app should be running on **port 8000**.
---
*🔹 Step 8: Set Up Nginx as a Reverse Proxy*
1️⃣ Stop Gunicorn and install Nginx:
```bash
sudo apt install nginx -y
```
2️⃣ Create an Nginx configuration file for Django:
```bash
sudo nano /etc/nginx/sites-available/django
```
3️⃣ Add the following configuration:
```
server {
listen 80;
server_name your-ec2-public-ip your-domain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```
4️⃣ Enable the Nginx configuration:
```bash
sudo ln -s /etc/nginx/sites-available/django /etc/nginx/sites-enabled
sudo systemctl restart nginx
```
---
*🔹 Step 9: Run Gunicorn as a Background Process*
Create a Gunicorn systemd service file:
```bash
sudo nano /etc/systemd/system/gunicorn.service
```
Add the following content:
```
[Unit]
Description=Gunicorn instance to serve Django
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/your-django-project
ExecStart=/home/ubuntu/your-django-project/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/your-django-project/gunicorn.sock your_project.wsgi:application
[Install]
WantedBy=multi-user.target
```
Save and restart Gunicorn:
```bash
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
```
*🔹 Hashtags:*
#Django #AWS #EC2 #Nginx #DjangoDeployment #Gunicorn #GitHub #Python #CloudComputing #DevOps #FullStack #AWSDeployment
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: