English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Flask applications on the development server can only be accessed on computers that have set up the development environment. This is a default behavior because in debug mode, users can execute arbitrary code on the computer.
If debugging is disabled, you can make the network users use the development server on the local computer by setting the hostname to:0.0.0.0
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 app.run(host = '0.0.0.0')
In this way, your operating system will listen to all public IPs, which means that all requests will be processed.
To switch from the development environment to a full production environment, the application needs to be deployed on a real web server. Depending on your specific situation, you can use different options to deploy Flask web applications.
For small applications, consider deploying them on any of the following hosting platforms, all of which offer free plans for small applications.
Heroku dotcloud webfaction
Flask applications can be deployed on these cloud platforms. In addition, Flask applications can be deployed on the Google Cloud Platform. Localtunnel service allows you to share your application on the local host without confusing DNS and firewall settings.
If you prefer to use a dedicated web server instead of the above shared platforms, you can use the following options.
mod_wsgi is an Apache module that provides a WSGI compatible interface for hosting Python-based web applications on Apache servers.
Install mod_wsgi
To install the official version directly from PyPi, you can run -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 pip install mod_wsgi
To verify that the installation was successful, use start-server command runs mod_wsgi-express script -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 mod_wsgi-express start-server
It will be on port:8000 start Apache/mod_wsgi. Then, you can access the application by pointing your browser to -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 http://localhost:8000/
Create .wsgi file
There should be a yourapplication.wsgi File. This file contains the mod_wsgi code that executes at startup to obtain the application object. For most applications, the following file should be sufficient -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 from yourapplication import app as application
Make sure yourapplication and all the libraries used are located on the python load path.
Configure Apache
You need to tell mod_wsgi the location of the application. Refer to the following configuration -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 <VirtualHost *> ServerName example.com WSGIScriptAlias / C:\yourdir\yourapp.wsgi <Directory C:\yourdir> Order deny,allow Allow from all </Directory> </VirtualHost>
There are many popular servers written in Python, which include WSGI applications and provide HTTP services.
Gunicorn Tornado Gevent Twisted Web