In this guide, we'll delve into the process of deploying a Django application on an AWS EC2 instance, leveraging the power of Gunicorn, Supervisor, and Nginx. This combination offers a robust, scalable, and secure solution for hosting your Django web application.
Click on instances and click on ‘Launch Instances’
Enter Server name
Select the Ubuntu AMI
Select the t2.micro if you want to stay on the Free tier.
Under Network Settings Allow SSH, HTTP and HTTPS
On the Summary pane on the right, confirm your set up and click on launch instance.
You should see a success banner after a successful launch.
There are multiple ways (read this to learn how to connect via PuTTY) to connect to an EC2 instance however, to keep things simple we will use the Amazon EC2 Instance Connect method
Navigate to EC2 > Instances > Select your instance > Click on Connect
On the Connect to Instance page, click on connect
sudo apt-get update
sudo apt-get upgrade
python3 –version
sudo apt-get install virtualenv
virtualenv venv
omsource ./venv/bin/activate
Let us assume we are deploying a portfolio site in my github repository.
git clone https://github.com/charlesu49/portfolio
pip install -r requirements.txt
Install Gunicorn: Run the command pip install gunicorn
to install gunicorn
cd /etc/supervisor/conf.d/
sudo touch gunicorn.conf/
sudo nano gunicorn.conf
[program:gunicorn]
directory:/home/ubuntu/portfolio
command=/home/ubuntu/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/portfolio/app.sock portfolio.wsgi:application
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log
[group:guni]
programs:gunicorn
mkdir /var/log/gunicorn/
to create log directorysudo supervisorctl reread
sudo supervisorctl update
Check supervisor status: sudo supervisorctl status
Ensure you see RUNNING as shown below:
sudo apt-get install -y nginx
Confirm installation: Open your browser and type in the IPV4 address of your instance and you should see the below page:
cd /etc/nginx/
sudo nano nginx.conf
Change the user www-data to any other configured user with necessary permissions. For the purpose of this test we will be using root to avoid any permission issues (this is not advised in a production setting)cd /etc/nginx/sites-available/
Create a config file for this projects: sudo nano portfolio.conf
server {
listen 80;
server_name <public_ipv4_address> <domain name e.g: charlesudo.com>;
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/portfolio/app.sock;
}
# Static files
location /static/ {
alias /home/ubuntu/portfolio/staticfiles/;
}
# Media files
location /media/ {
alias /home/ubuntu/portfolio/media/;
}
}
sudo nginx -t
sudo ln portfolio.conf /etc/nginx/sites-enabled
sudo service nginx restart
Open your browser and type in public IPV4 address of your EC2 instance or a domain name mapped to it and you should be able to access your website. If you make a change to your code and push to git hub you can pull the changes on your EC2 server and restart supervisor to see the update by running the command: sudo systemctl restart supervisor
Sign in to add to the conversation