How do I get Flask to run on port 80?
Asked Answered
N

16

345

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?

Notebook answered 26/11, 2013 at 9:22 Comment(3)
you can use it flask with apache.Exanthema
the title of the question doesn't fit the question. perhaps it should read 'how not to get other things to run on port 80'. i'm curious how to change the port flask is running on which is what this question asks, but is never mentioned how to hereEldoria
For anyone struggling to get anywhere with the provided answers, check out this related answer which uses authbind.Orlon
C
102

So it's throwing up that error message because you have apache2 running on port 80.

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

If it's for production either:

Not Recommended

  • Stop apache2 first;

Not recommended as it states in the documentation:

You can use the builtin server during development, but you should use a full deployment option for production applications. (Do not use the builtin development server in production.)

Recommended

  • Proxy HTTP traffic through apache2 to Flask.

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

Here's a link to the official documentation about setting up Flask with Apache + mod_wsgi.

Edit 1 - Clarification for @Djack

Proxy HTTP traffic to Flask through apache2

When a request comes to the server on port 80 (HTTP) or port 443 (HTTPS) a web server like Apache or Nginx handles the connection of the request and works out what to do with it. In our case a request received should be configured to be passed through to Flask on the WSGI protocol and handled by the Python code. This is the "dynamic" part.

reverse proxy for dynamic content

There are a few advantages to configuring your web server like the above;

  • SSL Termination - The web server will be optimized to handle HTTPS requests with only a little configuration. Don't "roll your own" in Python which is probably very insecure in comparison.
  • Security - Opening a port to the internet requires careful consideration of security. Flask's development server is not designed for this and could have open bugs or security issues in comparison to a web server designed for this purpose. Note that a badly configured web server can also be insecure!
  • Static File Handling - It is possible for the builtin Flask web server to handle static files however this is not recommended; Nginx/Apache are much more efficient at handling static files like images, CSS, Javascript files and will only pass "dynamic" requests (those where the content is often read from a database or the content changes) to be handled by the Python code.
  • +more. This is bordering on scope for this question. If you want more info do some research into this area.
Chaucerian answered 26/11, 2013 at 9:30 Comment(2)
That's a great answer indeed! Can you demystify in plain English the highly technical jargon you used "Proxy HTTP traffic to Flask through apache2" and "reverse proxy for dynamic content"?Tilth
@Djack - updated answer however it's bordering on scope for the question so please have a search online for more info.Chaucerian
L
580

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)
Lidstone answered 16/3, 2015 at 14:40 Comment(6)
its not working for me. it should be: flask run --host=0.0.0.0Kopje
@jOSe I tried using PyCharm on Ubuntu, works just fine.Papp
@jOSe Apache is not needed for linux to run, you can uninstall it, or change it's port.Modiste
It is important to note that using this you do not run flask run anymore but python name_of_the_file.py as mentioned in the answer from @mucaStrategy
Very important note by @nck. If u want to use flask run you need to use the port as argument flask run -p 80 or flask run --port 80 as shown in the answer by @KopjeSeeing
This isn't going to work on any Linux system as port 80 requires rootOleviaolfaction
K
257

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
Kopje answered 24/8, 2017 at 11:49 Comment(4)
Can this command able to be run utilizing env vars? I keep trying CMD flask run --host=0.0.0.0 --port=$PORT within my Dockerfile (for heroku reasons) and I keep getting errors about needing an integer for my portCoefficient
This worked fine on my local (laptop) but when I used it on my AWS EC2 instance, it says "Permission Denied". If I add "sudo" at the beginning, the it says "flask" command is not recognized...Americana
@Americana Maybe flask is not installed in root (sudo) userKopje
Hi @Harun-Ur-Rashid, that's probably the case - I installed it in an virtual environment. I'll check out how to fix it. Thanks for your reply.Americana
C
102

So it's throwing up that error message because you have apache2 running on port 80.

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

If it's for production either:

Not Recommended

  • Stop apache2 first;

Not recommended as it states in the documentation:

You can use the builtin server during development, but you should use a full deployment option for production applications. (Do not use the builtin development server in production.)

Recommended

  • Proxy HTTP traffic through apache2 to Flask.

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

Here's a link to the official documentation about setting up Flask with Apache + mod_wsgi.

Edit 1 - Clarification for @Djack

Proxy HTTP traffic to Flask through apache2

When a request comes to the server on port 80 (HTTP) or port 443 (HTTPS) a web server like Apache or Nginx handles the connection of the request and works out what to do with it. In our case a request received should be configured to be passed through to Flask on the WSGI protocol and handled by the Python code. This is the "dynamic" part.

reverse proxy for dynamic content

There are a few advantages to configuring your web server like the above;

  • SSL Termination - The web server will be optimized to handle HTTPS requests with only a little configuration. Don't "roll your own" in Python which is probably very insecure in comparison.
  • Security - Opening a port to the internet requires careful consideration of security. Flask's development server is not designed for this and could have open bugs or security issues in comparison to a web server designed for this purpose. Note that a badly configured web server can also be insecure!
  • Static File Handling - It is possible for the builtin Flask web server to handle static files however this is not recommended; Nginx/Apache are much more efficient at handling static files like images, CSS, Javascript files and will only pass "dynamic" requests (those where the content is often read from a database or the content changes) to be handled by the Python code.
  • +more. This is bordering on scope for this question. If you want more info do some research into this area.
