Deploying a Python Django App on AWS Lightsail
Tags: awslightsail, communitybuilder, python, containers
Published: 2024-07-10 10:59:13

Deploying a Django app on AWS Lightsail is a cost-effective and straightforward way to get your application online. In this guide, we'll walk through the process step-by-step, with examples to help you follow along.

## Prerequisites

1. **AWS Account:** You need an AWS account to use Lightsail.
2. **Django Application:** Have a Django app ready to deploy.
3. **Basic Knowledge of Django and the command line:** Familiarity with Django project structure and basic command line usage will be helpful.

## Step 1: Create an AWS Lightsail Instance

1. **Log in to AWS Lightsail**
   - Go to the [AWS Lightsail console](https://lightsail.aws.amazon.com/).
   - Sign in with your AWS account.


![AWS Lightsail Homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73o3pobo5c1vabzxdbo8.png)

2. **Create an Instance**
   - Click **Create instance**.
   - Choose your instance location (e.g., a region close to your users).
   - Under **Select a blueprint**, choose **OS Only** and then **Ubuntu 20.04 LTS**.
   - Choose your instance plan based on your resource needs and budget.
   - Give your instance a unique name.
   - Click **Create instance**.

## Step 2: Configure the Instance

1. **Connect to Your Instance**
   - In the Lightsail console, click on the instance you just created.
   - Click **Connect using SSH** to open a browser-based SSH terminal.

2. **Update the Package Manager**
   ```sh
   sudo apt-get update
   sudo apt-get upgrade
   ```

3. **Install Python and Pip**
   ```sh
   sudo apt-get install python3 python3-pip
   ```

4. **Install Virtualenv**
   ```sh
   sudo pip3 install virtualenv
   ```

## Step 3: Deploy Your Django Application

1. **Clone Your Django Project**
   - If your project is in a Git repository, clone it to your Lightsail instance:
     ```sh
     git clone https://github.com/yourusername/your-django-app.git
     cd your-django-app
     ```

2. **Set Up a Virtual Environment**
   ```sh
   virtualenv venv
   source venv/bin/activate
   ```

3. **Install Dependencies**
   ```sh
   pip install -r requirements.txt
   ```

4. **Configure Your Django Settings**
   - Update `settings.py` to configure your database, static files, and allowed hosts. Ensure `ALLOWED_HOSTS` includes your Lightsail instance's IP address or domain name.
   - Example:
     ```python
     ALLOWED_HOSTS = ['your_instance_ip_or_domain']
     ```

5. **Apply Migrations and Collect Static Files**
   ```sh
   python manage.py migrate
   python manage.py collectstatic
   ```

## Step 4: Set Up a Web Server and WSGI

1. **Install Gunicorn**
   ```sh
   pip install gunicorn
   ```

2. **Test Gunicorn**
   ```sh
   gunicorn --bind 0.0.0.0:8000 your_project_name.wsgi
   ```

3. **Install and Configure Nginx**
   ```sh
   sudo apt-get install nginx
   ```

   - Create an Nginx configuration file:
     ```sh
     sudo nano /etc/nginx/sites-available/your_project_name
     ```

   - Add the following configuration:
     ```nginx
     server {
         listen 80;
         server_name your_instance_ip_or_domain;

         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;
             proxy_set_header X-Forwarded-Proto $scheme;
         }

         location /static/ {
             alias /path/to/your/static/files;
         }

         location /media/ {
             alias /path/to/your/media/files;
         }
     }
     ```

   - Enable the configuration:
     ```sh
     sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled
     sudo nginx -t
     sudo systemctl restart nginx
     ```

## Step 5: Set Up a Systemd Service for Gunicorn

1. **Create a Systemd Service File**
   ```sh
   sudo nano /etc/systemd/system/gunicorn.service
   ```

2. **Add the Following Configuration**
   ```ini
   [Unit]
   Description=gunicorn daemon
   After=network.target

   [Service]
   User=your_username
   Group=www-data
   WorkingDirectory=/path/to/your/project
   ExecStart=/path/to/your/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/path/to/your/project.sock your_project_name.wsgi:application

   [Install]
   WantedBy=multi-user.target
   ```

3. **Start and Enable the Service**
   ```sh
   sudo systemctl start gunicorn
   sudo systemctl enable gunicorn
   ```

## Step 6: Finalize Deployment

1. **Open Ports in Lightsail**
   - In the Lightsail console, go to the Networking tab of your instance.
   - Add a custom TCP port 80 for HTTP.

2. **Access Your Application**
   - Visit your instance's IP address or domain in a web browser. You should see your Django application running.

## Conclusion

You've successfully deployed a Django application on AWS Lightsail. This setup includes a secure and scalable environment using Nginx and Gunicorn to serve your app. For further enhancements, consider setting up SSL with Let's Encrypt and automating your deployments with CI/CD tools.

---

### About the Author

Arafat is a third-year member of AWS Community Builder. He is passionate about software engineering and cloud computing by sharing knowledge with the community. The AWS Community Builders program provides me with the opportunity to connect with other cloud enthusiasts and stay updated with the latest AWS services and features.

![AWS Community Builders Badge](https://www.amazon.com/photos/shared/EVJ-l7ymQGiCUdRPHdjvJw.uNCphn1zCr8K7PVwDivFb0/gallery/gYfwdd62SISs3u9TODBWlA)