Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Flask to run on port 80?

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

But is it possible to simply access it at http://example.com? I'm assuming that means I have to change the port from 5000 to 80. But when I try that on Flask, I get this error message when I run it.

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? Is that safe? Or is there another way to keep Flask running on port 5000 but have the main website domain redirect somehow?

like image 935
quantumtremor Avatar asked Nov 26 '13 09:11

quantumtremor


People also ask

How do we change the default port in flask?

To change the host and port, pass them as options to the command. Pass --help for the full list of options. Setting the SERVER_NAME config will not affect the command either, as the command can't see the app's config. Never expose the dev server to the outside (such as binding to 0.0.


2 Answers

1- Stop other applications that are using port 80. 2- run application with port 80 :

if __name__ == '__main__':       app.run(host='0.0.0.0', port=80) 
like image 151
Amir Mofakhar Avatar answered Oct 16 '22 06:10

Amir Mofakhar


For externally visible server, where you don't use apache or other web server you just type

flask run --host=0.0.0.0 --port=80 
like image 25
Harun-Ur-Rashid Avatar answered Oct 16 '22 04:10

Harun-Ur-Rashid