How do I run Flask on port 80?

I have a Flask server running over port 5000, and that's fine. I can access it at http://example.com//000

But is it possible to simply access it at http://example.com ? I assume that this means that I should change the port from 5000 to 80. But when I try to do this on Flask, I get this error message at startup.

Traceback (most recent call last): File "xxxxxx.py", line 31, in <module> app.run(host="0.0.0.0", port=int("80"), debug=True) File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 772, in run run_simple(host, port, self, **options) File "/usr/local/lib/python2.6/dist-packages/werkzeug/serving.py", line 706, in run_simple test_socket.bind((hostname, port)) File "<string>", line 1, in bind socket.error: [Errno 98] Address already in use 

Running lsof -i :80 returns

 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME apache2 467 root 3u IPv4 92108840 0t0 TCP *:www (LISTEN) apache2 4413 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN) apache2 14346 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN) apache2 14570 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN) apache2 14571 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN) apache2 14573 www-data 3u IPv4 92108840 0t0 TCP *:www (LISTEN) 

Do I need to kill these processes first? It's safe? Or is there another way to keep Flask running on port 5000, but somehow redirect the main domain of the site?

+168
python flask networking port
Nov 26 '13 at 9:22
source share
10 answers

Thus, this causes this error message because apache2 is running on port 80.

If this is for development, I would just leave it like on port 5000.

If it is for production either:

Not recommended

  • First stop apache2 ;

It is not recommended to indicate in the documentation:

You can use the embedded server during development, but you must use the full deployment option for production applications. (Do not use the embedded development server during production.)

Recommended

  • Proxy HTTP traffic through apache2 to checkbox.

This way, apache2 can handle all of your static files (which is very good - much better than the debug server built into Flask) and acts as a reverse proxy for your dynamic content, passing those requests to Flask.

Here's a link to the official documentation on configuring Flask with Apache + mod_wsgi.

Edit 1 - Clarification for @Djack

Proxy HTTP traffic for Flask via apache2

When the request arrives at the server on port 80 ( HTTP ) or port 443 ( HTTPS ), a web server, such as Apache or Nginx, processes the connection of the request and tells what to do with it. In our case, the received request must be configured for transmission through a flag in the WSGI protocol and processed by Python code. This is the "dynamic" part.

reverse proxy for dynamic content

There are several advantages to setting up your web server as described above;

  • SSL termination. The web server will be optimized to handle HTTPS requests with only a small configuration. Do not “collapse your own” in Python, which is probably very insecure in comparison.
  • Security. Opening a port on the Internet requires careful consideration of security. The flash development server is not designed for this and may have open errors or security issues compared to a web server designed for this purpose. Please note that a poorly configured web server can also be unsafe!
  • Processing static files. Flask's built-in web server can handle static files, but this is not recommended; Nginx / Apache processes static files such as images, CSS, Javascript files much more efficiently and only transfers “dynamic” requests (those where content is often read from the database or content changes) processed by Python code.
  • + more. This borders on a question for this question. If you want more information, do some research in this area.
+60
Nov 26 '13 at 9:30
source share

1- Stop other applications using port 80. 2- start the application with port 80:

 if __name__ == '__main__': app.run(host='0.0.0.0', port=80) 
+359
Mar 16 '15 at 14:40
source share

For an external visible server where you are not using apache or another web server, just type

 flask run --host=0.0.0.0 --port=80 
+114
Aug 24 '17 at 11:49 on
source share

If you use the following to change the port or host:

 if __name__ == '__main__': app.run(host='0.0.0.0', port=80) 

use the following code to start the server (my main input for flask is app.py):

 python app.py 

Instead of using:

 flask run 
+16
Jan 18 '19 at 12:36
source share

If you want your application to be on the same port, i.e. port = 5000, then simply run this command in your terminal:

 fuser -k 5000/tcp 

and then run:

 python app.py 

If you want to work with the specified port, for example, if you want to work with port = 80, in the main file just specify this:

 if __name__ == '__main__': app.run(host='0.0.0.0', port=80) 
+8
Apr 13 '19 at 17:39
source share

Your problem is that you already have an apache web server running that already uses port 80. So you can:

  • Kill apache: you should probably do this via /etc/init.d/apache2 stop , and not just kill them.

  • Deploy the flash application in the apache process as described in the apache flask .

+6
Nov 26 '13 at 9:32
source share

I had to set FLASK_RUN_PORT in my environment to the specified port number. When I launched the application, it selected the port I needed.

+5
Nov 26 '18 at 10:14
source share

You do not need to change the port number for your application, just configure your www server (nginx or apache) for proxy server requests so that the port is checked. Pay attention to uWSGI .

+3
Nov 26 '13 at 9:31 on
source share

set the port using app.run(port=80,debug=True) for debug must be set to true, if enabled

+3
Feb 27 '19 at 17:23
source share

I can’t run my application at 80, it starts at 5000 just fine. I don’t see how Apache works? what else can i check? We want to work at 80, so we can use the URL without specifying a port.

 > server.run(debug=False, host=args.host, port=args.port) File "/home/ryanicky/.local/lib/python3.6/site-packages/flask/app.py", line > 944, in run > run_simple(host, port, self, **options) File "/home/ryanicky/.local/lib/python3.6/site-packages/werkzeug/serving.py", > line 1009, in run_simple > inner() File "/home/ryanicky/.local/lib/python3.6/site-packages/werkzeug/serving.py", > line 962, in inner > fd=fd, File "/home/ryanicky/.local/lib/python3.6/site-packages/werkzeug/serving.py", > line 805, in make_server > host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd File > "/home/ryanicky/.local/lib/python3.6/site-packages/werkzeug/serving.py", > line 698, in __init__ > HTTPServer.__init__(self, server_address, handler) File "/usr/lib64/python3.6/socketserver.py", line 456, in __init__ > self.server_bind() File "/usr/lib64/python3.6/http/server.py", line 136, in server_bind > socketserver.TCPServer.server_bind(self) File "/usr/lib64/python3.6/socketserver.py", line 470, in server_bind > self.socket.bind(self.server_address) OSError: [Errno 98] Address already in use [ryanicky@webstr WebSTR]$ systemctl status httpd Unit > httpd.service could not be found. 
0
Jul 03 '19 at 23:30
source share



All Articles