19

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.

13
  • 3
    sudo python manage.py runserver 0.0.0.0:80
    – mtt2p
    May 25, 2016 at 10:31
  • I don't understand your question. You don't use runserver with uwsgi. May 25, 2016 at 10:33
  • 3
    And @mtt2p please don't recommend that; you're basically encouraging OP to use runserver for production. May 25, 2016 at 10:34
  • Thanks @mtt2p that worked but why do I have to use sudo now? It used to work without sudo.
    – Helpme
    May 25, 2016 at 10:37
  • 1
    Argh this makes no sense. If you're running the dev server, you access it on the default port which is 8000. And what does this have to do with uwsgi? You do not appear to be using uwsgi at all. May 25, 2016 at 13:07

8 Answers 8

10

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.

0
5

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
5

Below helps to me to rectify same error:

in command line in windows:

net stop winnat

net start winnat

1
sudo python manage.py runserver 0.0.0.0:80

you need admin rights for port 80

4
  • 1
    add that sudo here (in your answer) also @mtt2p May 25, 2016 at 10:34
  • Then can you tell me how to get admin rights for a port?
    – Helpme
    May 25, 2016 at 10:38
  • serverfault.com/questions/112795/…
    – mtt2p
    May 25, 2016 at 10:39
  • to inherit the environment you can use the -E option with the sudo command: sudo -E python manage.py rumserver 0.0.0.0:80
    – Hamid
    May 29, 2021 at 20:14
1

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
1
  • Ubuntu uses bash so instead of doing sudo sh you have to do sudo bash. Just a little change. Jan 31, 2018 at 10:26
1

sudo ./venv/bin/python manage.py runserver 80

2
0

Run these Commands:

  • sudo su
  • pip3 install django
  • python3 manage.py runserver 0:80
0

If you have the same case as me and your port was working before but suddenly stopped working the most likely case is that the port has been taken by other service or that did not close properly.

In windows you can do:

1. windows+r and type cmd.
2. run: netstat -a -n -o
3. here you can see which PID has taken the port
4. for closing you can go to task manager (ctrl+alt+delete)
5. go to processes tab
6. look for the matching PID and stop the process

This was he working solution for me to make it work again in the usual port apparently other app I had hijacked the port.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.