[英]Deploying Flask application with uwsgi and nginx
I am trying to deploy a simple web app that I have built using Python and Flask. 我正在尝试部署一个使用Python和Flask构建的简单Web应用程序。
My app has the following structure: 我的应用程序具有以下结构:
/var/www/watchgallery/
+ app
+ __init__.py
+ views.py
+ templates
+ flask #virtual environment for Flask
+ run.py #script I used in my machine to start the development Flask server
+ watchgallery_nginx.conf
+ watchgallery_uwsgi.ini
+ watchgallery_uwsgi.sock
For this purpose of deploying, I am following this link: http://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/ 为了部署这个目的,我关注此链接: http : //vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/
In this tutorial, the Flask app consists of only a hello.py
file. 在本教程中,Flask应用程序仅包含一个hello.py
文件。 The way he configures his uwsgi file is like this (/var/www/demoapp/demoapp_uwsgi.ini): 他配置他的uwsgi文件的方式是这样的(/var/www/demoapp/demoapp_uwsgi.ini):
[uwsgi]
#application's base folder
base = /var/www/demoapp
#python module to import
app = hello
module = %(app)
home = %(base)/venv
pythonpath = %(base)
#socket file's location
socket = /var/www/demoapp/%n.sock
#permissions for the socket file
chmod-socket = 666
#the variable that holds a flask application inside the module imported at line #6
callable = app
#location of log files
logto = /var/log/uwsgi/%n.log
I have tried to apply the same logic to my uwsgi.ini
file, but I am doing something wrong. 我试图将相同的逻辑应用于我的uwsgi.ini
文件,但我做错了。 This is how my file looks like: 这是我的文件的样子:
[uwsgi]
#application's base folder
base = /var/www/watchgallery
#python module to import
app = run
module = %(app)
home = %(base)/flask
pythonpath = %(base)
#socket file's location
socket = /var/www/watchgallery/%n.sock
#permissions for the socket file
chmod-socket = 666
#the variable that holds a flask application inside the module imported at line #6
callable = app
When I am developing my app in my local machine, I run this command to start de the server: ./run.py
. 当我在本地计算机上开发我的应用程序时,我运行此命令来启动服务器: ./run.py
This is my run.py
file: 这是我的run.py
文件:
#!flask/bin/python
from app import app
app.run(debug = False)
Now, my question is: how should my uwsgi.ini file look like given that my Flask app consists of more than a single file? 现在,我的问题是:鉴于我的Flask应用程序包含多个文件,我的uwsgi.ini文件应该如何?
It doesn't matter how complex your application is. 应用程序的复杂程度无关紧要。 You tell uWSGI where the entry is, the rest is processed normally with Python imports. 您告诉uWSGI条目在哪里,其余部分通过Python导入正常处理。
In your case the entry is module = %(app)
and callable = app
. 在您的情况下,条目是module = %(app)
和callable = app
。 So uWSGI will load the module and send requests to the callable which is a Flask application. 所以uWSGI将加载模块并将请求发送到可调用的Flask应用程序。
Now since the requests are to be served by uWSGI and not Flask's server, you don't need the app.run(debug = False)
line. 既然请求是由uWSGI而不是Flask的服务器提供的,那么您不需要app.run(debug = False)
行。 But you can keep development and production code the same with this trick: 但是你可以通过这个技巧保持开发和生产代码相同:
#!flask/bin/python
from app import app
if __name__ == "__main__":
app.run(debug = False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.