How to start Django in development on EC2 so that it is available on the Internet?

I have a Django app. It works on EC2. EC2 has a private IP address and a resilient public IP address.

I want the web application to be accessible locally, as well as the IP address of the developer, which is located outside the network.

Define these three IP addresses as:

EC2_PRIVATE_IP EC2_PUBLIC_IP DEVELOPER_IP 

So what I did was run through EC2:

 python manage.py runserver 0.0.0.0:8000 

I went into my EC2 security settings and opened the inbound and outbound port 8000 in DEVELOPER_IP.

Then he asked the Developer to go to the address EC2_PUBLIC_IP in his browser.

Unfortunately, this does not work as it gets an error:

 Gateway Timeout: can't connect to remote host 

Update # 1

I tried before:

 python manage.py runserver {EC2_PUBLIC_IP}:8000 

But I got the error:

 Error: That IP address can't be assigned-to. 
+6
source share
7 answers

I get it. I also need to open the port on the Windows firewall!

0
source

The server must be running with a lower URL

 python manage.py runserver 0.0.0.0:8000 

In the EC2 security settings, add the following INBOUND settings

 HTTP TCP 8000 0.0.0.0/0 

Then you must access this machine with the URL

 http://EC2_PUBLIC_IP:8000 

If you want to access the url as

 http://EC2_PUBLIC_IP 

then start your web server on port 80 and, accordingly, change the EC2 security settings.

+5
source

All the answers here give a solution, I want to publish this for completeness.

By default, the runerver command starts the development server on the internal IP address on port 8000.

If you want to change the server port (the standard port for accessing the Internet is 80), pass it as a command line argument. For example, this command starts the server on port 80:

$ python manage.py runningerver 80

Note. You do not want users to enter : port_number after the URL in the browser. Another thing is that you may not own the host machine, so you may not have access to the firewall settings to allow ports other than 80.

If you want to change the IP address of the servers, send it along with the port. Therefore, to listen on all public IP addresses (useful if you want to show your work on other computers on your network), use:

$ python manage.py runningerver 0.0.0.0:80

Documentation about everything you need to know about the development server can be found here .

+1
source

Remove all the rules from your outbound security group and replace them with the All Traffic Allow rule.

Your machine receives an incoming packet, but its response receives a rejection of the security filter. Your developer block will send a packet with a (semi-) arbitrary source port number .

(In addition, you cannot bind to a shared address in an EC2 instance, as it translates from a private address using the routing infrastructure, so this is actually not β€œon” your block.)

0
source

Make sure your firewall is not blocking anything.

Try

 iptable -F 

This will delete all firewall rules on the computer itself.

Edit: However, you should not use it. If you want to add a port to your firewall, use the following estimate if you are using redhat-based distributions (e.g. centos, rhel)

 system-config-firewall 

If it is not, try installing it.

 # yum install system-config-firewall # run it after becoming a root 
0
source

In addition to adding firewall rules that allow traffic on port 8000 / Others

You will need to start the development server so that it listens for requests on all interfaces, and not just on the local

 python manage.py runserver 0.0.0.0:8000 

or any other port you can choose

0
source

Leave the Django application to run locally and try installing nginx on the ec2 instance and the trafic proxy on port 80 on localhost: 8000. Something like this:

 server { listen 80; server_name www.yoursite.com; client_max_body_size 1000M; location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 1000; proxy_read_timeout 1000; proxy_pass http://127.0.0.1:8080/; # This is the trick ! proxy_buffering off; proxy_set_header Connection ''; proxy_http_version 1.1; chunked_transfer_encoding off; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS'; } } 

You can add a service control for developers' IP addresses only. Nginx is a lightweight, fast, reliable and scalable Web / Proxy server. Most downloaded websites use NGINX! It will work as a service, it will listen to all traffic coming from the outside world (Internet), and in your case, you would like to tell it to listen to traffic through the port (port 80) and redirect it to your Django application (it works on port 8000); This is a really good idea, it is transparent, and if it is well set up, it will expand your site! In addition, Nginx is very suitable for delivering applications for the Internet and mobile devices. Installing and configuring nginx is very simple, and you need to integrate something similar to the above example to open the application for the Internet.

0
source

Source: https://habr.com/ru/post/987143/


All Articles