Skip to content
Myr

deploy.rst

This file

Path

docs/tutorial/deploy.rst

146 lines · 4.3 KiB

Last changed by

Merge branch '2.0.x'David Lordbefore adoptione37e871

History of this file

Take it

The walk stopped after 200 commits: lines older than 165f059 point at it.

Merge pull request #3695 from pallets/dependabot/pip/tox-3.18.0dependabot-preview[bot]before adoption165f059

lines 1–34why, 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-----------------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``. The

Merge remote-tracking branch 'origin/1.1.x'Grey Libefore adoptionde0af03

lines 35–36why, one step back
35file name is in the format of {project name}-{version}-{python tag}
36-{abi tag}-{platform tag}.

Merge pull request #3695 from pallets/dependabot/pip/tox-3.18.0dependabot-preview[bot]before adoption165f059

lines 37–50why, one step back
37
38Copy 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.50

Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc

line 51why, one step back
51.. tabs::

Merge pull request #3695 from pallets/dependabot/pip/tox-3.18.0dependabot-preview[bot]before adoption165f059

line 52why, one step back
52

Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc

lines 53–59why, one step back
53 .. group-tab:: Bash
5455 .. code-block:: text5657 $ export FLASK_APP=flaskr58 $ flask init-db59

Merge branch '2.0.x'David Lordbefore adoptione37e871

lines 60–66why, one step back
60 .. group-tab:: Fish
6162 .. code-block:: text6364 $ set -x FLASK_APP flaskr65 $ flask init-db66

Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc

lines 67–79why, one step back
67 .. group-tab:: CMD
6869 .. code-block:: text7071 > set FLASK_APP=flaskr72 > flask init-db7374 .. group-tab:: Powershell7576 .. code-block:: text7778 > $env:FLASK_APP = "flaskr"79 > flask init-db

Merge pull request #3695 from pallets/dependabot/pip/tox-3.18.0dependabot-preview[bot]before adoption165f059

lines 80–97why, one step back
80
81When Flask detects that it's installed (not in editable mode), it uses82a different directory for the instance folder. You can find it at83``venv/var/flaskr-instance`` instead.848586Configure the Secret Key87------------------------8889In the beginning of the tutorial that you gave a default value for90:data:`SECRET_KEY`. This should be changed to some random bytes in91production. Otherwise, attackers could use the public ``'dev'`` key to92modify the session cookie, or anything else that uses the secret key.9394You can use the following command to output a random secret key:9596.. code-block:: none97

Merge branch '2.0.x'David Lordbefore adoption0ae0f59

line 98why, one step back
98 $ python -c 'import secrets; print(secrets.token_hex())'

Merge pull request #3695 from pallets/dependabot/pip/tox-3.18.0dependabot-preview[bot]before adoption165f059

line 99why, one step back
99

Merge branch '2.0.x'David Lordbefore adoption0ae0f59

line 100why, one step back
100 '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'

Merge pull request #3695 from pallets/dependabot/pip/tox-3.18.0dependabot-preview[bot]before adoption165f059

lines 101–107why, one step back
101
102Create the ``config.py`` file in the instance folder, which the factory103will read from if it exists. Copy the generated value into it.104105.. code-block:: python106 :caption: ``venv/var/flaskr-instance/config.py``107

Merge branch '2.0.x'David Lordbefore adoption0ae0f59

line 108why, one step back
108 SECRET_KEY = '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'

Merge pull request #3695 from pallets/dependabot/pip/tox-3.18.0dependabot-preview[bot]before adoption165f059

lines 109–146why, one step back
109
110You can also set any other necessary configuration here, although111``SECRET_KEY`` is the only one needed for Flaskr.112113114Run with a Production Server115----------------------------116117When running publicly rather than in development, you should not use the118built-in development server (``flask run``). The development server is119provided by Werkzeug for convenience, but is not designed to be120particularly efficient, stable, or secure.121122Instead, use a production WSGI server. For example, to use `Waitress`_,123first install it in the virtual environment:124125.. code-block:: none126127 $ pip install waitress128129You need to tell Waitress about your application, but it doesn't use130``FLASK_APP`` like ``flask run`` does. You need to tell it to import and131call the application factory to get an application object.132133.. code-block:: none134135 $ waitress-serve --call 'flaskr:create_app'136137 Serving on http://0.0.0.0:8080138139See :doc:`/deploying/index` for a list of many different ways to host140your application. Waitress is just an example, chosen for the tutorial141because it supports both Windows and Linux. There are many more WSGI142servers and deployment options that you may choose for your project.143144.. _Waitress: https://docs.pylonsproject.org/projects/waitress/en/stable/145146Continue to :doc:`next`.