Reputation: 1513
This tutorial here shows how to start the development server on AWS EC2:
ALLOWED_HOSTS=['EC2_DNS_NAME']
...
python manage.py runserver 0.0.0.0:8000
In a browser, navigate to public DNS of the instance, make sure to append port “8000” at the end and if everything was properly configured, it will show the index page of the application.
For example: www.ec2dnsname.com:8000
My public IPv4 DNS is of this form:
ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com
So, in my settings.py
ALLOWED_HOSTS=['ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com']
And run this in the EC2 terminal:
python manage.py runserver 0.0.0.0:8000
However, when I try to reach this address:
www.ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com:8000
nothing is showing up (The page took too long to respond
).
Is this the complete and correct way to just try run a development server on AWS EC2?
Update after @Iam_batman response: Below is the Security Group setting attached to my EC2 instance:
All TCP TCP 0 - 65535 0.0.0.0/0 –
All TCP TCP 0 - 65535 ::/0 –
The Port Range seems to cover everything. I still can't access the index page from the development server.
Also, my AWS account is a workplace account, not a personal account. If I follow @iam_batman setup below, using HTTP and SSH protocol, I get denied the access to port 80
and 22
respectively.
Update: To further test, I run a curl command as following :
curl http://ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com:8000
I get the entire HTML code for the standard Django index page (the one with the spaceship). This implies that the website is already available at that address. But for some reason, it does not render on the browser?
Upvotes: 2
Views: 977
Reputation: 1103
There's two things you should check out.
1. Check if security groups are configured.
Search for "Security Groups" in aws dashboard. Find the 'Create Security Group' Button on the top right and create a security group with inbound and outbound rules that allow http traffic on the port you want to communicate with.
Here's an example where i am using the standard 80 port for http. But, you can use 8000 if you want.
Now, you just need to assign the the security group you created to your ec2 instance. Go to EC2 dashboard and select your instance. Then Actions > Security > Change Security Groups. Select the security group you just created and you are good to go.
2. Maybe you need an application server
I have never actually tried deploying with the django development server ( runserver command ) because its not meant for production. For, deploying python based frameworks like django/flask/fast-api you need to use an appserver like: uwsgi, gunicorn etc. So, do give it a look. You need an appserver atleast. For, more production use cases you might need to have an actual webserver like nginx/apache on top of the app server.
These are my two notes on your issue.
Upvotes: 3