AWS EC2 Django runserver problems with port 8000 - python

So I am running an EC2 instance, and I am having a bit of problems.
As of right now, the website is functioning, but not properly.
Django is listening on 0.0.0.0:8000, and I have port 8000 exposed, and the ports are defined 8000:8000 by default. My EC2 security group has 8000 open for incoming, and I can telnet connect from my EC2.
However, when I type my domain name in, domain.com, it does not connect. It connects on domain.com:8000. But I obviously do not want to have that happen, the website should jump right to port 8000.
So in the ports setting of my django project, I remapped 80:8000 so that you connect from port 80 client side and that is mapped to port 8000 server side. It is working now, as domain.com connects.
However I plan on adding SSL certs and this is most likely not going to work, and this is just not good practice.
Are there ways to run multiple manage.py runserver and collectstatic commands as well? Should I be using nginx or is the manage.py runserver 0.0.0.0:8000 command enough? Thanks!

You should not use runserver in production. I would suggest using Nginx with uwsgi.
You can run multiple manage.py runserver commands provided that different ports are used. For example
python3 manage.py runserver 8001
python3 manage.py runserver 8002
python3 manage.py runserver 8003
You can also run multiple collectstatic commands. Although, you would presumably want to use different arguments for each command, for example
python3 manage.py collectstatic --settings=mysite.settings
python3 manage.py collectstatic --settings=mysite.other_settings

Related

Django application not working on a new centos server [duplicate]

I followed the instructions here to run Django using the built-in webserver and was able to successfully run it using python manage.py runserver. If I access 127.0.0.1:port locally from the webserver, I get the Django page indicating it worked.
I realize the Django webserver is not a production server, but it's important for me for testing purposes to be able to access it from the outside world -- i.e. not from a web browser on the server, but from a different computer.
I tried:
http://mywebserver:port_django_runs_on
but it did not work. I also tried using the IP instead (based on ifconfig) to access:
http://myipaddress:port_django_runs_on
which did not work either.
The web server is running so it must be accessible from the outside, I'm just not sure how. I am running Linux with Apache, though I have not configured Django with Apache.
Any ideas on how to do this?
You have to run the development server such that it listens on the interface to your network.
E.g.
python manage.py runserver 0.0.0.0:8000
listens on every interface on port 8000.
It doesn't matter whether you access the webserver with the IP or the hostname. I guess you are still in your own LAN.
If you really want to access the server from outside, you also have to configure your router to forward port e.g. 8000 to your server.
Check your firewall on your server whether incoming connections to the port in use are allowed!
Assuming you can access your Apache server from the outside successfully, you can also try this:
Stop the Apache server, so that port 80 is free.
Start the development server with sudo python manage.py runserver 0.0.0.0:80
I had to add this line to settings.py in order to make it work (otherwise it showed an error when accessed from another computer)
ALLOWED_HOSTS = ['*']
then ran the server with:
python manage.py runserver 0.0.0.0:9595
Also ensure that the firewall allows connections to that port
Pick one or more from:
Your application isn't successfully listening on the intended IP:PORT
Because you haven't configured it successfully
Because the user doesn't have permission to
Your application is listening successfully on the intended IP:PORT, but clients can't reach it because
The server local iptables prevents it.
A firewall prevents it.
So, you can check that your application is listening successfully by running lsof -i as root on the machine and look for a python entry with the corresponding port you've specified.
Non-root users generally cannot bind to ports < 1024.
You'll need to look at iptables -nvL to see if there's a rule that would prevent access to the ip:port that you are trying to bind your application to.
If there is an upstream firewall and you don't know much about it, you'll need to talk to your network administrators.
just do this:
python manage.py runserver 0:8000
by the above command you are actually binding it to the external IP address.
so now when you access your IP address with the port number, you will be able to access it in the browser without any problem.
just type in the following in the browser address bar:
<your ip address>:8000
eg:
192.168.1.130:8000
you may have to edit the settings.py
add the following in the settings.py in the last line:
ALLOWED_HOSTS = ['*']
hope this will help...
For AWS users.
I had to use the following steps to get there.
1) Ensure that pip and django are installed at the sudo level
sudo apt-get install python-pip
sudo pip install django
2) Ensure that security group in-bound rules includ http on port 80 for 0.0.0.0/0
configured through AWS console
3) Add Public IP and DNS to ALLOWED_HOSTS
ALLOWED_HOSTS is a list object that you can find in settings.py
ALLOWED_HOSTS = ["75.254.65.19","ec2-54-528-27-21.compute-1.amazonaws.com"]
4) Launch development server with sudo on port 80
sudo python manage.py runserver 0:80
Site now available at either of the following (no need for :80 as that is default for http):
[Public DNS] i.e. ec2-54-528-27-21.compute-1.amazonaws.com
[Public IP] i.e 75.254.65.19
I'm going to add this here:
sudo python manage.py runserver 80
Go to your phone or computer and enter your computers internal IP (e.g 192.168.0.12) into the browser.
At this point you should be connected to the Django server.
This should also work without sudo:
python manage.py runserver 0.0.0.0:8000
UPDATED 2020
TRY THIS WAY
python manage.py runserver yourIp:8000
ALLOWED_HOSTS = ["*"]
You need just to allow any hosts :
settings.py :
ALLOWED_HOST = ['*']
Run your server :
python3 manage.py runserver 0.0.0.0:8000
If you want to connect android app just add internet permission in AndroidManifest
It's work for me ;)
open terminal
type : ifconfig
check results of ifconfig command
use the inet IP .. should look like this.. 192.168.1.121 or similar 192.168.x.x.
now runserver like you normally do but this time specify the inet IP
python3 manage.py runserver 192.168.x.x:8000 (replace the x with your inet)
also
on settings.py
ALLOWED_HOSTS = ['*']
install ngrok in terminal
sudo apt-get install -y ngrok-client
after that run:
ngrok http 8000
or
ngrok http example.com:9000
If you are using Docker you need to make sure ports are exposed as well

