Run Flask on Port 80: A Complete Guide for Beginners (2026)
Learn to run your Flask app on port 80 for direct access without specifying a port number. This guide covers reverse proxy setup and common errors.
Run Flask on Port 80: A Complete Guide for Beginners (2026)
Running a Flask application on port 80 instead of the default port 5000 can simplify access by allowing users to connect directly to your domain without specifying a port number. However, running on port 80, which is a privileged port, presents unique challenges, especially on Unix-like systems. This tutorial will guide you through the process of configuring Flask to run on port 80, addressing common issues and providing troubleshooting tips.
Key Takeaways
- Understand why port 80 is significant and how it differs from other ports.
- Learn to set up Flask to run on port 80 using different methods.
- Configure system settings to allow Flask to bind to port 80.
- Discover how to use a reverse proxy as an alternative solution.
- Troubleshoot common errors encountered when binding to port 80.
Introduction
Flask is a popular micro web framework for Python, often used for building web applications quickly and with minimal overhead. By default, Flask runs on port 5000, which is suitable for development and testing. However, in production environments, applications often need to run on port 80, the default port for HTTP traffic, to make them accessible without specifying a port number in the URL.
Configuring Flask to run on port 80 can improve user experience and is a common requirement for deploying web applications. This tutorial will cover the necessary steps to achieve this, explain the significance of port 80, and offer solutions to potential issues you may encounter.
Prerequisites
- Basic understanding of Flask and Python.
- Access to a Unix-like environment (Linux or macOS) or Windows with administrative privileges.
- Flask application installed and running on port 5000.
- Familiarity with SSH and command-line operations.
Step 1: Understand Privileged Ports
Ports below 1024 are considered privileged ports on Unix-like systems. This means that only processes with root or equivalent privileges can bind to them. Port 80 is among these privileged ports, as it is the standard port for HTTP traffic.
Attempting to bind a Flask application to port 80 without the necessary privileges will result in a permission denied error. Understanding this limitation is crucial for troubleshooting and configuring your environment correctly.
Step 2: Run Flask as a Root User (Not Recommended)
One direct approach to running Flask on port 80 is to start the application with root privileges. However, this method is generally discouraged due to security risks associated with running web applications as root.
sudo python app.pyWhile this command allows Flask to bind to port 80, it exposes your system to potential vulnerabilities. Therefore, it's advisable to explore more secure alternatives.
Step 3: Use a Reverse Proxy
A safer and more common practice is to use a reverse proxy server such as Nginx or Apache. A reverse proxy can forward requests from port 80 to your Flask application running on a non-privileged port, like 5000.
3.1: Install Nginx
sudo apt update
sudo apt install nginx3.2: Configure Nginx
Create a configuration file for your Flask application:
sudo nano /etc/nginx/sites-available/flaskappAdd the following configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Enable the configuration by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/flaskapp /etc/nginx/sites-enabledRestart Nginx to apply the changes:
sudo systemctl restart nginxNow, your Flask application should be accessible via http://example.com without specifying a port.
Step 4: Use Port Forwarding (Alternative)
Another approach is to use port forwarding to redirect traffic from port 80 to port 5000. This can be achieved using the iptables command on Linux:
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 5000This command forwards incoming traffic on port 80 to port 5000, where your Flask application is running.
Common Errors/Troubleshooting
- Permission Denied: Ensure that you have the necessary privileges when attempting to bind to port 80.
- Nginx Configuration Errors: Double-check your Nginx configuration for typos or syntax errors.
- Firewall Blocking: Ensure that your firewall settings allow incoming traffic on port 80.
Conclusion
Running a Flask application on port 80 can improve accessibility and user experience by eliminating the need for specifying a non-standard port in URLs. While running Flask as a root user may seem like a quick fix, it poses security risks, and using a reverse proxy or port forwarding is recommended. By understanding the challenges associated with privileged ports and exploring secure alternatives, you can effectively deploy your Flask application on port 80.
Frequently Asked Questions
Why can't I run Flask on port 80 directly?
Ports below 1024 are privileged, requiring root privileges to bind to them. This is for security reasons.
What is a reverse proxy, and why use it?
A reverse proxy forwards client requests to backend servers, providing security and the ability to handle traffic on privileged ports like 80.
Is it safe to run Flask as a root user?
It's not recommended due to security risks. Alternatives like reverse proxies or port forwarding are safer.