【问题标题】:Quickest way to deploy a Flask app on ubuntu [duplicate]在ubuntu上部署Flask应用程序的最快方法[重复]
【发布时间】:2016-11-23 09:21:57
【问题描述】:

我知道flask deployment options,但是对于我编写的这么简单的烧瓶应用程序,所有这些选项对我来说似乎都是多余的。它适用于烧瓶自己的内置服务器。

workon kittapp
python run.py // runs the webserver at configured port, everything's fine

这是我的 run.py 文件

from kittapp import app

if __name__ == '__main__':
    app.run(host=app.config['HOST'], port=app.config['PORT'], debug=app.config['DEBUG'])

第一个问题是,一旦我退出 ssh 会话,服务器就会停止,所以我尝试使用一次性 cron 作业、at nownohup 命令将任务推送到后台。它工作正常,但第二个问题是几个小时后,python 进程(被推送到 bg)不再运行,webapp 已关闭。

我知道我最终需要为此编写一个类似守护进程的启动脚本。只是想看看是否有任何其他简单而可靠的解决方案可以在 ubuntu 机器上部署烧瓶应用程序?

【问题讨论】:

  • 检查日志为什么服务器宕机,最好用主管或类似的东西包装它。
  • 在哪里可以查看烧瓶应用程序的日志?
  • nohup.out 或者你在运行 nohup 时应该定义的东西

标签: python ubuntu deployment flask


【解决方案1】:

您可能正在寻找Supervisor

Supervisor 是类 Unix 系统的进程控制工具。它提供了一个易于使用的界面,仅使用config files 即可构建和管理类似守护进程的受监督进程。

您可以为您的 Flask 应用程序创建一个简单的配置文件,将其添加到 Supervisor,启动它,您就完成了。方法如下:

# Install supervisor
sudo apt install supervisor -y # Or pip install supervisor 

# Start supervisor service
sudo service supervisor start

# Create your config file
# I'll add a sample kittapp.conf later on...
sudo vim /etc/supervisor/conf.d/kittapp.conf

# Add and start your job
sudo supervisorctl add kittapp
sudo supervisorctl start kittapp

简单吧?但是在使用 virtualenvs 时有一个小问题,你就是这样。请注意workon 是一个shell 函数,而不是PATH 中的可执行文件。在运行作业之前,Supervisor 不会获取您的 ~/.bash*~/.zsh* 文件。它不知道它们。因此,workon 将不可用。相反,我们需要正确设置 python 路径以指向我们的 venv 的bin/ 目录。可以使用environment 指令来完成。

这是一个最小的 Supervisor 配置文件:

[program:kittapp]
environment=PATH="/home/user/virtualenvs/kittapp/bin"    # Point it to the bin/ directory of your venv
command=python run.py                                    # Here's the actual command that supervisor needs to run in order to start the server
directory=/var/www/kittapp                               # Instructs supervisor to cd into this directory before running the command
stdout_logfile=/var/www/kittapp/logs/supervisor.log      # Write logs to this file
redirect_stderr=true                                     # Redirect errors to supervisor output, so you'll have your errors in the log file  

好吧,这就是它的全部内容。

你可能想看看这个关于 Supervisor 的 Laracasts 课程:
https://laracasts.com/lessons/supervise-this

【讨论】:

  • 感谢您的回答。我去看看。
  • 与主管一起启动并运行。感谢您的视频链接。让我们看看它是否继续运行。
  • 我同意。为了澄清起见,我没有建议 OP 运行 Flask 的开发服务器。问题本身是关于 Flask 的开发服务器以及如何可靠地运行它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-05-27
  • 2020-10-03
相关资源
最近更新 更多