deploy.rst
This file
Path
docs/tutorial/deploy.rst
139 lines · 4.2 KiB
Last changed by
Merge pull request #4301 from kkirsche/secret_key_via_secretsDavid Lordbefore adoptionc5ed6c5
History of this fileTake it
1Deploy 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-----------------1617When you want to deploy your application elsewhere, you build a18distribution file. The current standard for Python distribution is the19*wheel* format, with the ``.whl`` extension. Make sure the wheel library20is installed first:2122.. code-block:: none2324 $ pip install wheel2526Running ``setup.py`` with Python gives you a command line tool to issue27build-related commands. The ``bdist_wheel`` command will build a wheel28distribution file.2930.. code-block:: none3132 $ python setup.py bdist_wheel3334You can find the file in ``dist/flaskr-1.0.0-py3-none-any.whl``. The35file name is in the format of {project name}-{version}-{python tag}36-{abi tag}-{platform tag}.3738Copy this file to another machine,39:ref:`set up a new virtualenv <install-create-env>`, then install the40file with ``pip``.4142.. code-block:: none4344 $ pip install flaskr-1.0.0-py3-none-any.whl4546Pip will install your project along with its dependencies.4748Since this is a different machine, you need to run ``init-db`` again to49create the database in the instance folder.5051.. tabs::5253 .. group-tab:: Bash5455 .. code-block:: text5657 $ export FLASK_APP=flaskr58 $ flask init-db5960 .. group-tab:: CMD6162 .. code-block:: text6364 > set FLASK_APP=flaskr65 > flask init-db6667 .. group-tab:: Powershell6869 .. code-block:: text7071 > $env:FLASK_APP = "flaskr"72 > flask init-db7374When Flask detects that it's installed (not in editable mode), it uses75a different directory for the instance folder. You can find it at76``venv/var/flaskr-instance`` instead.777879Configure the Secret Key80------------------------8182In the beginning of the tutorial that you gave a default value for83:data:`SECRET_KEY`. This should be changed to some random bytes in84production. Otherwise, attackers could use the public ``'dev'`` key to85modify the session cookie, or anything else that uses the secret key.8687You can use the following command to output a random secret key:8889.. code-block:: none9091 $ python -c 'import secrets; print(secrets.token_hex())'9293 '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'9495Create the ``config.py`` file in the instance folder, which the factory96will read from if it exists. Copy the generated value into it.9798.. code-block:: python99 :caption: ``venv/var/flaskr-instance/config.py``100101 SECRET_KEY = '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'102103You can also set any other necessary configuration here, although104``SECRET_KEY`` is the only one needed for Flaskr.105106107Run with a Production Server108----------------------------109110When running publicly rather than in development, you should not use the111built-in development server (``flask run``). The development server is112provided by Werkzeug for convenience, but is not designed to be113particularly efficient, stable, or secure.114115Instead, use a production WSGI server. For example, to use `Waitress`_,116first install it in the virtual environment:117118.. code-block:: none119120 $ pip install waitress121122You need to tell Waitress about your application, but it doesn't use123``FLASK_APP`` like ``flask run`` does. You need to tell it to import and124call the application factory to get an application object.125126.. code-block:: none127128 $ waitress-serve --call 'flaskr:create_app'129130 Serving on http://0.0.0.0:8080131132See :doc:`/deploying/index` for a list of many different ways to host133your application. Waitress is just an example, chosen for the tutorial134because it supports both Windows and Linux. There are many more WSGI135servers and deployment options that you may choose for your project.136137.. _Waitress: https://docs.pylonsproject.org/projects/waitress/en/stable/138139Continue to :doc:`next`.