DEV Community

ARAFAT O. OLAYIWOLA
ARAFAT O. OLAYIWOLA

Posted on

Deploying a Python Django App on AWS Lightsail

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

AWS Lightsail Homepage

  1. 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

    sudo apt-get update
    sudo apt-get upgrade
    
  3. Install Python and Pip

    sudo apt-get install python3 python3-pip
    
  4. Install Virtualenv

    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

    virtualenv venv
    source venv/bin/activate
    
  3. Install Dependencies

    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

    python manage.py migrate
    python manage.py collectstatic
    

Step 4: Set Up a Web Server and WSGI

  1. Install Gunicorn

    pip install gunicorn
    
  2. Test Gunicorn

    gunicorn --bind 0.0.0.0:8000 your_project_name.wsgi
    
  3. Install and Configure Nginx

    sudo apt-get install nginx
    
  • Create an Nginx configuration file:

     sudo nano /etc/nginx/sites-available/your_project_name
    
  • Add the following configuration:

     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:

     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

    sudo nano /etc/systemd/system/gunicorn.service
    
  2. Add the Following Configuration

    [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



  1. Start and Enable the Service ```sh sudo systemctl start gunicorn sudo systemctl enable gunicorn
Enter fullscreen mode Exit fullscreen mode

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

Top comments (3)

Collapse
 
haroffcode profile image
ARAFAT O. OLAYIWOLA • Edited

Interesting read about python deployment on AWS lightsail

Collapse
 
harofah profile image
Arafah Olawumi

Thanks so much for the article! It saved my ass today

Collapse
 
haroffcode profile image
ARAFAT O. OLAYIWOLA

I am happy you liked it!