Flask 简明教程

Flask – Deployment

Externally Visible Server

在开发服务器上只能在设置开发环境的计算机上访问 Flask 应用程序。这是一项默认行为,因为在调试模式下,用户可以在计算机上执行任意代码。

A Flask application on the development server is accessible only on the computer on which the development environment is set up. This is a default behavior, because in debugging mode, a user can execute arbitrary code on the computer.

如果禁用 debug ,可以通过将主机名设置为 ‘0.0.0.0’ ,使本地计算机上的开发服务器可供网络上的用户使用。

If debug is disabled, the development server on local computer can be made available to the users on network by setting the host name as ‘0.0.0.0’.

app.run(host = ’0.0.0.0’)

因此,您的操作系统将侦听到所有公共 IP。

Thereby, your operating system listens to all public IPs.

Deployment

要从开发环境切换到成熟的生产环境,应用程序需要部署在真正的 Web 服务器上。根据您的情况,可以使用不同的选项来部署 Flask Web 应用程序。

To switch over from a development environment to a full-fledged production environment, an application needs to be deployed on a real web server. Depending upon what you have, there are different options available to deploy a Flask web application.

对于小型应用程序,您可以考虑将它部署在以下任何托管平台上,所有这些平台都为小型应用程序提供免费计划。

For small application, you can consider deploying it on any of the following hosted platforms, all of which offer free plan for small application.

  1. Heroku

  2. dotcloud

  3. webfaction

Flask 应用程序可以在这些云平台上部署。此外,也可以在 Google 云平台上部署 Flask 应用程序。Localtunnel 服务允许您在本地主机上共享您的应用程序,而无需更改 DNS 和防火墙设置。

Flask application can be deployed on these cloud platforms. In addition, it is possible to deploy Flask app on Google cloud platform. Localtunnel service allows you to share your application on localhost without messing with DNS and firewall settings.

如果您倾向于使用专用 Web 服务器来替代上述共享平台,则可以使用以下选项进行探索。

If you are inclined to use a dedicated web server in place of above mentioned shared platforms, following options are there to explore.

mod_wsgi

mod_wsgi 是一个 Apache 模块,为在 Apache 服务器上托管基于 Python 的 Web 应用程序提供了一个符合 WSGI 的接口。

mod_wsgi is an Apache module that provides a WSGI compliant interface for hosting Python based web applications on Apache server.

Installing mod_wsgi

要直接从 PyPi 安装官方版本,您可以运行 -

To install an official release direct from PyPi, you can run −

pip install mod_wsgi

要验证安装是否成功,请使用 start-server 命令运行 mod_wsgi-express 脚本 -

To verify that the installation was successful, run the mod_wsgi-express script with the start-server command −

mod_wsgi-express start-server

这将在端口 8000 上启动 Apache/mod_wsgi。然后,您可以通过将浏览器的指针指向 -

This will start up Apache/mod_wsgi on port 8000. You can then verify that the installation worked by pointing your browser at −

http://localhost:8000/

Creating .wsgi file

应该有一个 yourapplication.wsgi 文件。此文件包含在启动时执行的代码 mod_wsgi, 以获取应用程序对象。对于大多数应用程序,以下文件应该足够 -

There should be a yourapplication.wsgi file. This file contains the code mod_wsgi, which executes on startup to get the application object. For most applications, the following file should be sufficient −

from yourapplication import app as application

确保 yourapplication 和正在使用的所有库都位于 Python 加载路径上。

Make sure that yourapplication and all the libraries that are in use are on the python load path.

Configuring Apache

您需要告诉 mod_wsgi, 您应用程序的位置。

You need to tell mod_wsgi, the location of your application.

<VirtualHost *>
   ServerName example.com
   WSGIScriptAlias / C:\yourdir\yourapp.wsgi

   <Directory C:\yourdir>
      Order deny,allow
      Allow from all
   </Directory>

</VirtualHost>

Standalone WSGI containers

Python 中编写了许多流行的服务器,这些服务器包含 WSGI 应用程序并提供 HTTP。

There are many popular servers written in Python that contains WSGI applications and serve HTTP.

  1. Gunicorn

  2. Tornado

  3. Gevent

  4. Twisted Web