I am using the ubuntu server and want to deploy Django project in AWS using ec2, and it requires me to use port 80 to access its HTTP how do I solve this problem?
I heard from other people using sudo and enabling env but this error is all I got
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
Please help I really need help
The error you are getting with sudo because of the path, I am assuming you must be using virtualenv or any other tool for managing the python version and corresponding dependencies.
You can actually change the port where you default runserver
./manage.py runserver 0.0.0.0:<your_port>
django change default runserver port
The port numbers from 1 to 1023 are restricted for root user only and we can not assign those ports without having root access. If you actually want to start the server on port 80, there are some.
How to bind to port number less than 1024 with non root access?
sudo setcap CAP_NET_BIND_SERVICE=+eip /path/to/binary
OR
sudo touch /etc/authbind/byport/80
sudo chmod 777 /etc/authbind/byport/80
authbind --deep /path/to/binary command line args
Related
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
I installed Anaconda with Python, added some more packages and tried to run Django development python manage.py runserver 0.0.0.0. It's running fine but I can't access it remotely from some reason. My server on other port is working fine and firewalls are all set. I found others had this problem and they simply run it as superuser sudo python manage.py runserver 0.0.0.0.
My problem is that when running as superuser, it will use different Python (or at least that's what it looks like). It is same 2.7.12 version (but no Anaconda suffix) and there are no required packages, so I can't run server.
I'm not really experienced with Linux. I tried to remove Python from root and passing env variables, but that didn't help. How can I run python as superuser and use Python with packages from my user.
Try sudo running your local python, like
sudo /home/YourAnaconda_bin/python manage.py runserver 0.0.0.0
I'm very new to this whole setup so please be nice.
On dev the command usually works with no errors but since I have been experimenting with different commands for Django someting has gone wrong.
python manage.py runserver 0.0.0.0:80
I don't have permission to use this port anymore. I can use port 8080 but the website doesn't work when I add the port to the end of the usual host name in the url. When I used port 80 I never had to add :80 to the url anyway.
I had an error where I didn't have permissions to the log file but I changed the permissions on that file. It seems there is now many things I don't have permissions for.
Django 1.8.5.
Using a virtual envirnment and I have 2 apps in the project.
If you're on Linux, you'll receive this error.
First and foremost, Django does not have a production server, just a very basic development server and uses port 8080 by default.
when you execute the command
python manage.py runserver
you tell django to start its development server and it runs so you can test your web app before deployment to a production server.
Django Documentation -> django-admin -> Run Server
The way to access the server is to use your browser and plug in the URL in the address bar as so
localhost:8080
by default, most HTTP applications run on port 80 unless otherwise stated. For example, your MySQL server could run on port 3306 by default.
Basically, you can think of ports as old school telephone lines that connect you to whom ever your looking to communicate with.
There's nothing really special about any of this. You should probably play with bottle to get the basics down first; just a friendly suggestion.
You can dig in to the details on the website. While not secure, you can use sudo to run on port 80, but for security reasons you should avoid it.
#mtt2p mentions a serverfault post that does a great job of the why
I'm sure there's a way to tell the server to allow only local connections, but you should only use 0.0.0.0:80 when you want to show off your work to other people or see what your web app looks like on other devices.
In the long run, sudo is just easier and quicker, but lazy and insecure.
This is a link that explains it in the context of a virtualenv.
Django runserver error when specifying port
The answer states
I guess the sudo command will run the process in the superuser
context, and the superuser context lack virtualenv settings.
Make a shell script to set the virtualenv and call manage.py
runserver, then sudo this script instead.
You should note that the answer explaining a virtualenv based context is also insecure. It should just be run as
sudo python manage.py runserver 80
not
sudo bash script-name
outside of a virtualenv. Doing so defeats the purpose of sand-boxing your application. If you ignore this, you'll be exposing yourself to a race condition.
I am in Xubuntu 20.04 version and I use this command (because I have an python env) :
$ sudo ~/.virtualenvs/myproject/bin/python manage.py runserver 0.0.0.0:80
And to know where is your envs python folder, I did :
$ which python
sudo python manage.py runserver 0.0.0.0:80
you need admin rights for port 80
I set up a virtualenv called "aira" and installed virtualenvwrapper in the root environment (my virtualenvwrapper settings in /root/.bashrc are at the bottom). This reduces the number of sudo commands I need to cascade to -c together to get runserver working:
sudo sh -c "workon aira && python manage.py runserver --insecure 0.0.0.0:80"
If you've set up your django app's virtualenv without virtualenvwrapper you'll need to manually change to the correct directory and activate your virtualenv within the sudo command sequence. My virtualenv is called aira and I keep my virtualenvs in /root/.virtualenvs. My django project is in the ubuntu user's home directory:
sudo sh -c "source $HOME/.virtualenvs/aira/bin/activate && cd /ubuntu/src/aira/ && python manage.py runserver --insecure 0.0.0.0:80"
If you've installed django and your requirements.txt in the system site packages then you can use sudo to runserver.
sudo python manage.py runserver --insecure 0.0.0.0:80"
The --insecure option allows staticfiles to serve your static assets (images, css, javascript).
For completeness, here're my virtualenvwrapper configuration variables in /root/.bashrc on Ubuntu 16.04:
# python3 is used for virtualenv and virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
# *root* reuses the virtualenvs in *ubuntu*'s home directory
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=/home/ubuntu/src
source /usr/local/bin/virtualenvwrapper.sh
sudo ./venv/bin/python manage.py runserver 80
I'm using django's builtin server to develop a site and I want other computers in the same network I'm on to be able to access the server using the local IP address.
I have seen many posts about this and after trying all suggestions it's still not allowing other computers in my network to access the site.
I run the server using
python manage.py runserver 0.0.0.0:8000
and have already opened port 8000 as you can see in the following image.
I'm running Django 1.4.2, Python 2.7.3, Fedora 18 on kernel 3.8.11-200
Any help is highly appreciated. Thanks.
Use python manage.py runserver <ip>:<port>
For example,my IP is 192.168.0.100 and I want to run django app on port 80,I have to do
[sudo] python manage.py runserver 192.168.0.100:80
My port 80 needed root permissions,maybe because I have other applications accessing it.
You also have to add the IP address to ALLOWED_HOSTS list in settings.py
By doing this all clients in the 192.168.0 network will be able to access the site at 192.168.0.100
You're starting Django as needed - it will accept connections from anywhere as soon as the connections get to it.
Check your firewall and make sure it's allowing 8000 port connections. Something like this should work:
iptables -I INPUT -p tcp -m tcp --dport 8000 -j ACCEPT
Optionally you will need to extend the INTERNAL_IPS variable in the setting to allow remote debugging: https://docs.djangoproject.com/en/dev/ref/settings/#internal-ips .
skarap is correct. If your network is configured correctly and your django application with pytho9n manage.py runserver 0.0.0.0:8000 and you still can't access your django app from the VM host there is almost certainly a firewall issue. The illustration above is good if you are running iptables.
I deployed CentOS 7 on a virtualbox VM from a Windows 7 host. I didn't know that this distribution uses firewalld, not iptables to control access.
if
ps -ae | grep firewall
returns something like
602 ? 00:00:00 firewalld
your system is running firewalld, not iptables. They do not run together.
To correct you VM so you can access your django site from the host use the commands:
firewall-cmd --zone=public --add-port=8000/tcp --permanent
firewall-cmd --reload
Many thanks to pablo v on the http://www.scriptscoop.net site for pointing this out.
You have to add the server IP to the ALLOWED_HOSTS in the settings.py
set firewall rules with
$ sudo ufw enable
$ sudo ufw allow 8000
type ipconfig
copy addr under inet
do python3 manage.py runserver your_inet_addr:8000
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