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
- AWS Account: You need an AWS account to use Lightsail.
- Django Application: Have a Django app ready to deploy.
- 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
-
Log in to AWS Lightsail
- Go to the AWS Lightsail console.
- Sign in with your AWS account.
-
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
-
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.
-
Update the Package Manager
sudo apt-get update sudo apt-get upgrade
-
Install Python and Pip
sudo apt-get install python3 python3-pip
-
Install Virtualenv
sudo pip3 install virtualenv
Step 3: Deploy Your Django Application
-
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
- If your project is in a Git repository, clone it to your Lightsail instance:
-
Set Up a Virtual Environment
virtualenv venv source venv/bin/activate
-
Install Dependencies
pip install -r requirements.txt
-
Configure Your Django Settings
- Update
settings.py
to configure your database, static files, and allowed hosts. EnsureALLOWED_HOSTS
includes your Lightsail instance's IP address or domain name. - Example:
python ALLOWED_HOSTS = ['your_instance_ip_or_domain']
- Update
-
Apply Migrations and Collect Static Files
python manage.py migrate python manage.py collectstatic
Step 4: Set Up a Web Server and WSGI
-
Install Gunicorn
pip install gunicorn
-
Test Gunicorn
gunicorn --bind 0.0.0.0:8000 your_project_name.wsgi
-
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
-
Create a Systemd Service File
sudo nano /etc/systemd/system/gunicorn.service
-
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
-
Start and Enable the Service
```sh
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
Step 6: Finalize Deployment
-
Open Ports in Lightsail
- In the Lightsail console, go to the Networking tab of your instance.
- Add a custom TCP port 80 for HTTP.
-
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.
Top comments (3)
Interesting read about python deployment on AWS lightsail
Thanks so much for the article! It saved my ass today
I am happy you liked it!