Run the Django server on a different port

If you start your Django development server it runs on port 8000 by default.

$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
October 19, 2020 - 12:19:16
Django version 3.1.1, using settings 'api.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

To start the development server on a different port number you pass it as an argument after runserver. To run the development server on port 8001 that would make the command python manage.py runserver 8001.

$ python manage.py runserver 8001
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
October 19, 2020 - 12:20:38
Django version 3.1.1, using settings 'api.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CONTROL-C.

You can even run multiple development servers simultaneously this way if you’d like to do so.

Run on port 80

Running on port 80 can be done with some special treatment. A disclaimer first, there are probably better ways to make you site available via port 80, with a reversed proxy for example.

You most likely have to use sudo in order to run the server via port 80. This is where other problems may arise. If you run the command using sudo the virtualenv isn’t taken into account. So just running sudo python manage.py runserver 80 won’t work if you’re using virtualenv. Since the python command references a different binary. One way to work around that is by using the full path to the python in your virtualenv.

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

This might have done the trick, but there is a chance you got stuck at a next problem. If you have NGINX or Apache installed on your system than port 80 is probably in use already. Giving you the following error message.

Error: That port is already in use

If that’s the case you need to stop those services first. In case you’re running Apache you can most likely stop it using the following command, but it may be different depending on your operating system.

$ sudo service apache2 stop

For NGINX the command would be.

$ sudo service nginx stop

Now you should be all set to run the Django development server on port 80. But again, look into reversed proxies.

$ sudo /home/../venv/bin/python manage.py runserver 80
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
October 19, 2020 - 12:42:55
Django version 3.1.1, using settings 'api.settings'
Starting development server at http://127.0.0.1:80/
Quit the server with CONTROL-C.

More options

For more options you can use the help option as well.

$ python manage.py runserver --help

See Also

6 Non-Programming Books for Programmers