'Django: Error: You don't have permission to access that port

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.



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

sudo python manage.py runserver 0.0.0.0:80

you need admin rights for port 80

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel
Solution 2 Peyo Delavegua
Solution 3
Solution 4 hobs