Cannot access Django server on EC2 instance - python

I created an EC2 instance. Installed Python 3.4 on it and then installed Django 1.10.6 on it. I was trying to develop my first django application
I started django server.
python manage.py runserver
I could not access at http://n.n.n.n:8000.
I get a ERR_CONNECTION_REFUSED error.
I went back to the EC2 instance and added the protocol/port to the security group. This is how it looks after I add the port/proptocol
Custom TCP Rule TCP 8000 0.0.0.0/0
Custom TCP Rule TCP 8000 ::/0
It did not work. I even added a rule to allow all traffic from anywhere. It still did not work.
However, if I start django server the following way
python manage.py runserver 0.0.0.0:8000
I get the following error:
DisallowedHost at /
Invalid HTTP_HOST header: 'n.n.n.n:8000'. You may need to add 'n.n.n.n' to ALLOWED_HOSTS.
If tried adding the IP to ProjectName/settings.py,
ALLOWED_HOSTS = ['n.n.n.n'] #Make sure your host IP is a string
I get the ERR_CONNECTION_REFUSED error
I can ping the IP. I can ssh (there is rule to allow ssh). Does not look like there is a firewall.
$ sudo service iptables status
iptables: Firewall is not running.
Why am I not able access http/django server?
Thanks

Actually, I must not have started the server right, when I added the host to the ProjectName/settings.py file. I tried again, and this time it worked.
So looks like
django server started on EC2 instances with the following
python manage.py runserver
may not be accessible from other machines.
django server started with the following
python manage.py runserver 0.0.0.0:8000
would be accessible from the Internet provided the IP address of the host machine is added to the ALLOWED_HOSTS in the ProjectName/settings.py file
ALLOWED_HOSTS = ['n.n.n.n'] #Make sure your host IP is a string
This is all in addition to the entries in the security group

The IP to the allowed hosts means the IP of the devices which you want to access Django from.

Related

django run localhost from another computer connected to another network

I am running my django project in localhost and it works fine..
For test purpose I want to run my localhost from another computer connected in the same network.
I have done python manage.py runserver 'my ip address'
That works fine too.. Is there any way that I can access my localhost from another computer connected to another network?
Like I am connected to A network and running my localhost and my friend is connected to B network. Suppose he wants to access my localhost and see my project running then is it possible to access localhost of a computer from another computer connected to another project?
You can. Just run the django runserver in
python manage.py runserver 0.0.0.0:8000
Now you can access your app using youripaddress:8000
From Django ...
Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0 or :: (with IPv6 enabled).
You can provide an IPv6 address surrounded by brackets (e.g. [200a::1]:8000). This will automatically enable IPv6 support.
Updated:
In order to match the answer to Title of the question. You need configure your router to forward port 80 to yourapp address
Run server with local host or your system IP like one of the below
python manage.py runserver 192.168.6.7:8000
python manage.py runserver 0.0.0.0:8000
python manage.py runserver 127.0.0.1:8000
add hosts in settings.py to access from other system in network.
ALLOWED_HOSTS = ['127.0.0.1', 'localhost','192.168.6.7']
If you're working DEBUG=True mod in your Django project, you shouldn't need anything other than that (I assume you are not using port 80, it requires root access).
You must use 0.0.0.0 as host IP, it is a simple solution. And the command is:
python manage.py runserver 0.0.0.0:8000. That's it.

Accessing the Django embedded server for development inside PyCharm by other hosts inside my local network?

I'm working with PyCharm and I wonder if there's a way to make the Django embedded server accesible for the other hosts in my local network or I need to deploy my app on a dedicated web server such as Apache?
Now, I'm accessing my Django app like this in the browser:
http://localhost:8000/mypage/
and I want other users inside my local network to type:
http://my_private_ip:8000/mypage/
in their browsers and see the same page.
Just run the server (which is Django's embedded server FWIW, not PyCharm's) under http://my_private_ip:8000:
# ./manage.py help runserver
Usage: manage.py runserver [options] [optional port number, or ipaddr:port]
Starts a lightweight Web server for development.
(...)
# ./manage.py runserver my_private_ip:8000
Assuming a Unix environment.
You need to ensure the server is listening not on the lo interface but on all interfaces (or at least the one used to connect to the LAN).
If you can customize the way PyCharm launches the server, use 0.0.0.0 as the host, as in:
python manage.py runserver 0.0.0.0:8000
Your coworkers can then use your LAN IP address. If you don't know it, use $ ip a.

