0

I would like to make the website built on Django server (hosted on AWS EC2 instance) as public. As a first step, I would want to update the default port from 0.0.0.0:8000 to 0.0.0.0:80 or 0.0.0.0.

Every time I change port and run server, it gives me the below error:

System check identified no issues (0 silenced).
October 08, 2022 - 15:55:12
Django version 3.2.12, using settings 'config.settings'
Starting development server at http://0.0.0.0:80/
Quit the server with CONTROL-C.
Error: You don't have permission to access that port.

If I use sudo or access it as root user, it gives me the following error:

(venv) [ec2-user@ip-172-31-19-84 config]$ sudo python3 manage.py runserver 0.0.0.0:80
Traceback (most recent call last):
  File "manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 14, in main
    "Couldn't import Django. Are you sure it's installed and "
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?

And as root user:

(venv) [root@ip-172-31-19-84 config]# python3 manage.py runserver 0.0.0.0:80
Traceback (most recent call last):
  File "manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 14, in main
    "Couldn't import Django. Are you sure it's installed and "
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?

Here's my manage.py file:

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise 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?"
        )
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()
  • The Django development server is **not** for production/public use. Use an actual web server, like Apache or nginx. – pigrammer Oct 08 '22 at 17:58
  • You need to run Django with gunicorn and Nginx or apache. nginx and apache are actual webservers that can accept requests on the port 80 and pass the request to gunicorn server of Django. You can search on the internet, how to configure Nginx with gunicorn and Django. – K14 Oct 08 '22 at 19:20
  • the python command would not work with sudo or root user, because the python command run from simple user uses python installation for current user whereas commands run through root or sudo will use the python installation of root user. just for clarification, commands run through sudo or root users are the same. to make run django on root user first you need to switch to root user and then install all the project dependencies again for the root python installation. – K14 Oct 08 '22 at 19:25

0 Answers0