26

I am new to EC2 and web development. Currently I have a Linux EC2 instance running, and have installed Django. I am creating a test project before I start on my real project and tried running a Django test server.

This is my output in the shell:

python manage.py runserver ec2-###-##-##-##.compute-1.amazonaws.com:8000
Validating models...

0 errors found
Django version 1.3, using settings 'testsite.settings'
Development server is running at http://ec2-###-##-##-##.compute-1.amazonaws.com:8000/
Quit the server with CONTROL-C.

To test that it is wroking I have tried visiting: ec2-###-##-##-##.compute-1.amazonaws.com:8000 but I always get a "Cannot connect" message from my browser.

Whenever I do this lcoally on my computer however I do successfully get to the DJango development home page at 127.0.0.1:8000. Could someone help me figure out what I am doing wrong / might be missing when I am doing this on my EC2 instance as opposed to my own laptop?

1

6 Answers 6

51

Using an ec-2 instance with Ubuntu, I found that specifying 0.0.0.0:8000 worked:

$python manage.py runserver 0.0.0.0:8000  

Of course 8000 does need to be opened for TCP in your security group settings.

3
  • You also need to be sure that this port is not already being used.
    – bozdoz
    Oct 19, 2016 at 23:11
  • 2
    This is the right response for this question. Thanks May 1, 2017 at 5:50
  • how to open 8000 for TCP in security group. I have outbound traffic set to ALL TRAFIC. But Its not working yet
    – Aseem
    Mar 27, 2019 at 21:25
26

You probably don't have port 8000 open on the firewall. Check which security group your instance is running (probably "default") and check the rules it is running. You will probably find that port 8000 is not listed.

3
  • Okay I added 8000 to UDP, TCP, and ALL to ICMP with 'default' as the source but trying to connect to port 8000 still isn't working. Does this mean that perhaps Django isn't working or is it still the firewall on the Amazon ec2 servers? Thanks so much for your help!
    – Mars J
    Mar 28, 2012 at 3:06
  • Open a second connection to the same machine and try to telnet to port 80, telnet localhost 80 then type GET / and hit enter a couple of times. If you can't connect then runserver isn't working properly, if you can and you get HTML from the GET then it's between the server and where you are that the problem is.
    – KayEss
    Mar 29, 2012 at 2:55
  • That should have been port 8000 above of course. You can get telnet with something like sudo apt-get install telnet (if you're on a debian/ubuntu system anyway).
    – KayEss
    Jun 12, 2012 at 2:46
10

1) You need to make sure port 8000 is added as a Custom TCP Rule into your Security Group list of inbound ports

2) Odds are that the IP that you see listed on your AWS Console, which is associated to your instance is a PUBLIC IP OR a PUBLIC Domain Name(i.e. ec2-###-##-##-##.compute-1.amazonaws.com or 174.101.122.132) that Amazon assigns.

2.1) If it is a public IP, then your instance has no way of knowing what the Public IP assigned to it is, rather it will only know the its assigned Local IP.

2.2) To get your Local IP on a Linux System, type:

    $ ifconfig

Then look at the eth0 Data and you'll see an IP next to "inet addr" of the format xxx.xxx.xxx.xxx (e.g. 10.10.12.135) This is your Local IP

3) To successfully runserver you can do one of the following two:

    $ python manage.py runserver <LOCAL IP>:8000
    or
    $ python manage.py runserver 0.0.0.0:8000

** Option Two also works great as Ernest Ezis mentioned in his answer. EDIT : From The Django Book : "The IP address 0.0.0.0 tells the server to listen on any network interface"

** My theory of Public IP could be wrong, since I'm not sure how Amazon assigns IPs. I'd appreciate being corrected.

3
  • Thanks! Your Step #1 "You need to make sure port 8000 is added as a Custom TCP Rule into your Security Group list of inbound ports" was what I needed. Afterwards, python manage.py runserver 0.0.0.0:8000 worked like a (py)charm! Feb 3, 2016 at 4:42
  • how to do first step? Aug 12, 2016 at 19:37
  • @vijayshanker Sign into your EC2 console -> instances -> select instance from list -> "Security" from the bottom menu -> In Bound Rules -> edit -> add rules
    – Shmack
    Nov 15, 2021 at 22:35
2

I was having the same problem. But I was running RHEL on EC2. Besides from adding a rule to security group, I had to manually add a port to firewalld.

firewall-cmd --permanent --add-port=8000/tcp
firewall-cmd --reload

That worked for me! (Although no idea why I had to do that)

1
  • This worked for me on CENTOS7. Before opening this port on the firewall using the firewall-cmd, the test page was inaccessible from ec2-###-##-##-##.compute-1.amazonaws.com:8000. Popped into existence immediately after trying this.
    – eculeus
    Apr 28, 2016 at 16:34
1

Yes, if you use quick launch EC2 option, you should add new HTTP rule (just as it appears on the list) to run a development server.

2
  • 1
    Can you clarify "just as it appears on the list"?
    – pjmorse
    Sep 18, 2012 at 1:31
  • @pjmorse, add a rule in the security group for that instance. Mar 11, 2018 at 19:32
0

Adding a security group with the inbound rules as follows usually does the trick unless you have something else misconfigured. The port range specifies which port you want to allow incoming traffic on.

  • HTTP access would need 80
  • HTTP access over port 8000 would need 8000
  • SSH to server would need 22
  • HTTPS would need 443

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.