Connecting to EC2 Django development Server - django

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?

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.

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.

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.

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)

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.

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

Related

access django website hosted on home server from external IP - CONNECTION TIMEOUT

I am working on a django project which I was able to access from an external network. But this has recently stopped working. I have tried troubleshooting the problem but I can't seem to figure out the issue.
Below are the steps I followed:
Public IP: 86.40.54.24
Private IP: 192.168.1.18
OS: Ubuntu 20.04
Router: EIR Network default router. Allows the required changes to forward requests
add routing rule in the router
Allow connections from any IP on port 80 to 192.168.1.18 on port 12987
setup rules in ufw to allow connections
ufw allow proto tcp from any to 192.168.1.18 port 12987
settings.py file
ALLOWED_HOSTS ["*"]
run django development server
python3 manage.py runserver 0.0.0.0:12987
Note:
An approach (similar to above) had worked for me and I was able to access my website from an external IP. But it suddenly stopped working. I have not been able to identify the problem.
I have kept the restrictions to bare minimum to be able to get this working once again. I will harden the server after the issue is identified and fixed.

Deploy Django Application on Cloud, but even cannot get access to ip_address:8000

I am a beginner on Django. And try to deploy a testproject on cloud server to check if it works or not.
Server: Ubuntu 16.04
And after I create virtualenv on the server with nginx installed.
I execute below:
python manage.py runserver 0.0.0.0:8000
And then I go to the browser to access my server's http://ip-address:8000.
But it failed to show anything of my application.
I already added the ip-address to ALLOWED_HOST. But still not working.
Are there any thoughts for this situation?
maybe this method work for you because it worked for mine on the ec2 instance on Amazon web server.
step1: go to your dashboard find launch-wizard menu and open it.
step2: Now click on inbound after that click on edit. Here click on Add rule and select Custom TCP rule & enter 8000 in port range. Now again click on Add rule and select HTTP and similarly do for HTTPS then click on save button.
step3: save and restart your AWS machine. Hopefully, your Django app runs on 8000 port.
For a Linux instance in the security group, follow these steps to verify the security group rule:
Connect to a Linux instance by using a password.
Run the following command to check whether TCP 80 is being listened. netstat -an | grep 80
If the following result returns, web service for TCP port 80 is enabled.
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
If not then there is a problem with your security group setup, please go through the official documentation and see how you can fix it:
https://www.alibabacloud.com/help/doc-detail/25471.htm

Accessing django website hosted on vm with mobile device

I run my django website with python manage.py runserver 0.0.0.0:8080 on Vagrant which is set up to forward 8080 port to 8081 on host machine. I'm able to access this website on host by going to it's local ip (192.168.X.X) but can't on mobile device (of course also by going into it's local ip).
Any idea? All I could find about this is to run server with 0.0.0.0 what is already happening in my case.
Never mind, I've forgot to enable public_network in my Vagrantfile.

How can I change django runserver url?

I'm trying to change django project url, so that users, who wants to connect to website in local area network will see url instead of localhost:8000 or 127.0.0.1. I need to change localhost:8000/users/board to be http://example.eu. I've tried to python manage.py runserver http://example.euand then thought about changing url's but it doesn't work. Also tried to change hosts file(Windows OS), but as far as I understand I need real ip address not url. Is it possible to do this thing and how?
You can use python manage.py runserver 0.0.0.0:8000. 0.0.0.0 means all IPv4 addresses on the local machine. So the server can be reachable by 127.0.0.1 and your private ip address like 10.10.5.8. So now others can access the server using http://10.10.5.8:8000. You the runserver on port 80, so that port can be removed from the url (by default is 80).
But to use any domain instead of ip, you have to change the hosts file of all the clients using the server to add domain to ip address mapping. Alternatively you can configure local network server to map the particular url to your system ip.
Run a local domain with the same port
Opening the /etc/hosts file on your mac with
sudo nano /etc/hosts
And for windows I believe you need to open:
C:\Windows\System32\Drivers\etc\hosts
In here add the domains you want, for example I added vazkir.com
....
127.0.0.1 localhost
127.0.0.1 vazkirtje.com
127.0.0.1 www.vazkirtje.com
.....
Lastly you can add it the domain to you ALLOW_HOSTS in your settings.py:
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "vazkirtje.com", "www.vazkirtje.com"]
And now you can visit your Django application (with port 8000) at:
http://vazkirtje.com:8000
The solution is not perfect since you do have to specify the port you are using, but this does let you use a domain for your local Django application in a relatively easy manner;)
Run a local non-existent domain without specifying the port
If you do want to run it without specifying the port; so just vazkirtje.com then you can use port 80, because this is the default port for HTTP.
Make sure the domain you are testing does not exist, since the domain lookup will first be done on existing domains. So check if you get a similar message to the one I got on chrome, when visiting the url:
Now you can specify this port by adding port "80" to the "runserver" command. You only do need to use "sudo" to run the command at this port, since you need admin rights for this. So run:
sudo python manage.py runserver 80
And now you should be able to access your Django application by visiting:
http://vazkirtje.com

Django runserver on all ports?

Why can't I run python manage.py runserver on all ports?
I understand this is probably a newbie question, but what prevents me from achieving this?
I would like to access my app without having to include the port, like a regular website
WARNING - Do not run the test server in production!
The reason you have to type in the port when connecting to the test server is because it doesn't run on a standard web port being "http: 80 and https: 443". If you use the command below it will not require the port number be provided when connecting to the test server. Keep in mind that you will need root or sudo access and if something is already running on port 80 it will fail.
Runserver with port:
python manage.py runserver -p 80
Just run it on port 80 and you won't have to specify the port.
You can't blast it on all other ports because many, many other services already use those other ports. Network services need to have ports specified.