Ubuntu Server 16.04 LTS EC2实例Flask应用127.0.0.1:5000无法外部访问求助
Hey Samuel, let's troubleshoot why your Flask app isn't accessible from outside the EC2 instance. Since it works fine on Windows, the issue is almost certainly tied to network binding, firewall rules, or EC2 security settings. Let's go through each step one by one:
1. Make Flask Listen on All Network Interfaces
By default, Flask only binds to 127.0.0.1 (the local loopback), which means it won't accept connections from outside the server. You need to tell it to listen on all available interfaces:
Option 1: Run Flask with command-line arguments
Stop your current Flask process and restart it with:flask run --host=0.0.0.0 --port=5000(If you're using a custom app file, add
--app your_app_file.pyif needed.)Option 2: Update your Flask code
In your app's entry point, modify theapp.run()line to explicitly set the host:if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)This ensures the app starts listening on all interfaces every time you run it.
2. Allow Port 5000 Through Ubuntu's UFW Firewall
Ubuntu EC2 instances often have UFW (Uncomplicated Firewall) enabled by default. You need to allow incoming traffic on port 5000:
- Run this command to add the rule:
sudo ufw allow 5000/tcp - Verify the rule is active with:
You should see a line likesudo ufw status5000/tcp ALLOW Anywhere.
3. Configure EC2 Security Group Rules
This is the most commonly missed step! AWS EC2 uses security groups as a virtual firewall for your instance. You need to open port 5000 to external traffic:
- Go to the AWS EC2 Console, find your instance, and click on its associated Security Group (under the "Security" tab).
- Click "Edit inbound rules".
- Add a new rule:
- Type: Custom TCP
- Port range: 5000
- Source: For testing, set this to
0.0.0.0/0(allows all IPs). Once you confirm access, you can restrict it to your specific public IP for security.
- Save the rules.
4. Verify Local Connectivity First
Before testing from your local machine, confirm the app is running correctly on the EC2 instance itself. SSH into the instance and run:
curl localhost:5000
If this returns your app's response, the Flask app is working. If not, check that you installed all dependencies (use pip3 install -r requirements.txt if you have a requirements file) and that Python 3.6 is properly set up (Ubuntu might have python3.6 available via sudo apt-get install python3.6 python3.6-pip).
5. Check for Port Conflicts
If port 5000 is already in use by another process, Flask won't be able to bind to it. Check what's using the port with:
sudo lsof -i :5000 # Or alternatively: sudo netstat -tulpn | grep 5000
If you see another process listed, you can kill it (with sudo kill <PID>) or change your Flask app to use a different port (like 8080) by updating the --port argument or app.run() port value.
Start with step 1, then work your way through—most of the time, it's a combination of binding to 0.0.0.0 and updating the EC2 security group. Let me know if any step gives you trouble!
内容的提问来源于stack exchange,提问作者Samuel Nde

