This Guide will Help you to Deploy your Django Project on AWS (amazon web services)
Prerequisites :
- Working Django Project Hosted on Github
- amazon AWS Account
Before we begin lets see how we will deploy it
- Django Project will be Deployed on AWS EC2 Instance.
- No S3 or Database (will use Django Default Database)
- Buy a Domain Name (Hostinger is used).
- use Route 53 to connect our Domain Name.
This Tutorial is Divided into 6 Parts :
1. Launching a EC2 instance
-
Search EC2 in Search option
-
Go to Instances and Click Launch Instance
-
Select
Ubuntu 20.04-amd64-server
-
Select the
t2.micro
Free tier eligible -
Go to
Congfigure Security Group
and click add followings rules :Type Protocol Port Range Source HTTP TCP 80 0.0.0.0/0 HTTP TCP 80 ::/0 SSH TCP 22 0.0.0.0/0 HTTPS TCP 443 0.0.0.0/0 -
Click Launch Instance
-
Now After Instance Launched click on it and then Click
Connect
-
You will see
EC2 Instance Connect
and then click onConnect
-
A new Tab will Open that's your Server (We can also ssh directly but in this tutorial we will use Browser only)
Now that Our Instance is Launched we have do update it and make it ready for Django
2. Updating Ubuntu, Installing Python,Virtualenv, nginx, supervisor
-
Run The FOllowing Commands :
sudo apt-get update
sudo apt-get upgrade
-
Check if Python3 is installed :
python3 --version
-
install python environment :
sudo apt-get install python3-venv
python3 -m venv Projectname-env
-
Activate the Python Environment by
source Projectname-env/bin/activate
-
Install nginx
sudo apt-get install -y nginx
-
Install supervisor
sudo apt-get install supervisor
Everything is Ready Now, we will clone our Repository from GitHub
3. Cloning Repository and Setting Django Project (Projectname-env should be activated):
-
run
git clone https://github.com/agajareiitr/Todo-list-app.git
Note : here "Todo-list-app" is my repository change it with your Repository
-
run :
pip3 install gunicorn
-
Install the Project requirements :
pip3 install -r requirements.txt
-
Go to
cd /etc/supervisor/conf.d/
location -
Run Below Commands :
-
sudo touch gunicorn.conf
-
sudo nano gunicorn.conf
-
Paste the Below code there and Edit your
Project Name
:[program:gunicorn] directory=/home/ubuntu/Todo-list-app command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/Todo-list-app/app.sock svportal.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
-
-
create gunicorn file :
sudo mkdir /var/log/gunicorn
sudo supervisorctl reread
- output:
guni:avaliable
- output:
sudo supervisorctl update
- output:
guni:added process group
- output:
sudo supervisorctl status
- output:
guni:gunicorn Running pid <id>, uptime 0:00:01
- output:
-
Go to
cd /etc/nginx/sites-available/
and run below commands :-
sudo touch django.conf
-
sudo nano django.conf
-
Paste the Below Code (change the server name with IPv4 public address):
server{ listen 80; server_name 65.0.130.1; location / { include proxy_params; proxy_pass http://unix:/home/ubuntu/Todo-list-app/app.sock; } }
-
Now let's test the nginx service
sudo nginx -t
sudo ln django.conf /etc/nginx/sites-enabled/
sudo service nginx restart
-
Our App don't have Static Files but if your App does then follow the Below Steps
4. Connecting the Static Files :
-
Go to Path -
cd /etc/nginx/sites-enabled/
-
Open Django.conf file by
sudo nano django.conf
-
Paste the Below Code and Edit according to you:
server{ listen 80; server_name 65.0.130.1; location / { include proxy_params; proxy_pass http://unix:/home/ubuntu/Todo-list-app/app.sock; } location /static { autoindex on; alias /home/ubuntu/Todo-list-app/static; } }
-
Run nginx service again
sudo nginx -t
sudo service nginx restart
Now Your Site is running on your IP check it, below steps are for connecting Domain
5. Connecting Domain Name with Route 53 :
-
Go to Any Domain Name PRovider and Purchase any Domain you want
-
Go to Path -
cd /etc/nginx/sites-enabled/
-
Open Django.conf file by
sudo nano django.conf
-
Edit the
server_name
:server{ listen 80; server_name todolist.com www.todolist.com; }
-
Run nginx service again
sudo nginx -t
sudo service nginx restart
-
Remember the ipv4 public address copy it from EC2 instance
-
in AWS search
Route53
and click Create a Hosted Zone and follow the below commands-
Click Create Hosted Zone and add Domain Name todolist.com (without www) and create it
-
Click Create Record set :
Record name : www Record type : A value : Your IPv4 public address leave else as it is and click create
-
Click Create Record set :
Record name : leave blank Record type : A Value : theres a Alias Button click it and choose endpoint Alias to another record in this hosted zone now select the www.todolist.com leave else as it is and click create
-
Visit your Domain Provider and go to thier DNS servers settings
-
In route53 theres a record name with type NS(name servers) paste them in DNS of domain provider one by one, it looks like below code :
ns-557.awsdns-05.net ns-165.awsdns-20.com ns-1831.awsdns-36.co.uk ns-1154.awsdns-16.org
-
Now your website will redirect the ipv4 to your domain name, but you can see its says now secure, so lets add a SSL Certificate
6. Adding SSL Certificate
-
Run the Following Commands :
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python3-certbot-nginx
- Give the Permission required and run
sudo certbot --nginx
-
following will be asked
- Email: enter your email so that you will get updates about your certificate
- Agree to terms of services: enter ‘a’ for accepting
- Share your email: You can ignore it by entering ‘n’.
- Choose your domains. If you want all the domains then hit enter
- Choose the redirect option: Enter 2 for enabling redirect.
-
Now Restart Everything
sudo supervisorctl reload
sudo service nginx restart