How to enable port 5000 on AWS ubuntu [closed]

筅森魡賤 提交于 2020-01-01 01:45:10

问题


I have a flask app running on AWS Ubuntu server on port 5000 (flask runs default on port 5000). But when I try to access the server on that port, it never connects.

I added a security group on AWS console as Custom TCP on port 5000 for any ip address 0.0.0.0/0, but still I cannot access it.

Do I have to restart the server? Or am I missing anything?

Let me know any additional information to provide.


回答1:


In addition to allowing access to port 5000 via the Security Group, you also need to ensure that your app is listening on an IP which can accept TCP connections from outside. To listen on all IPs, in your app, use:

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug = False)

Instead of:

if __name__ == '__main__':
    app.run(host='127.0.0.1', debug = False)

To see what address your application is listening on, you can run this command:

netstat -an | grep :5000

After making these changes, you will want to restart your Flask application.

I'm assuming you're just using this for development and testing, since you're keeping it on port 5000, but, when you're ready to deploy your application into production, you need to put it behind a real webserver. I would recommend using nginx with uWSGI. Here is a guide to configuring Flask + nginx + uWSGI, and here is the official documentation from Flask on the subject.




回答2:


In addition to @Will's answer, its possible that whichever Ubuntu AMI you're using came with restrictive iptables rules by default. Use:

sudo iptables -L

to list any current rules that exist. Use:

sudo iptables -A INPUT -p tcp --dport 5000 -j ACCEPT

to open the port if necessary.



来源:https://stackoverflow.com/questions/31864499/how-to-enable-port-5000-on-aws-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!