How do I access my django app running on Amazon ec2?

So, I have looked around stack overflow + other sites, but havent been able to solve this problem: hence posting this question!
I have recently started learning django... and am now trying to run it on ec2.
I have an ec2 instance of this format: ec2-xx-xxx-xx-xxx.us-west-2.compute.amazonaws.com on which I have a django app running. I changed the security group of this instance to allow http port 80 connections.
I did try to run it the django app the following ways: python manage.py runserver 0.0.0.0:8000 and python manage.py runserver ec2-xx-xxx-xx-xxx.us-west-2.compute.amazonaws.com:8000 and that doesnt seem to be helping either!
To make sure that there is nothing faulty from django's side, I opened another terminal window and ssh'ed into the instance and did a curl GET request to localhost:8000/admin which went through successfully.
Where am I going wrong? Will appreciate any help!
You are running the app on port 8000, when that port isn't open on the instance (you only opened port 80).
So either close port 80 and open port 8000 from the security group, or run your app on port 80.
Running any application on a port that is less than 1024 requires root privileges; so if you try to do python manage.py runserver 0.0.0.0:80 as a normal user, you'll get an error.
Instead of doing sudo python manage.py runserver 0.0.0.0:80, you have a few options:
Run a pre-configured AMI image for django (like this one from bitnami).
Configure a front end server to listen on port 80, and then proxy requests to your django application. The common stack here is nginx + gunicorn + supervisor, and this blog post explains how to set that up (along with a virtual environment which is always a good habit to get into).
Make sure to include your IPv4 Public IP address in the ALLOWED_HOSTS section in Django project/app/settings.py script...

Accessing django project in LAN systems

I used django and developed a site which is working fine, and its about to move to production and ready for deployment in a couple of weeks.
So before moving to production, i want to share the site with some of my employees to check the functionality and something else. Actually their systems are connected in LAN with mine.
So my system IP address is something like 192.168.12.135, when we run run django development server its runs at localhost:8000, i mean with the system IP address and with a port 8000 like 192.168.12.135:8000 right.
So i had shared them the project site link as 192.168.12.135:8000, but when they tried on the systems which are connected in LAN, it is not accessible and displaying an error Server not found.
I tried the above same way because recently i used python web.py framework and developed a minimal site , and when we run the server, it by default runs as localhost:8080 , and when i accessed this link from others system that are connected in LAN with mine as 192.168.12.135:8000 , its working fine and is accessible.
So can anyone please let me know
1. How to access the site on the systems that are connected in LAN before moving to production(in some real servers like apache, nginx etc.,).
2. Basically i am new to web developing and this is my first site developed in python, so
i don't know more about servers and deploying a project. So can anyone please let me know
the detailed information about deploying django on different servers
(First of all i am looking for a solution for 1st problem(Accessing in LAN before moving to
production))
If you run
python manage.py runserver 0.0.0.0:8000
your development server will be available on port 8000 to anyone on your LAN and on localhost as well (and it does not depend on your ip address)
You need to explicitly tell the development server to run on your IP rather than localhost.
Try python manage.py runserver your_ip:port.
Though it'll be accessible if you're running through apache or any other webservers other than the development server.
And to your 1st question, I would advice you to host and use a local apache server rather than using development server. Doing so, you can foresee the issues you'll be facing when moving to production.
And to 2nd, there are plenty of resources available configuring Django with different servers. Hail Google. :)
In your settings.py change ALLOWED_HOSTS to
ALLOWED_HOSTS = ['*']
Run your server by entering the following command
python manage.py runserver 0.0.0.0:8000
In order to access the project from another device enter the IP address of the server followed by the port number, which is 8000 in this example.
On windows I did everything you said but one thing was missing at my end to connect through Wi-Fi..
In settings.py:
ALLOWED_HOST = ['*']
Put Network profil in Private mode:
Windows > Settings > Network & Internet > Wi-Fi > (Click on_your_network) > In Network profil select: Private
Exemple: Run your server on the port 8000:
python manage.py runserver 0.0.0.0:8000
Then to access to the server with your other devices connected to the same network, enter the IPv4's server address with the your port (here 8000)
Exemple, if the IPv4's server address is 192.168.20.26 put the folling text directly in your browser:
192.168.20.26:8000

