English 中文(简体)
Flask - Deployment
  • 时间:2024-09-17

Flask – Deployment


Previous Page Next Page  

Externally Visible Server

A Flask apppcation 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.

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’)

Thereby, your operating system pstens to all pubpc IPs.

Deployment

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

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

    Heroku

    dotcloud

    webfaction

Flask apppcation 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 apppcation on localhost without messing with DNS and firewall settings.

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

mod_wsgi

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

Instalpng mod_wsgi

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

pip install mod_wsgi

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

mod_wsgi-express start-server

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

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

from yourapppcation import app as apppcation

Make sure that yourapppcation and all the pbraries that are in use are on the python load path.

Configuring Apache

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

<VirtualHost *>
   ServerName example.com
   WSGIScriptApas / C:yourdiryourapp.wsgi

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

</VirtualHost>

Standalone WSGI containers

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

    Gunicorn

    Tornado

    Gevent

    Twisted Web

Advertisements