How is Django able to grant reserved port numbers?

Using this command
python manage.py runserver 0.0.0.0:8000
we can host a Django server locally on any port.So a developer can use reserved and privileged port numbers say
python manage.py runserver 127.0.0.1:80
So, now I am using port 80 defined for the HTTP protocol.
So, why does this not raise any issues and how is this request granted ?
Port 80 has no magical meaning, it is not "reserved" or "privileged" on your server (besides most likely requiring root privileges to access, as others have mentioned). It is just a regular port that was chosen to be a default for http, so you don't have to write google.com:80 every time in your browser, that's it.
If you have no web server running such as apache or nginx which usually listen to that port, then port 80 is up for grabs. You can run django runserver on it, you can run a plain python script listening to it, whatever you like.
You should use a proper server instead of Django's test server such as nginx or apache to run the server in production on port 80. Running something like sudo python manage.py runserver 0.0.0.0:80 is not recommended at all.

Manage.py runserver for demonstration

How would I run a Django application on, a digitalocean droplet let's say, with just using the development server Django provides. I've tried just running python3 manage.py runserver, but I can't pull it up with the browser from another computer
I know this is bad practice, but I really only need it up to demonstrate for a class project
by default runserver only listn on 127.0.0.1 that is not accessible from remote computer.
run
python3 manage.py runserver 0.0.0.0:8000
will solve it, simply check the real IP if the machine and use it as address in your browser
python3 manage.py runserver <your IP address>:8000

Running django on 0.0.0.0:8000

I am using vagrant along with Virtualbox, so I can't runserver on the default port and address or it won't work.
Django says:
(...) to listen on all public IPs (useful if you want to show off your work on
other computers), use:
python manage.py runserver 0.0.0.0:8000
Then I tried to access the server via http://127.0.0.1:8888/ but it says Unable to connect. Any guess how to run the server correctly?
I have followed the gettingstartedwithdjango vidoes and this http://127.0.0.1:8888/ worked for the author.
For people who are just looking where to configure the IP, it is run like so (as seen in the OP) -
python manage.py runserver 0.0.0.0:8000
If you are accessing it over your local network you will also need to update settings.py with the IP of your host, as well as specifying localhost. Your local IP can be gotten on Windows by running ipconfig in the command prompt, or ip addr show in the terminal on Ubuntu.
ALLOWED_HOSTS = ['192.168.1.XXX', 'localhost']
You have to run with root privileges. If you run with sudoyou will succeed.
UPDATE 1
The previous information is irrelevant with the topic.
In vagrant you have to forward your port to a local port. Please have a look at this.
UPDATE 2
This page explained how to install and configure django in a vagrant box.

How to access the local Django webserver from outside world

