简体   繁体   中英

Django in Ubuntu 18.04 on Azure VM gunicorn Can't connect to ('0.0.0.0', 80)

I'm trying to deploy my Django application to Azure virtual machine with Ubuntu 18.04.

  1. I have set up the VM and connect to it via SSH.
  2. Then run the update and upgrade command
  3. Setup Python and Virtualenvironment
  4. Upload my code and activate the environment
  5. Allow the port 8000 using sudo ufw allow 8000 for testing
  6. After installing all the requirements, when I run the command:

     python manage.py runserver 0.0.0.0:8000 

The application runs, but when I open the URL as: :8000/

It doesn't return anything not any errors in the console

Update: It's just fixed by manually adding the port 8000 in azure portal under Inbound port rules . But: when I try to run it via gunicorn as:

gunicorn --pythonpath PROJECT PROJECT.wsgi:application --log-file - --bind 0.0.0.0:80

it returns another error as below:

[30007] [ERROR] Can't connect to ('0.0.0.0', 80)

What can be wrong here?

You also need to add 0.0.0.0:80 to inbound port rules. As of right now its accepting only 8000 port requests.

Then Try again:

gunicorn --pythonpath PROJECT PROJECT.wsgi:application --log-file -b 0.0.0.0:80

Steps To Add 80 port in Azure:

You open a port, or create an endpoint, to a virtual machine (VM) in Azure by creating a network filter on a subnet or a VM network interface. You place these filters, which control both inbound and outbound traffic, on a network security group attached to the resource that receives the traffic.

The example in this article demonstrates how to create a network filter that uses the standard TCP port 80 (it's assumed you've already started the appropriate services and opened any OS firewall rules on the VM).

1 - After you've created a VM that's configured to serve web requests on the standard TCP port 80, you can:

2 -Create a network security group.

3 - Create an inbound security rule allowing traffic and assign values to the following settings:

4 - Destination port ranges: 80

5 - Source port ranges: * (allows any source port)

6 - Priority value: Enter a value that is less than 65,500 and higher in priority than the default catch-all deny inbound rule.

Associate the network security group with the VM network interface or subnet.

To fix the issue about the application runs via python manage.py runserver 0.0.0.0:8000 which can not be accessed, there are two reasons cause the issue.

  1. The inbound port rules of Azure VM NSG did not allow the inbound request to port 8000. To add a new port rule for port 8000 in NSG on Azure portal to fix it, as the figures below.

Fig 1. To add this rule in figure to allow inbound requests of port 8000

在此处输入图片说明

Fig 2. The dialog of add inbound security rule

在此处输入图片说明

  1. Edit the settings.py file to add the allowed hosts or IPs into the ALLOWED_HOSTS array, as below.

     # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['<your vm ip or DNS name>', 'localhost', '127.0.0.1'] 

Then run python manage.py runserver 0.0.0.0:8000 , you can see Django default index page without any error in browser, as the figure below.

Note: The gunicorn server listens on port 80 which is a default allowed inbound port rule.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM