Deploy to Production
This part of the tutorial assumes you have a server that you want todeploy your application to. It gives an overview of how to create thedistribution file and install it, but won’t go into specifics aboutwhat server or software to use. You can set up a new environment on yourdevelopment computer to try out the instructions below, but probablyshouldn’t use it for hosting a real public application. SeeDeployment Options for a list of many different ways to host yourapplication.
Build and Install
When you want to deploy your application elsewhere, you build adistribution file. The current standard for Python distribution is thewheel format, with the .whl
extension. Make sure the wheel libraryis installed first:
- $ pip install wheel
Running setup.py
with Python gives you a command line tool to issuebuild-related commands. The bdist_wheel
command will build a wheeldistribution file.
- $ python setup.py bdist_wheel
You can find the file in dist/flaskr-1.0.0-py3-none-any.whl
. Thefile name is the name of the project, the version, and some tags aboutthe file can install.
Copy this file to another machine,set up a new virtualenv, then install thefile with pip
.
- $ pip install flaskr-1.0.0-py3-none-any.whl
Pip will install your project along with its dependencies.
Since this is a different machine, you need to run init-db
again tocreate the database in the instance folder.
- $ export FLASK_APP=flaskr
- $ flask init-db
When Flask detects that it’s installed (not in editable mode), it usesa different directory for the instance folder. You can find it atvenv/var/flaskr-instance
instead.
Configure the Secret Key
In the beginning of the tutorial that you gave a default value forSECRET_KEY
. This should be changed to some random bytes inproduction. Otherwise, attackers could use the public 'dev'
key tomodify the session cookie, or anything else that uses the secret key.
You can use the following command to output a random secret key:
- $ python -c 'import os; print(os.urandom(16))'
- b'_5#y2L"F4Q8z\n\xec]/'
Create the config.py
file in the instance folder, which the factorywill read from if it exists. Copy the generated value into it.
venv/var/flaskr-instance/config.py
- SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/'
You can also set any other necessary configuration here, althoughSECRET_KEY
is the only one needed for Flaskr.
Run with a Production Server
When running publicly rather than in development, you should not use thebuilt-in development server (flask run
). The development server isprovided by Werkzeug for convenience, but is not designed to beparticularly efficient, stable, or secure.
Instead, use a production WSGI server. For example, to use Waitress,first install it in the virtual environment:
- $ pip install waitress
You need to tell Waitress about your application, but it doesn’t useFLASK_APP
like flask run
does. You need to tell it to import andcall the application factory to get an application object.
- $ waitress-serve --call 'flaskr:create_app'
- Serving on http://0.0.0.0:8080
See Deployment Options for a list of many different ways to hostyour application. Waitress is just an example, chosen for the tutorialbecause it supports both Windows and Linux. There are many more WSGIservers and deployment options that you may choose for your project.
Continue to Keep Developing!.