I followed the instructions here to run Django using the built-in webserver and was able to successfully run it using python manage.py runserver. If I access 127.0.0.1:port locally from the webserver, I get the Django page indicating it worked.
I realize the Django webserver is not a production server, but it's important for me for testing purposes to be able to access it from the outside world -- i.e. not from a web browser on the server, but from a different computer.
I tried:
http://mywebserver:port_django_runs_on
but it did not work. I also tried using the IP instead (based on ifconfig) to access:
http://myipaddress:port_django_runs_on
which did not work either.
The web server is running so it must be accessible from the outside, I'm just not sure how. I am running Linux with Apache, though I have not configured Django with Apache.
Any ideas on how to do this?
You have to run the development server such that it listens on the interface to your network.
E.g.
python manage.py runserver 0.0.0.0:8000
listens on every interface on port 8000.
It doesn't matter whether you access the webserver with the IP or the hostname. I guess you are still in your own LAN.
If you really want to access the server from outside, you also have to configure your router to forward port e.g. 8000 to your server.
Check your firewall on your server whether incoming connections to the port in use are allowed!
Assuming you can access your Apache server from the outside successfully, you can also try this:
Stop the Apache server, so that port 80 is free.
Start the development server with sudo python manage.py runserver 0.0.0.0:80
I had to add this line to settings.py in order to make it work (otherwise it showed an error when accessed from another computer)
ALLOWED_HOSTS = ['*']
then ran the server with:
python manage.py runserver 0.0.0.0:9595
Also ensure that the firewall allows connections to that port
Pick one or more from:
Your application isn't successfully listening on the intended IP:PORT
Because you haven't configured it successfully
Because the user doesn't have permission to
Your application is listening successfully on the intended IP:PORT, but clients can't reach it because
The server local iptables prevents it.
A firewall prevents it.
So, you can check that your application is listening successfully by running lsof -i as root on the machine and look for a python entry with the corresponding port you've specified.
Non-root users generally cannot bind to ports < 1024.
You'll need to look at iptables -nvL to see if there's a rule that would prevent access to the ip:port that you are trying to bind your application to.
If there is an upstream firewall and you don't know much about it, you'll need to talk to your network administrators.
just do this:
python manage.py runserver 0:8000
by the above command you are actually binding it to the external IP address.
so now when you access your IP address with the port number, you will be able to access it in the browser without any problem.
just type in the following in the browser address bar:
<your ip address>:8000
eg:
192.168.1.130:8000
you may have to edit the settings.py
add the following in the settings.py in the last line:
ALLOWED_HOSTS = ['*']
hope this will help...
For AWS users.
I had to use the following steps to get there.
1) Ensure that pip and django are installed at the sudo level
sudo apt-get install python-pip
sudo pip install django
2) Ensure that security group in-bound rules includ http on port 80 for 0.0.0.0/0
configured through AWS console
3) Add Public IP and DNS to ALLOWED_HOSTS
ALLOWED_HOSTS is a list object that you can find in settings.py
ALLOWED_HOSTS = ["75.254.65.19","ec2-54-528-27-21.compute-1.amazonaws.com"]
4) Launch development server with sudo on port 80
sudo python manage.py runserver 0:80
Site now available at either of the following (no need for :80 as that is default for http):
[Public DNS] i.e. ec2-54-528-27-21.compute-1.amazonaws.com
[Public IP] i.e 75.254.65.19
I'm going to add this here:
sudo python manage.py runserver 80
Go to your phone or computer and enter your computers internal IP (e.g 192.168.0.12) into the browser.
At this point you should be connected to the Django server.
This should also work without sudo:
python manage.py runserver 0.0.0.0:8000
UPDATED 2020
TRY THIS WAY
python manage.py runserver yourIp:8000
ALLOWED_HOSTS = ["*"]
You need just to allow any hosts :
settings.py :
ALLOWED_HOST = ['*']
Run your server :
python3 manage.py runserver 0.0.0.0:8000
If you want to connect android app just add internet permission in AndroidManifest
It's work for me ;)
open terminal
type : ifconfig
check results of ifconfig command
use the inet IP .. should look like this.. 192.168.1.121 or similar 192.168.x.x.
now runserver like you normally do but this time specify the inet IP
python3 manage.py runserver 192.168.x.x:8000 (replace the x with your inet)
also
on settings.py
ALLOWED_HOSTS = ['*']
install ngrok in terminal
sudo apt-get install -y ngrok-client
after that run:
ngrok http 8000
or
ngrok http example.com:9000
If you are using Docker you need to make sure ports are exposed as well

Categories

Resources