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
Related
I just started learning Django
and it's stopping me from the beginning.
as you can see from the image, whenever I try to runserver Errno 11001 comes in.
So I googled it and seemed like proxy problem
so I tried everything I could find but nothing fixed it.
(environmental variables, trying different proxys, etc..)
Can anybody helpme plzzzz
Even though Django's official starter tutorial suggests that we can use '0' as a shortcut for IP '0.0.0.0' the command runserver throws an error Error: [Errno 11001] getaddrinfo failed when the shortcut is used with it.
Use
python manage.py runserver 0.0.0.0:8000
to listen on all available public IPs on port 8000 instead of
python manage.py runserver 0:8000
No, that's now how you should run the server you have to pass ip address and port number like this
python manage.py runserver 0.0.0.0:800
hope this helps!
Run Django server with this command
python manage.py runserver
Or
python manage.py runserver 0.0.0.0:8000
Here, 8000 is port, you can use other port as well.
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
I am using Windows 8.1. When I run a command to start the django server using
python manage.py runserver
it runs fine. But when I open 127.0.0.1:8000 in the browser it says:
This site can't be reached.
127.0.0.1 took too long to respond.
I have already tried to change the port number by using
python manage.py runserver 8080
but still it doesn't run.
Please help me solving this issue.
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 only have ssh access to a server. It is ubuntu from amazon aws. I have uploaded my django project to it. When I run the command python manage.py runserver or any variants of it, the server starts.
But as you know this will not allow me to access the web application
from my browser which resides on a different network.
My question is
What would be the way that after I give my command to runserver either via django's development server or gunicorn. I may access it from the outside world by typing IP:8000 in my browser.
Secondly I would like to turn the access to IP:8000 on/off as needed. Like I may allow access for 5 minutes and then run a command to stop the access from outside world?
For accessing the django project else every where you have use the command
python manage.py runserver 0.0.0.0:8000
then You will be access it outside by giving the IP:8000
Note : the server provided by the Django is only for the development purpose only . If you want a permanent hosting you may like to use Apache or any server , where You can host your project. Hope This helps . :)
You need assign a elastic IP address (EIP) on the aws ec2 instance, and allow the inbound traffic 0.0.0.0/0 to port 8000 from its security group.
Then you should be fine to access it from every where with EIP, such as http://54.12.23.34:8000
EIP may generate cost, if you stop the ec2 instance.
You may also want to try the command:
python manage.py runserver [::]:8000
This will bind runserver to all available interfaces on your AWS VM. Please also note: runserver is really just meant for development. Running it in production is a bad idea.
You can use http://localtunnel.me/, it's very very easy but you should use it only for development purposes.
Just in case, you want to run it on 80 port then you need root permissions
sudo python manage.py runserver 0.0.0.0:80