-1

On production, I run the django project with

python3 manage.py runserver

Resulting:

Performing system checks...

System check identified no issues (0 silenced).
December 09, 2019 - 03:23:26
Django version 2.2.7, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

I also tried python3 manage.py runserver 0.0.0.0:8000

But when I access it, the site is refused to connect. (I tried 0.0.0.0:8000, 127.0.0.1:8000, site-url:8000)

When I tunnel it via ngrok, it run smoothly. So I found out that the django project is already running but we can't access it from outside.

When I run sudo lsof -i -P -n | grep LISTEN

The result is:

sshd        890            root    3u  IPv4   18590      0t0  TCP *:22 (LISTEN)
sshd        890            root    4u  IPv6   18592      0t0  TCP *:22 (LISTEN)
apache2    6049            root    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6049            root    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
python3    6117            aina    4u  IPv4 2896228      0t0  TCP 127.0.0.1:8000 (LISTEN)
apache2    6141        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6141        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
ngrok      6198            root    3u  IPv4 2896574      0t0  TCP 127.0.0.1:4040 (LISTEN)
apache2    6383        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6383        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
apache2    6401        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6401        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
apache2    6417        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6417        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
apache2    6419        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6419        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
apache2    6420        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6420        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
apache2    6421        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6421        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
apache2    6512        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6512        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
apache2    6555        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6555        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)
apache2    6693        www-data    4u  IPv6 2895520      0t0  TCP *:80 (LISTEN)
apache2    6693        www-data    6u  IPv6 2895524      0t0  TCP *:443 (LISTEN)

I tried to telnet to port 8000 by running telnet 127.0.0.1 8000 The results is:

Connected to 127.0.0.1.
Escape character is '^]'.

As there is other project is running in server (a PHP application in Apache), i follow this question Run both Django and PHP application in Apache

But the site still can't be reached when I access it directly (not via ngrok). What should I do so the django can be accessed?

Aina Aiikyo
  • 125
  • 7
  • Can you telnet to the machine on Port 8000? – ThatCampbellKid Dec 09 '19 at 04:15
  • yes, and it said connected – Aina Aiikyo Dec 09 '19 at 04:19
  • Is the URL you are connecting to in the allowed hosts in settings.py? – ThatCampbellKid Dec 09 '19 at 04:21
  • To access you mean from another computer in the same network? In this case, you have to run the Django server in `0.0.0.0` address then only you can access it from another computer. `python3 manage.py runserver 0.0.0.0:8000` – Raja Simon Dec 09 '19 at 04:22
  • 1
    First, you should use `0.0.0.0:8000` for the `runserver` command. You are most probably blocked by a firewall (`ngrok` creates a tunnel so it can work behind firewalls). – Selcuk Dec 09 '19 at 04:23
  • in settings.py i made ```ALLOWED_HOSTS = ['*']``` – Aina Aiikyo Dec 09 '19 at 04:26
  • i already tried ```python3 manage.py runserver 0.0.0.0:8000``` the result is the same – Aina Aiikyo Dec 09 '19 at 04:27
  • 1
    It is not advisable to run `python manage.py runserver` in production. – Red Cricket Dec 09 '19 at 04:27
  • is there another way beside runserver? – Aina Aiikyo Dec 09 '19 at 04:31
  • Several https://docs.djangoproject.com/en/2.2/howto/deployment/. `runserver` is the development server. It says as much in the start up output: "`Starting development server at http://127.0.0.1:8000/`" – Red Cricket Dec 09 '19 at 04:34
  • 1
    I would suggest not using the development server (runserver) in production. You're better off configuring a proper nginx/gunicorn setup based on your production environment. The django docs explicitly say that the development server is unsuitable for production and will quickly fail as your site's load increases. EDIT: Yes, there is another way. Depending on which platform you are deploying on, google how to deploy django application on x (can be aws, heroku, pythonanywhere whatever your production server is) – AbdurRehman Khan Dec 09 '19 at 04:45

1 Answers1

1

It's because I access it via https it should be from http

Aina Aiikyo
  • 125
  • 7