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 fileTake it
The walk stopped after 200 commits: lines older than 65b0eef point at it.
Merge pull request #4500 from pallets/release-2.1.0David Lordbefore adoption65b0eef
lines 1–16why, one step back1Deploy to Production2====================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-----------------16Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
lines 17–18why, one step back17When you want to deploy your application elsewhere, you build a *wheel*18(``.whl``) file. Install and use the ``build`` tool to do this.Merge pull request #4500 from pallets/release-2.1.0David Lordbefore adoption65b0eef
lines 19–21why, one step back1920.. code-block:: none21Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
lines 22–23why, one step back22 $ pip install build23 $ python -m build --wheelMerge pull request #4500 from pallets/release-2.1.0David Lordbefore adoption65b0eef
lines 24–41why, one step back2425You 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.41Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 42why, one step back42 .. code-block:: textMerge pull request #4500 from pallets/release-2.1.0David Lordbefore adoption65b0eef
line 43why, one step back43Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 44why, one step back44 $ flask --app flaskr init-dbMerge pull request #4500 from pallets/release-2.1.0David Lordbefore adoption65b0eef
lines 45–47why, one step back4546When Flask detects that it's installed (not in editable mode), it uses47a different directory for the instance folder. You can find it atMerge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
line 48why, one step back48``.venv/var/flaskr-instance`` instead.Merge pull request #4500 from pallets/release-2.1.0David Lordbefore adoption65b0eef
lines 49–70why, one step back495051Configure 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:: pythonMerge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
line 71why, one step back71 :caption: ``.venv/var/flaskr-instance/config.py``Merge pull request #4500 from pallets/release-2.1.0David Lordbefore adoption65b0eef
lines 72–94why, one step back7273 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 useMerge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 95why, one step back95``--app`` like ``flask run`` does. You need to tell it to import andMerge pull request #4500 from pallets/release-2.1.0David Lordbefore adoption65b0eef
lines 96–111why, one step back96call 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`.