flask/docs/deploying/mod_wsgi.rst

106 lines
3.1 KiB
ReStructuredText
Raw Normal View History

2022-06-13 04:45:31 +08:00
mod_wsgi
========
2022-06-13 04:45:31 +08:00
`mod_wsgi`_ is a WSGI server integrated with the `Apache httpd`_ server.
The modern `mod_wsgi-express`_ command makes it easy to configure and
start the server without needing to write Apache httpd configuration.
2022-06-13 04:45:31 +08:00
* Tightly integrated with Apache httpd.
* Supports Windows directly.
* Requires a compiler to install. Requires Apache installed separately
on Windows.
* Does not require a reverse proxy setup.
2022-06-13 04:45:31 +08:00
This page outlines the basics of running mod_wsgi-express, not the more
complex installation and configuration with httpd. Be sure to read the
`mod_wsgi-express`_, `mod_wsgi`_, and `Apache httpd`_ documentation to
understand what features are available.
2022-06-13 04:45:31 +08:00
.. _mod_wsgi-express: https://pypi.org/project/mod-wsgi/
.. _mod_wsgi: https://modwsgi.readthedocs.io/
.. _Apache httpd: https://httpd.apache.org/
2022-06-13 04:45:31 +08:00
Installing
----------
2022-06-13 04:45:31 +08:00
On Linux/Mac, the most straightforward way to install mod_wsgi is to
install the ``mod_wsgi-standalone`` package, which will compile an
up-to-date version of Apache httpd as well.
2022-06-13 04:45:31 +08:00
Create a virtualenv, install your application, then install
``mod_wsgi-standalone``.
2022-06-13 04:45:31 +08:00
.. code-block:: text
2022-06-13 04:45:31 +08:00
$ cd hello-app
$ python -m venv venv
$ . venv/bin/activate
$ pip install . # install your application
$ pip install mod_wsgi-standalone
2022-06-13 04:45:31 +08:00
If you want to use the system-installed version of Apache httpd
(required on Windows, optional but faster on Linux/Mac), install the
``mod_wsgi`` package instead. You will get an error if Apache and its
development headers are not available. How to install them depends on
what OS and package manager you use.
2022-06-13 04:45:31 +08:00
.. code-block:: text
2022-06-13 04:45:31 +08:00
$ pip install mod_wsgi
2022-06-13 04:45:31 +08:00
Running
-------
2022-06-13 04:45:31 +08:00
The only argument to ``mod_wsgi-express`` specifies a script containing
your Flask application, which must be called ``application``. You can
write a small script to import your app with this name, or to create it
if using the app factory pattern.
2022-06-13 04:45:31 +08:00
.. code-block:: python
:caption: ``wsgi.py``
2022-06-13 04:45:31 +08:00
from hello import app
2022-06-13 04:45:31 +08:00
application = app
2022-06-13 04:45:31 +08:00
.. code-block:: python
:caption: ``wsgi.py``
2022-06-13 04:45:31 +08:00
from hello import create_app
application = create_app()
2022-06-13 04:45:31 +08:00
Now run the ``mod_wsgi-express start-server`` command.
2022-06-13 04:45:31 +08:00
.. code-block:: text
2022-06-13 04:45:31 +08:00
$ mod_wsgi-express start-server wsgi.py --processes 4
2022-06-13 04:45:31 +08:00
The ``--processes`` option specifies the number of worker processes to
run; a starting value could be ``CPU * 2``.
2022-06-13 04:45:31 +08:00
Logs for each request aren't show in the terminal. If an error occurs,
its information is written to the error log file shown when starting the
server.
2022-06-13 04:45:31 +08:00
Binding Externally
------------------
2022-06-13 04:45:31 +08:00
Unlike the other WSGI servers in these docs, mod_wsgi can be run as
root to bind to privileged ports like 80 and 443. However, it must be
configured to drop permissions to a different user and group for the
worker processes.
2022-06-13 04:45:31 +08:00
For example, if you created a ``hello`` user and group, you should
install your virtualenv and application as that user, then tell
mod_wsgi to drop to that user after starting.
2022-06-13 04:45:31 +08:00
.. code-block:: text
2022-06-13 04:45:31 +08:00
$ sudo /home/hello/venv/bin/mod_wsgi-express start-server \
/home/hello/wsgi.py \
--user hello --group hello --port 80 --processes 4