Skip to content
Myr

deploy.rst

This file

Path

docs/tutorial/deploy.rst

111 lines · 3.6 KiB

Last changed by

Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9

History of this file

Take it

The walk stopped after 200 commits: lines older than 979e0ad point at it.

fix pr linkDavid Lordbefore adoption979e0ad

lines 1–16why, one step back
1Deploy to Production
2====================34This part of the tutorial assumes you have a server that you want to5deploy your application to. It gives an overview of how to create the6distribution file and install it, but won't go into specifics about7what server or software to use. You can set up a new environment on your8development computer to try out the instructions below, but probably9shouldn't use it for hosting a real public application. See10:doc:`/deploying/index` for a list of many different ways to host your11application.121314Build and Install15-----------------16

Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9

lines 17–18why, one step back
17When you want to deploy your application elsewhere, you build a *wheel*
18(``.whl``) file. Install and use the ``build`` tool to do this.

fix pr linkDavid Lordbefore adoption979e0ad

lines 19–21why, one step back
19
20.. code-block:: none21

Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9

lines 22–23why, one step back
22 $ pip install build
23 $ python -m build --wheel

fix pr linkDavid Lordbefore adoption979e0ad

lines 24–47why, one step back
24
25You can find the file in ``dist/flaskr-1.0.0-py3-none-any.whl``. The26file name is in the format of {project name}-{version}-{python tag}27-{abi tag}-{platform tag}.2829Copy this file to another machine,30:ref:`set up a new virtualenv <install-create-env>`, then install the31file with ``pip``.3233.. code-block:: none3435 $ pip install flaskr-1.0.0-py3-none-any.whl3637Pip will install your project along with its dependencies.3839Since this is a different machine, you need to run ``init-db`` again to40create the database in the instance folder.4142 .. code-block:: text4344 $ flask --app flaskr init-db4546When Flask detects that it's installed (not in editable mode), it uses47a different directory for the instance folder. You can find it at

Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9

line 48why, one step back
48``.venv/var/flaskr-instance`` instead.

fix pr linkDavid Lordbefore adoption979e0ad

lines 49–70why, one step back
49
5051Configure the Secret Key52------------------------5354In the beginning of the tutorial that you gave a default value for55:data:`SECRET_KEY`. This should be changed to some random bytes in56production. Otherwise, attackers could use the public ``'dev'`` key to57modify the session cookie, or anything else that uses the secret key.5859You can use the following command to output a random secret key:6061.. code-block:: none6263 $ python -c 'import secrets; print(secrets.token_hex())'6465 '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'6667Create the ``config.py`` file in the instance folder, which the factory68will read from if it exists. Copy the generated value into it.6970.. code-block:: python

Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9

line 71why, one step back
71 :caption: ``.venv/var/flaskr-instance/config.py``

fix pr linkDavid Lordbefore adoption979e0ad

lines 72–111why, one step back
72
73 SECRET_KEY = '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'7475You can also set any other necessary configuration here, although76``SECRET_KEY`` is the only one needed for Flaskr.777879Run with a Production Server80----------------------------8182When running publicly rather than in development, you should not use the83built-in development server (``flask run``). The development server is84provided by Werkzeug for convenience, but is not designed to be85particularly efficient, stable, or secure.8687Instead, use a production WSGI server. For example, to use `Waitress`_,88first install it in the virtual environment:8990.. code-block:: none9192 $ pip install waitress9394You need to tell Waitress about your application, but it doesn't use95``--app`` like ``flask run`` does. You need to tell it to import and96call the application factory to get an application object.9798.. code-block:: none99100 $ waitress-serve --call 'flaskr:create_app'101102 Serving on http://0.0.0.0:8080103104See :doc:`/deploying/index` for a list of many different ways to host105your application. Waitress is just an example, chosen for the tutorial106because it supports both Windows and Linux. There are many more WSGI107servers and deployment options that you may choose for your project.108109.. _Waitress: https://docs.pylonsproject.org/projects/waitress/en/stable/110111Continue to :doc:`next`.