Prequisite to Build & Deploy Django App in AWS EC2
- OS Linux-Ubuntu (Windows ,Mac)
- Visual Studio Code
- Python
- Django
- Bootstrap
Steps to run this Shopping-Mart Website in your local host
To get this repository, run the following command inside your git enabled terminal
Step 1 $ git clone https://bit.ly/3ddPqVu
Step 2 $ python manage.py makemigrations
This will create all the migrations file (database migrations) required to run this App.
Step 3 Now, to apply this migrations run the following command
$ python manage.py migrate
Step 4
One last step and then our ecoomerce Shopping Mart website will be live. We need to create an admin user to run this App. On the terminal, type the following command and provide username, password and email for the admin user
$ python manage.py createsuperuser
Step 5
Now let's make the App live. We just need to start the server now and then we can start using our simple ecoomerce Shopping Mart. Start the server by following command
$ python manage.py runserver
Step 6
Once the server is hosted, head over to http://127.0.0.1:8000/products/ for the App.That was pretty simple, right?
How To Deploy Django Application in AWS EC2
Django comes with a development server that helps run Django projects in localhost. However, to make your web application available worldwide you will need a host machine. Amazon Web Services (AWS) provides EC2 (Elastic Compute Cloud) which is the most popular choice to host Django web applications.
Creating EC2 Instance With Ubuntu
Step 1: Select EC2 From AWS Console and click lauch Instance
Step 2: Name your Instance and Select Ubuntu
Step 3:
select instance type t2-micro (make sure free tier eligible) and create new key pair
Step 4:
Select launch Instance
Step 5:
If Instance created Succesfully you can view instance
Step 6:
Check instance is running and check status 2/2 passed
Step 7
Now select security and edit inbound rule
Step 8:
In this Security setting add rules and edit inbound rule select custom TCP--Port range 8000 and source
port 0.0.0.0 it will allow excess to anywhere click save rules
Step 9:
Now go back to your EC2 page select your instance and right click connect
Step 10:
Now open your ubuntu terminal & locate your private key file. The key used to launch this instance is YOUR_KEY_NAME.pem file
in my case my pem file is in Download folder
cd Downloads
Run this command, to ensure your key is not publicly viewable.
chmod 400 WRITE_YOUR_KEY_NAME.pem
Connect to your instance using its Public DNS:
sudo ec2-3-92-214-213.compute-1.amazonaws.com (Enter Your Public DNS)
you will connect to AWS EC2 Ubuntu server
Step 11
ls
cd My_Shopping_Mart
make sure git install into your ubuntu server before cloning github project type or copy this command in your terminal
git clone https://github.com/prakashsanjay/My-Shopping-Mart.git and find your project by typing ls command you will fimd My_Shopping_mart
Step 12
now go to settings.py file
cd pyshop
ls
vim settings.py press enter
make sure Python and Django both are installed in your AWS linux
Step 13
press i to insert
than scrool down to Allowed Hosts [ ] empty box means your local host. now
enter asterisk ['*'] it will allow any ip address or [you can enter your EC2 publiv IP ]
press esc button
:wq press enter
you can also update it from vs code project folder locate settings.py file in pyshop folder
you can change allowed host from there as well.
Step 14
Run server
Python3 manage.py runserver 0.0.0.0:8000
Step 15
CTRL + CLICK 0.0.0.0:8000
Once the server is hosted, head over to http://0.0.0.0:8000/products/ for the App.and our django app now running into AWS EC2 server
That was also pretty simple, right?
thanks & regards
Happy Learning
Top comments (0)