How do I get Flask to run on port 80?
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 throughapache2
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.
How can I change the host and port that the flask command uses?
The flask
command is separate from the flask.run
method. It doesn't see the app or its configuration. To change the host and port, pass them as options to the command.
flask run -h localhost -p 3000
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.0.0
). Use a production WSGI server such as uWSGI or Gunicorn.
gunicorn -w 2 -b 0.0.0.0:3000 myapp:app
Running flask on port 80 in linux
You need root to run at port 80.
sudo python app.py
Python run flask on https with port 80
The problem is that you are running your app on port 80(default port for http) with https configured.
When accessing https://localhost
the browser tries to access the port 443 (the default port for https), while your app is hosted on port 80.
Changing the hosting port to 443 should allow you to access https://localhost
without specifying the port.
where to define port for Flask
Port can be specified in the parameters of the run
function of the app. For instance:
from flask import Flask
app = Flask(__name__)
...
host = '0.0.0.0'
port = 5001
app.run(host=host, port=port)
Configure Flask dev server to be visible across the network
While this is possible, you should not use the Flask dev server in production. The Flask dev server is not designed to be particularly secure, stable, or efficient. See the docs on deploying for correct solutions.
The --host
option to flask run
, or the host
parameter to app.run()
, controls what address the development server listens to. By default it runs on localhost
, change it to flask run --host=0.0.0.0
(or app.run(host="0.0.0.0")
) to run on all your machine's IP addresses.
0.0.0.0
is a special value that you can't use in the browser directly, you'll need to navigate to the actual IP address of the machine on the network. You may also need to adjust your firewall to allow external access to the port.
The Flask quickstart docs explain this in the "Externally Visible Server" section:
If you run the server you will notice that the server is only
accessible from your own computer, not from any other in the network.
This is the default because in debugging mode a user of the
application can execute arbitrary Python code on your computer.If you have the debugger disabled or trust the users on your network,
you can make the server publicly available simply by adding--host=0.0.0.0
to the command line:$ flask run --host=0.0.0.0
This tells your operating system to listen on all public IPs.
Quick way to run flask on 0.0.0.0:80 in company's intranet
Please try to run it with
python <yourfile>.py
flask run might be a cause of your problem
Related Topics
Why Does My Pandas Dataframe Not Display New Order Using 'Sort_Values'
How Does _Contains_ Work for Ndarrays
Python: Best Way to Add to Sys.Path Relative to the Current Running Script
Convert Floating Point Number to a Certain Precision, and Then Copy to String
How to Run All Python Unit Tests in a Directory
Typeerror: Str Does Not Support Buffer Interface
How to Change a Module Variable from Another Module
Removing Index Column in Pandas When Reading a CSV
Group by Pandas Dataframe and Select Latest in Each Group
Python: Download a File from an Ftp Server
How to Make Environment Variable Changes Stick in Python
Create PDF from a List of Images
Heapq with Custom Compare Predicate
Comparing Python Dictionaries and Nested Dictionaries
Stop/Start/Pause in Python Matplotlib Animation