manage.py runserver

I am running python manage.py runserver from a machine A
when I am trying to check in machine B. The url I typed is http://A:8000/ .
I am getting an error like The system returned: (111) Connection refused
You can run it for machines in your network by
./manage.py runserver 0.0.0.0:8000
And than you will be able to reach you server from any machine in your network.
Just type on other machine in browser http://192.168.0.1:8000 where 192.168.0.1 is IP of you server... and it ready to go....
or in you case:
On machine A in command line ./manage.py runserver 0.0.0.0:8000
Than try in machine B in browser type http://A:8000
Make a sip of beer.
Source from django docs
You need to tell manage.py the local ip address and the port to bind to. Something like python manage.py runserver 192.168.23.12:8000. Then use that same ip and port from the other machine. You can read more about it here in the documentation.
I was struggling with the same problem and found one solution. I guess it can help you. when you run python manage.py runserver, it will take 127.0.0.1 as default ip address and 8000. 127.0.0.0 is the same as localhost which can be accessed locally. to access it from cross origin you need to run it on your system ip or 0.0.0.0. 0.0.0.0 can be accessed from any origin in the network.
for port number, you need to set inbound and outbound policy of your system if you want to use your own port number not the default one.
To do this you need to run server with command python manage.py runserver 0.0.0.0:<your port> as mentioned above
or, set a default ip and port in your python environment. For this see my answer on
django change default runserver port
Enjoy coding .....
Just in case any Windows users are having trouble, I thought I'd add my own experience. When running python manage.py runserver 0.0.0.0:8000, I could view urls using localhost:8000, but not my ip address 192.168.1.3:8000.
I ended up disabling ipv6 on my wireless adapter, and running ipconfig /renew. After this everything worked as expected.
in flask using flask.ext.script, you can do it like this:
python manage.py runserver -h 127.0.0.1 -p 8000
For people who are using CentOS7, In order to allow access to port 8000, you need to modify firewall rules in a new SSH connection:
sudo firewall-cmd --zone=public --permanent --add-port=8000/tcp
sudo firewall-cmd --reload
I had the same problem and here was my way to solve it:
First, You must know your IP address.
On my Windows PC, in the cmd windows i run ipconfig and select my IP V4 address. In my case 192.168.0.13
Second as mention above: runserver 192.168.0.13:8000
It worked for me.
The error i did to get the message was the use of the gateway address not my PC address.
First, change your directory:
cd your_project name
Then run:
python manage.py runserver
Ok just came across this post this is a little off topic but hopefully explains a few things, The IP 127.0.0.1 points to your network card so any traffic that you cause to go to that IP address will not leave your computer.
For example modern network cards in laptops for example will not even give you that IP if you are not connected to a wifi or cabled network so you'll need to be connected at least to activate the card.
If you need to run multiple servers on the same machine but want to access them with a domain then you have a couple of options
edit your computers host file to define the domain and what IP it goes to
use a DNS Alias I set up using a cname record years ago *.local.irishado.com will point to 127.0.0.1
so for example these three domains will point to your local machine
http://site1.local.irishado.com
http://site2.local.irishado.com
http://site3.local.irishado.com
will all point to your local machine then in python projects you will need to edit the projects setting file ALLOWED_HOSTS property to hold the domain it will accept
ALLOWED_HOSTS = ['site1.local.irishado.com']

Categories