Note: Here my django application name is “office”
Deploy DJango application ON EC2 using nginx, Supervisor, Gunicorn.
- In the EC2 dashboard, the Launch Instance portal will set up a new instance.
- Then Choose an Amazon Machine Image(AMI) In the free tier option.
- Configure instance types and Security Groups.
- A key pair is generated after launching the instance. This key-pair file is to be downloaded on the local system as it helps in connecting to EC2 using SSH.
Connecting to EC2
- Open PuTTY by clicking on the desktop icon or the putty.exe file in the PuTTY folder.
- In the Host Name (or IP address) box, type IPV4 address.
- Port is set to 22.
- Under Authentication parameters, click Browse and navigate to the directory where your PuTTY Private Key (.ppk) file is located.
- Click on the (.ppk) file to select it.
- Click Open.
- If this is the first time you have connected to your Instance, a PuTTY Security Alert will ask you whether to proceed with the connection.
- Click Yes to complete the connection.
- The EC2 Instance window will appear (black screen).
First thing you need to do update apt package manager in Ubuntu and as well as install Required packages Using the below commands.
sudo apt-get update
sudo apt-get install python3-pip -y
sudo pip3 install gunicorn -y
sudo apt-get install supervisor
sudo apt-get install nginx -y
sudo pip3 install django
Supervisor configuration:
PATH: sudo vi /etc/supervisor/config.d/gunicorn.conf
add below conf code into gunicorn.conf file
[program:gunicorn]
directory=/home/ubuntu/office
command=/usr/local/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/office/app.sock office.wsgi:application
autostart=true
autorestart=true
stderr_logfile=gunicorn.err.log
stdout_logfile=gunicorn.out.log
[group:guni]
programs:gunicorn
Verify Supervisor configuration:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status
NGINX configuration changes:
PATH: sudo vi /etc/nginx/sites-available/django.conf
add below conf code into django.conf file
server{
listen 80;
server_name 127.0.0.1;
location / {
include proxy_params;
proxy_pass /home/ubuntu/office/app.sock;
}
}
Verify NGINX config changes and restart server:
sudo nginx -t
sudo ln django.conf /etc/nginx/sites-enabled
sudo service nginx restart
Leave a comment