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-serverSelect the
t2.microFree tier eligibleGo to
Congfigure Security Groupand 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
ConnectYou will see
EC2 Instance Connectand then click onConnectA 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 updatesudo apt-get upgrade
Check if Python3 is installed :
python3 --version
install python environment :
sudo apt-get install python3-venvpython3 -m venv Projectname-env
Activate the Python Environment by
source Projectname-env/bin/activateInstall nginx
sudo apt-get install -y nginxInstall 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.gitNote : here "Todo-list-app" is my repository change it with your Repositoryrun :
pip3 install gunicornInstall the Project requirements :
pip3 install -r requirements.txtGo to
cd /etc/supervisor/conf.d/locationRun Below Commands :
sudo touch gunicorn.confsudo nano gunicorn.confPaste 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/gunicornsudo 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.confsudo nano django.confPaste 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 -tsudo 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.confPaste 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 -tsudo 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.confEdit the
server_name: server{ listen 80; server_name todolist.com www.todolist.com; }Run nginx service again
sudo nginx -tsudo service nginx restart
Remember the ipv4 public address copy it from EC2 instance
in AWS search
Route53and click Create a Hosted Zone and follow the below commandsClick 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 createVisit 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 updatesudo apt-get install software-properties-commonsudo add-apt-repository universesudo add-apt-repository ppa:certbot/certbotsudo apt-get updatesudo 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 reloadsudo service nginx restart
