如何在具有崩溃保护功能的 Lightsail 上部署 Flask 应用程序?

How can I deploy a flask app on Lightsail with crash protection?

我正在寻求有关部署我的 Flash 应用程序的帮助。我已经编写了该应用程序并且运行良好。我目前在我的烧瓶代码目录中使用以下命令:

sudo uwsgi --socket 0.0.0.0:70 --protocol=http -w AppName:app --buffer-size=32768

这是在我的 Amazon Lightsail 实例上。我将实例链接到静态 public IP,如果我导航到该网站,效果很好。但是,即使在退出 Lightsail 后,为了让命令在后台连续 运行,我首先启动 screen 命令,执行上面的代码行,然后使用 [= 分离屏幕12=].

问题是,如果应用程序崩溃(这是可以理解的,因为它非常大并且正在开发中),或者如果命令 运行ning 太久,进程将被终止,并且它不再提供服务。

我正在寻找一种更好的方法来在 Amazon Lightsail 上部署 Flask 应用程序,以便在发生崩溃时重新部署该应用程序,而无需我进行任何交互。

通常,您会为 systemd 编写自己的 unit 文件,以保留您的应用程序 运行,当它崩溃时自动重启并在您启动实例时启动。

许多教程 展示了如何编写这样的 unit 文件。一些例子:

您可以使用pm2

Starting an application with PM2 is straightforward. It will auto discover the interpreter to run your application depending on the script extension. This can be configurable via the Ecosystem config file, as I will show you later on this article.

所有你需要安装pm2然后

pm2 start appy.py

Great, this application will now run forever, meaning that if the process exit or throw an exception it will get automatically restarted. If you exit the console and connect again you will still be able to check the application state.

列出由 PM2 运行 管理的应用程序:

pm2 ls

您还可以查看日志

pm2 logs

在服务器重启时保持进程活动

If you want to keep your application online across unexpected (or expected) server restart, you will want to setup init script to tell your system to boot PM2 and your applications.

使用 PM2 非常简单,只需 运行 这个命令(没有 sudo):

pm2 startup

Pm2 Manage-Python-Processes