Chaucerian answered 26/11, 2013 at 9:30 Comment(2)
That's a great answer indeed! Can you demystify in plain English the highly technical jargon you used "Proxy HTTP traffic to Flask through apache2" and "reverse proxy for dynamic content"?Tilth
@Djack - updated answer however it's bordering on scope for the question so please have a search online for more info.Chaucerian
T
47

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 entrance for flask is app.py):

python app.py

instead of using:

flask run
Then answered 18/1, 2019 at 12:36 Comment(3)
Says permission denied.Homes
@GauravHazra port 80 is a privileged port (as are all ports below 1024), which means you need to run it as a superuser: sudo python app.py.Herries
Why there is a flask method app.run with parameters like host or port if it doesn't work when you use flask run? Is better to use python app.py instead flask command? I've read a lot about that but nobody was able to make me understand why is the sense of that weird behaviorPenguin
T
42

A convinient way is using the package python-dotenv: It reads out a .flaskenv file where you can store environment variables for flask.

  • pip install python-dotenv
  • create a file .flaskenv in the root directory of your app

Inside the file you specify:

FLASK_APP=application.py
FLASK_RUN_HOST=localhost
FLASK_RUN_PORT=80

After that you just have to run your app with flask run and can access your app at that port.

Please note that FLASK_RUN_HOST defaults to 127.0.0.1 and FLASK_RUN_PORT defaults to 5000.

Thain answered 22/11, 2019 at 16:34 Comment(0)
T
33

This is the only solution that worked for me on Ubuntu-18.

Inside the file app.py , use:

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

The code above will give the same permission error unless sudo is used to run it:

sudo python3 app.py
Thinia answered 19/3, 2020 at 2:29 Comment(0)
B
24

you can easily disable any process running on port 80 and then run this command

flask run --host 0.0.0.0 --port 80

or if u prefer running it within the .py file

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
Baby answered 13/7, 2020 at 16:13 Comment(0)
L
13

I had to set FLASK_RUN_PORT in my environment to the specified port number. Next time you start your app, Flask will load that environment variable with the port number you selected.

Lawford answered 26/11, 2018 at 10:14 Comment(0)
M
13

If you want your application on same port i.e port=5000 then just in your terminal run this command:

fuser -k 5000/tcp

and then run:

python app.py

If you want to run on a specified port, e.g. if you want to run on port=80, in your main file just mention this:

if __name__ == '__main__':  
    app.run(host='0.0.0.0', port=80)
Maddock answered 13/4, 2019 at 17:39 Comment(0)
W
12

On my scenario the following steps worked like a charm:

  • Installing the package:

    pip install --upgrade pip
    pip install python-dotenv
    
  • Creating a hidden file in my app directory "flaskr/.flaskenv"

  • Adding the following content:

    FLASK_APP=flaskr
    FLASK_RUN_HOST=localhost
    FLASK_RUN_PORT=8000
    
  • Finally, run the flask command one more time:

    flask run
    
  • The version which I am working on is:

    pip freeze |grep -i flask
    Flask==1.1.1
    
Warta answered 13/2, 2020 at 14:7 Comment(0)
N
8

Your issue is, that you have an apache webserver already running that is already using port 80. So, you can either:

  1. Kill apache: You should probably do this via /etc/init.d/apache2 stop, rather than simply killing them.

  2. Deploy your flask app in your apache process, as flask in apache describes.

Nowadays answered 26/11, 2013 at 9:32 Comment(0)
N
7

You don't need to change port number for your application, just configure your www server (nginx or apache) to proxy queries to flask port. Pay attantion on uWSGI.

Neutrality answered 26/11, 2013 at 9:31 Comment(0)
H
6

set the port with app.run(port=80,debug=True) you should set debug to true when on dev

Hasidism answered 27/2, 2019 at 17:23 Comment(0)
F
4

Easiest and Best Solution

Save your .py file in a folder. This case my folder name is test. In the command prompt run the following

c:\test> set FLASK_APP=application.py
c:\test> set FLASK_RUN_PORT=8000
c:\test> flask run

----------------- Following will be returned ----------------

 * Serving Flask app "application.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/Aug/2019 09:40:04] "[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [23/Aug/2019 09:40:04] "[33mGET /favicon.ico HTTP/1.1[0m" 404 -

Now on your browser type: http://127.0.0.1:8000. Thanks

Figment answered 23/8, 2019 at 14:47 Comment(1)
He needs to run it on 80 which is a special port. He needs to stop other stuff first.Burnt
G
1

I solved 2 times and solved it with 2 solution

The first solution, follow these steps:

1 open cmd

2 run ipconfig at cmd

3 copy IPv4 Address from cmd

4 Use your IPv4 as the local host in the next:

app.run(debug=True, port=5000, host='localhost')

at replace 'localhost' with your IPv4 Address

5 for your front end, do the same of step 4 like:

at 'http://localhost:5000/auth' replace 'localhost' with your IPv4 Address

Note: use the same port for {the frontend, and the backend}

6 run your both projects together(the backend and the frontend)


The second time, the last solution didn't work, and I solved it with this:

  • at the mobile side I used that URL: "http://10.0.2.2:8000" and at the Flask side used

app.run(debug=True, port=8000, host='localhost')

Gamelan answered 6/8, 2022 at 10:57 Comment(0)
C
0

Do it like that:

if __name__ ==  '__main__' :
    app.run( host='0.0.0.0', port=80)
Callus answered 13/3 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.