Running Flask Applications on Port 80

Running a Flask application on port 80 is often necessary for production environments where you want users to access your web application without specifying a custom port. By default, Flask runs on port 5000, but changing this to port 80 requires some additional considerations due to the way operating systems handle privileged ports.

Understanding Port 80

Port 80 is one of the well-known ports and is used for HTTP traffic by default. Because it’s a privileged port (ports less than 1024), running an application on this port usually requires root or administrator privileges. This is why, when you try to run your Flask application directly on port 80 without proper configuration, you might encounter permission errors.

Stopping Other Applications

If another service, like Apache or Nginx, is already using port 80, you’ll need to stop that service before running your Flask app. You can identify which processes are using port 80 by running the lsof -i :80 command on Unix-based systems or netstat -tlnp | grep 80 on Linux.

To stop Apache, for example, you would use:

sudo service apache2 stop

Or, to kill a specific process ID (replace PID with the actual process number):

kill PID

Running Flask on Port 80

After ensuring port 80 is available, you can run your Flask application by specifying the host and port:

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

Then, start your server with python app.py instead of using flask run.

Using a Reverse Proxy

For production environments, it’s recommended to use a full-fledged web server like Apache or Nginx as a reverse proxy for your Flask application. This setup allows the web server to handle static files efficiently and pass dynamic requests to your Flask app.

Here’s an example configuration for Apache with mod_wsgi:

  1. Install mod_wsgi if it’s not already installed.
  2. Create a configuration file for your site, usually in /etc/apache2/sites-available/, specifying the proxy settings:
WSGIScriptAlias / /path/to/your/app.wsgi

<Directory /path/to/your/app>
    WSGIProcessGroup your_app
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>
  1. Enable the site configuration and restart Apache:
sudo a2ensite your_site_config
sudo service apache2 restart

Environment Variables with python-dotenv

For convenience, you can use python-dotenv to manage environment variables for Flask, including the host and port:

  1. Install python-dotenv.
  2. Create a .flaskenv file in your project root.
  3. Define FLASK_RUN_HOST and FLASK_RUN_PORT:
FLASK_APP=application.py
FLASK_RUN_HOST=localhost
FLASK_RUN_PORT=80
  1. Run Flask with flask run.

Conclusion

Running a Flask application on port 80 can be straightforward if you’re aware of the considerations for privileged ports and potential conflicts with other services. For production environments, using a reverse proxy setup not only improves security but also enhances performance by leveraging the strengths of both your web server and Flask.

Leave a Reply

Your email address will not be published. Required fields are marked *