【发布时间】:2016-07-29 17:45:16
【问题描述】:
我已经成功地使用 .wsgi 文件中的以下配置部署了一个带有 wsgi 和 apache2 的单例烧瓶应用程序。
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/Devrupt/")
from Devrupt import app as application
application.secret_key = 'Add your secret key'
activate_this = '/var/www/Devrupt/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
我的 Apache2 .conf 文件:
<VirtualHost *:80>
ServerName devrupt.com
ServerAdmin lui@devrupt.com
WSGIScriptAlias / /var/www/Devrupt/devrupt.wsgi
<Directory /var/www/Devrupt/Devrupt/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/Devrupt/Devrupt/static
<Directory /var/www/Devrupt/Devrupt/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
如何部署具有工厂功能的 Flask 应用程序来创建应用程序?
具有工厂函数结构的Flask Application:
| - Devrupt
| - Devrupt
|- app/
|- templates/
|- static/
|- main/
|- __init__.py
|- errors.py
|- forms.py
|- views.py
|- __init__.py # Factory Function
|- models.py
|- migrations/ # Contains the database migrations scripts
|- tests/ # Where unit tests are written
|- __init__.py
|- test*.py
|- venv/ # Contains the python virtual environment
|- requirements.txt # List package dependencies so that it is easy to regenerate an identical virtual environment on a diff machine
|- config.py # Stores the configuration settings
|- manage.py # Launch the application and other applcation tasks
| - devrupt.wsgi
mod_wsgi (Apache) 声明“如果您没有用于创建应用程序的工厂函数,但有一个单例实例,您可以直接将其作为应用程序导入。”
app/init.py(工厂函数):
# Application Package Constructor
from flask import Flask, render_template
from flask.ext.bootstrap import Bootstrap
from flask.ext.mail import Mail
from flask.ext.moment import Moment
from config import config
bootstrap = Bootstrap()
moment = Moment()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
bootstrap.init_app(app)
moment.init_app(app)
# Attach Routes and custom error messages here
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
我必须做哪些修改才能使其在工厂功能应用程序上工作?
【问题讨论】:
-
为什么需要工厂?如果您成功地作为单身人士运行,那您就可以了。而且我们无法告诉您是否需要更改,因为工厂与目录结构无关,它是您在代码中定义的东西。你写过工厂函数吗?如果没有,那就是你需要开始的地方。
标签: python flask apache2 mod-wsgi wsgi