[英]How to access a Python flask application running on AWS EC2 remotely?
I have a flask app that I have cloned onto my aws ec2 instance.我有一个烧瓶应用程序,我已经将它克隆到我的 aws ec2 实例上。 I can only run it using a virtual environment (which I activate by running the following):
我只能使用虚拟环境运行它(我通过运行以下命令激活它):
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install --upgrade pip
$ pip install flask==1.1.1
Below is my app:下面是我的应用程序:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
It runs just fine when executing env FLASK_APP=app.py flask run
.执行
env FLASK_APP=app.py flask run
时它运行得很好。 The problem is, I'd like to access the exposed routes remotely using my aws ec2's public IP or hostname.问题是,我想使用我的 aws ec2 的公共 IP 或主机名远程访问公开的路由。 I get a tiemout error whenever I try to access it though.
但是,每当我尝试访问它时,都会出现 tiemout 错误。 I think this is because i'm running the flask app in a virtual environment but I'm not sure.
我认为这是因为我在虚拟环境中运行 Flask 应用程序,但我不确定。 I can't find any good tutorials on how to expose this.
我找不到关于如何公开这一点的任何好的教程。 Where am I going wrong here?
我哪里出错了?
As you're running the development server, which isn't meant for production.当您运行不用于生产的开发服务器时。 I would say the best way to connect to this app from just your own machine , is with an ssh tunnel:
我想说从你自己的机器连接到这个应用程序的最好方法是使用 ssh 隧道:
ssh -L 5000:localhost:5000 ec2_addr
You can then point your web-browser to http://localhost:5000/
on your client machine, which will be tunneled to port 5000 on the EC2 instance.然后,您可以将 Web 浏览器指向客户端计算机上的
http://localhost:5000/
,该计算机将通过隧道连接到 EC2 实例上的端口 5000。 This is a fast way to connect to a Flask (or any) server on a remote Linux box.这是连接到远程 Linux 机器上的 Flask(或任何)服务器的快速方法。 The tunnel is destroyed when you stop that ssh session.
当您停止该 ssh 会话时,隧道将被破坏。
I'd like to access the exposed routes remotely using my aws ec2's public IP or hostname.
我想使用我的 aws ec2 的公共 IP 或主机名远程访问公开的路由。
The timeout isn't because it's running in a virtual environment: You'll probably find it's because you need to assign Security Groups to your instance through the EC2 console.超时不是因为它在虚拟环境中运行:您可能会发现这是因为您需要通过 EC2 控制台为您的实例分配安全组。 These allow you to open certain ports on the public IP.
这些允许您在公共 IP 上打开某些端口。
See this other answer I wrote, regarding EC2 security groups .请参阅我写的有关EC2 安全组的其他答案。
However be careful .不过要小心。 You shouldn't expose the development server in this manner.
您不应该以这种方式公开开发服务器。 There are a number of tutorials like this one from digital ocean which cover deploying a flask app behind
gunicorn
and nginx
with a Let's Encrypt
SSL cert.有很多这样的教程来自数字海洋,其中包括使用
Let's Encrypt
SSL 证书在gunicorn
和nginx
后面部署烧瓶应用程序。 You should find yourself in a position where your security group exposes port 80 and 443, with requests to port 80 being redirected to the https
URL by the nginx configuration.您应该会发现自己处于安全组公开端口 80 和 443 的位置,对端口 80 的请求被 nginx 配置重定向到
https
URL。
If this sounds like a whole load of hassle / you don't have Linux skills / you don't want to learn Linux skills, then these are common reasons people pick managed services like AWS Elastic Beanstalk, to which you can deploy your flask app (official guide) , without having to worry about server config.如果这听起来很麻烦/您没有 Linux 技能/您不想学习 Linux 技能,那么这些是人们选择 AWS Elastic Beanstalk 等托管服务的常见原因,您可以在其中部署您的 Flask 应用程序(官方指南) ,无需担心服务器配置。 Heroku is another (non AWS) service which offers such features.
Heroku 是提供此类功能的另一项(非 AWS)服务。
It really depends on what you require / wish to gain.这实际上取决于您需要/希望获得什么。 Stay safe!
注意安全!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.