在后台启动Flask服务器

编程入门 行业动态 更新时间:2024-10-07 22:27:43
本文介绍了在后台启动Flask服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个flask应用程序,目前正在按照以下方式启动:

I have a flask application which I am currently starting up in the following way:

#phantom.py __author__ = 'uruddarraju' from phantom.api.v1 import app app.run(host='0.0.0.0', port=8080, debug=True)

,当我运行此脚本时,它通过打印成功执行:

and when I run this script, it executes successfully by printing:

loading config from /home/uruddarraju/virtualenvs/PHANTOMNEW/Phantom/etc/phantom/phantom.ini * Running on 0.0.0.0:8080/

但是它永远不会返回,如果我执行CTRL-C,服务器将停止。我正在尝试将其部署到生产环境中,并希望在后台运行该启动程序,只要服务器处于启动状态,进程就会一直保持下去。

But it never returns and if I do a CTRL-C the server stops. I am trying to deploy this to production and want to run this startup on the background, where the process stays up as long as the server is up.

什么是最好的

推荐答案

我在生产中最喜欢的方法是将flask与uwsgi和nginx结合使用以保持持久性。以下是很好的设置说明,可帮助您入门: http://www.markjberger。 com / flask-with-virtualenv-uwsgi-nginx /

My favorite way of doing it in production is to combine flask with uwsgi and nginx to keep persistence. Here are nice setup instructions to get you started: www.markjberger/flask-with-virtualenv-uwsgi-nginx/

Jist:

首先确保您的vps具有最新更新:

First make sure that your vps has the latest updates:

sudo apt-get update sudo apt-get upgrade

现在安装python和virtualenv:

Now install python and virtualenv:

sudo apt-get install build-essential python-dev python-pip sudo pip install virtualenv

为您的网站创建一个文件夹:

Make a folder for your website:

sudo mkdir -p /var/www/mysite sudo chown -R <your user id> /var/www/mysite cd /var/www/mysite

设置virtualenv并安装flask:

Setup virtualenv and install flask:

virtualenv .env --no-site-packages source .env/bin/activate pip install flask

将烧瓶应用程序放置在此文件夹中。如果 __ name__ ==‘__main __’:,请确保您的主机设置为0.0.0.0,并且您的应用程序在下面。如果您的应用程序具有功能,则uwsgi将无法调用它。

Place your flask app in this folder. Make sure that your host is set to 0.0.0.0 and that your app is under if __name__ == '__main__':. If your app is in a function, uwsgi will not be able to call it.

现在是时候使用flask开发服务器测试您的应用程序,看是否一切正常到目前为止工作正常。如果一切运行顺利,请安装nginx和uwsgi:

Now is a good time to test your app with the flask development server to see if everything is working so far. If everything runs smoothly, install nginx and uwsgi:

deactivate sudo apt-get install nginx uwsgi uwsgi-plugin-python

接下来,我们必须为nginx创建一个套接字文件,以便与uwsgi通信:

Next we must create a socket file for nginx to communicate with uwsgi:

cd /tmp/ touch mysite.sock sudo chown www-data mysite.sock

通过将mysite.sock的所有者更改为www-data,nginx将能够写入套接字。现在,我们要做的就是为nginx和uwsgi添加配置文件。首先删除nginx的默认配置:

By changing the owner of mysite.sock to www-data, nginx will be able to write to the socket. Now all we have to do is add our configuration files for nginx and uwsgi. First delete the default configuration for nginx:

cd /etc/nginx/sites-available sudo rm default

创建一个新的配置文件mysite并添加以下内容:

Create a new configuration file mysite and add the following:

server { listen 80; server_tokens off; server_name www.mysite mysite; location / { include uwsgi_params; uwsgi_pass unix:/tmp/mysite.sock; } location /static { alias /var/www/mysite/static; } ## Only requests to our Host are allowed if ($host !~ ^(mysite|www.mysite)$ ) { return 444; } }

要启用该网站,我们必须链接配置文件到/ etc / nginx / sites-enabled /:

In order to enable the site, we must link our configuration file to /etc/nginx/sites-enabled/:

sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite

与uwsgi的过程类似。创建文件/etc/uwsgi/apps-available/mysite.ini并添加以下内容:

The process is similar for uwsgi. Create the file /etc/uwsgi/apps-available/mysite.ini and add the following:

[uwsgi] vhost = true socket = /tmp/mysite.sock venv = /var/www/mysite/.env chdir = /var/www/mysite module = app callable = app

模块是python脚本的名称,而callable是python的名称您的烧瓶实例。因此,如果您的烧瓶站点位于名为mysite.py的文件中,则如下所示:

Module is the name of your python script and callable is the name of your flask instance. So if your flask site was in a file called mysite.py that looked like this:

from flask import Flask my_app = Flask(__name__) @my_app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': my_app.run(host='0.0.0.0')

您的mysite.ini文件为:

Your mysite.ini file would be:

module = mysite callable = my_app

将配置文件链接到enabled-apps文件夹:

Link the configuration file to the enabled-apps folder:

sudo ln -s /etc/uwsgi/apps-available/mysite.ini /etc/uwsgi/apps-enabled/mysite.ini

最后,重新启动nginx和uwsgi:

Finally, restart nginx and uwsgi:

sudo service nginx restart sudo service uwsgi restart

更多推荐

在后台启动Flask服务器

本文发布于:2023-11-05 10:20:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   服务器   Flask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!