> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy Flask on Upsun Cloud

> Complete the last required steps to successfully deploy Flask on Upsun Cloud.


> [!NOTE]
>
> Note
> Before you start, check out the [Upsun Cloud demo app](https://console.upsun.com/projects/create-project) and the main [Getting started guide](https://developer.upsun.com/docs/get-started/here).
> They provide all the core concepts and common commands you need to know before using the materials below.

For Flask to successfully deploy and operate, **after completing the [Getting started guide](https://developer.upsun.com/docs/get-started/here)**,
you still need to make a few changes to your Upsun Cloud configuration.

## Before you begin

You need:

<ul><li>[Git](https://git-scm.com/downloads).
Git is the primary tool to manage everything your app needs to run.
Push commits to deploy changes and control configuration through YAML files.
These files describe your infrastructure, making it transparent and version-controlled.</li>
<li>An Upsun account.
If you don't already have one, [register for a trial account](https://auth.upsun.com/register).
You can sign up with an email address or an existing GitHub, Bitbucket, or Google account.
If you choose one of these accounts, you can set a password for your Upsun account later.</li>
<li>The [Upsun CLI](https://developer.upsun.com/cli.md).
This lets you interact with your project from the command line.
You can also do most things through the [Web Console](https://developer.upsun.com/docs/administration/web).</li></ul>

## 1. Leverage environment variables

The CLI generated a `.environment` file during the Getting started guide.
Notice it has already created some environmental variables for you to connect to your database service.

```bash .environment theme={null}
export RELATIONSHIPS_JSON="$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode)"

# Set database environment variables
export DB_HOST="$(echo $RELATIONSHIPS_JSON | jq -r '.postgresql[0].host')"
export DB_PORT="$(echo $RELATIONSHIPS_JSON | jq -r '.postgresql[0].port')"
export DB_DATABASE="$(echo $RELATIONSHIPS_JSON | jq -r '.postgresql[0].path')"
export DB_USERNAME="$(echo $RELATIONSHIPS_JSON | jq -r '.postgresql[0].username')"
export DB_PASSWORD="$(echo $RELATIONSHIPS_JSON | jq -r '.postgresql[0].password')"
export DB_CONNECTION="$(echo $RELATIONSHIPS_JSON | jq -r '.postgresql[0].scheme')"
export DATABASE_URL="postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}"
```

To configure all the environment variables Flask needs to run smoothly, follow these steps.

1. Set the `FLASK_APP` environment variable to specify how to load the app.

   ```bash .environment theme={null}
   export FLASK_APP="autoapp.py"
   ```

   The above example uses the file `autoapp.py` as the main entry.
   Adjust according to your situation.

2. Set the `FLASK_ENV` environment variable to enable behaviors based on the environment type.

   Upsun Cloud provides information about what type of environment the app is running in via the `PLATFORM_ENVIRONMENT_TYPE` environment variable.
   Its values can be `production`, `development`, or `staging`.
   Use this information to set the value for `FLASK_ENV`.

   ```bash .environment theme={null}
   export FLASK_ENV="${PLATFORM_ENVIRONMENT_TYPE}"
   ```

   Several other environmental variables need to change based on the environment type.
   Leverage the information in `PLATFORM_ENVIRONMENT_TYPE` for these other variables too.

3. Set the `FLASK_DEBUG` environment variable to `1` (enabled) if you're not running in production.

   ```bash .environment theme={null}
   if [ "$PLATFORM_ENVIRONMENT_TYPE" = "production" ]; then
     FLASK_DEBUG=0
   else
     FLASK_DEBUG=1
   fi
   export FLASK_DEBUG
   ```

4. Do the same for `LOG_LEVEL`.

   ```bash .environment theme={null}
   if [ "$PLATFORM_ENVIRONMENT_TYPE" = "production" ]; then
     LOG_LEVEL="info"
   else
     LOG_LEVEL="debug"
   fi
   export LOG_LEVEL
   ```

5. Set the `SEND_FILE_MAX_AGE_DEFAULT` to `0` (disabled) if you're not in production, but a higher value if you are.

   ```bash .environment theme={null}
   if [ "$PLATFORM_ENVIRONMENT_TYPE" = "production" ]; then
     SEND_FILE_MAX_AGE_DEFAULT=31556926
   else
     SEND_FILE_MAX_AGE_DEFAULT=0
   fi
   export SEND_FILE_MAX_AGE_DEFAULT
   ```

6. Optional: You may also need to set a `SECRET_KEY` environment variable.
   It's used for securely signing the session cookie and can be used for any other security-related needs by extensions or your app.
   It usually is a long random string.

   Set the `SECRET_KEY` environment variable to leverage the [`PLATFORM_PROJECT_ENTROPY` variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables) provided by Upsun Cloud:

   ```bash .environment theme={null}
   export SECRET_KEY="${PLATFORM_PROJECT_ENTROPY}"
   ```

   Your `.environment` file should now look similar to the following:

   ```bash .environment theme={null}
   # Set database environment variables
   export DB_HOST="$POSTGRESQL_HOST"
   export DB_PORT="$POSTGRESQL_PORT"
   export DB_PATH="$POSTGRESQL_PATH"
   export DB_USERNAME="$POSTGRESQL_USERNAME"
   export DB_PASSWORD="$POSTGRESQL_PASSWORD"
   export DB_SCHEME="postgresql"
   export DATABASE_URL="${DB_SCHEME}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_PATH}"

   export FLASK_ENV="${PLATFORM_ENVIRONMENT_TYPE}"
   if [ "$PLATFORM_ENVIRONMENT_TYPE" = "production" ]; then
     FLASK_DEBUG=0
   else
     FLASK_DEBUG=1
    fi
   export FLASK_DEBUG
   if [ "$PLATFORM_ENVIRONMENT_TYPE" = "production" ]; then
     LOG_LEVEL="info"
   else
     LOG_LEVEL="debug"
   fi
   export LOG_LEVEL
   if [ "$PLATFORM_ENVIRONMENT_TYPE" = "production" ]; then
     SEND_FILE_MAX_AGE_DEFAULT=31556926
   else
     SEND_FILE_MAX_AGE_DEFAULT=0
   fi
   export SEND_FILE_MAX_AGE_DEFAULT
   export SECRET_KEY="${PLATFORM_PROJECT_ENTROPY}"
   ```

## 2. Configure static assets

You need to add some writable disk space to hold the static assets that `flask-static-digest` generates and `npm` builds.

To do so, define the `./<APP_NAME>:/static` directory as [a mount](https://developer.upsun.com/docs/configure-apps/image-properties/mounts.md).
In your app configuration, locate the section dedicated to mounts and update it as follows:


&#x20; applications:
&#x20;   \<APP\_NAME>:
&#x20;     \#...
&#x20;     mounts:
&#x20;       "\<APP\_NAME>:/static":
&#x20;         source: "storage"
&#x20;         source\_path: "static\_assets"
&#x20;&#x20;

Replace `<APP_NAME>:` with the `app_name` you defined when creating your Flask project.

## 3. Install dependencies and builds

Instruct Upsun Cloud to automatically run `npm install` in addition to installing your Python dependencies
when building the application container.

To do so, customize your [build hook](https://developer.upsun.com/docs/configure-apps/hooks/hooks-comparison.md#build-hook).
In your app configuration, locate the section dedicated to it and update it as follows:


&#x20; applications:
&#x20;   \<APP\_NAME>:
&#x20;     \#...
&#x20;     \# Hooks allow you to customize your code/environment as the project moves through the build and deploy stages
&#x20;     \# More information: /docs/configure-apps/app-reference#hooks
&#x20;     hooks:
&#x20;       \# The build hook is run after any build flavor.
&#x20;       \# More information: /docs/configure-apps/hooks/hooks-comparison#build-hook
&#x20;       build: |
&#x20;         set -eux
&#x20;         pip install -r requirements.txt
&#x20;&#x20;

The Upsun Cloud CLI automatically added `pip install -r requirements.txt` to your build hook configuration when you
[configured your Upsun Cloud project](https://developer.upsun.com/docs/get-started/here/configure).

If you want `pip` to be upgraded first to the latest version, add `pip install --upgrade pip` to the above line.
Then, add `npm install`:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       \#...
&#x20;       \# Hooks allow you to customize your code/environment as the project moves through the build and deploy stages
&#x20;       \# More information: /docs/configure-apps/app-reference#hooks
&#x20;       hooks:
&#x20;         \# The build hook is run after any build flavor.
&#x20;         \# More information: /docs/configure-apps/hooks/hooks-comparison#build-hook
&#x20;         build: |
&#x20;           set -eux
&#x20;           pip install --upgrade pip
&#x20;           pip install -r requirements.txt
&#x20;           npm install
&#x20;&#x20;

Note that if your project uses a different package manager, Upsun Cloud also supports [several other options](https://developer.upsun.com/docs/languages/python/dependencies.md).

## 4. Configure the deploy phase

Instruct Upsun Cloud to automatically run `npm run build` when building the application container.
To do so, customize your [deploy hook](https://developer.upsun.com/docs/configure-apps/hooks/hooks-comparison.md#deploy-hook).
In your app configuration, locate the section dedicated to it:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       \#...
&#x20;       \# Hooks allow you to customize your code/environment as the project moves through the build and deploy stages
&#x20;       \# More information: /docs/configure-apps/app-reference#hooks
&#x20;       hooks:
&#x20;         \#...
&#x20;         \# The deploy hook is run after the app container has been started, but before it has started accepting requests.
&#x20;         \# More information: /docs/configure-apps/hooks/hooks-comparison#deploy-hook
&#x20;         deploy: |
&#x20;           set -eux
&#x20;           \# echo 'Put your deploy command here'
&#x20;&#x20;

Add `npm run build`:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       \#...
&#x20;       \# Hooks allow you to customize your code/environment as the project moves through the build and deploy stages
&#x20;       \# More information: /docs/configure-apps/app-reference#hooks
&#x20;       hooks:
&#x20;         \#...
&#x20;         \# The deploy hook is run after the app container has been started, but before it has started accepting requests.
&#x20;         \# More information: /docs/configure-apps/hooks/hooks-comparison#deploy-hook
&#x20;         deploy: |
&#x20;           set -eux
&#x20;           npm run build
&#x20;&#x20;

## 5. Configure the web server

[Configure the web server](https://developer.upsun.com/docs/configure-apps#configure-whats-served) running in front of your app to define how your app handles dynamic requests.
To do so, in your app configuration, locate the section dedicated to the web server:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       \#...
&#x20;       \# The web key configures the web server running in front of your app.
&#x20;       \# More information: /docs/configure-apps/app-reference#web
&#x20;       web:
&#x20;         \# Commands are run once after deployment to start the application process.
&#x20;         \# More information: /docs/configure-apps/image-properties/web#web-commands
&#x20;&#x20;

To add a basic Flask server, replace the default information added by the Upsun Cloud CLI with `flask run -p $PORT`.
Also, change the `socket_family` value from `unix` to `tcp`:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       \#...
&#x20;       \# The web key configures the web server running in front of your app.
&#x20;       \# More information: /docs/anchors/app/reference/web/
&#x20;       web:
&#x20;         \# Commands are run once after deployment to start the application process.
&#x20;         \# More information: /docs/anchors/app/reference/web/commands/
&#x20;         commands:
&#x20;           start: "flask run -p \$PORT"
&#x20;         upstream:
&#x20;           socket\_family: tcp
&#x20;&#x20;

You can now commit all of the above changes and push to Upsun Cloud.

```bash Terminal theme={null}
git add .
git commit -m "Add changes to complete my Upsun Cloud configuration"
git push
```

Note that if your Flask project requires a different web server,
Upsun Cloud also supports [several other options](https://developer.upsun.com/docs/languages/python/server.md), including Gunicorn, Daphne,
Uvicorn, and Hypercorn.

If you use Pip, make sure you add your chosen web server to your `requirements.txt` file.

## 6. Handle database migrations

### Prepare database migrations

If you have a new Flask project that uses [Flask-migrate](https://flask-migrate.readthedocs.io/en/latest/),
or an existing app but need to set up the initial migrations, you can do so using the database service you created earlier.

To do so, follow these steps.

1. Set up a virtual environment where your project can run:

   ```bash Terminal theme={null}
   python3 -m venv env && source venv/bin/activate
   ```

2. Just like in your build hook, update pip and install the requirements:

   ```bash Terminal theme={null}
   pip install --upgrade pip && pip install -r requirements.txt
   ```

3. Set up a way for your local instance of Flask to communicate with your database service:

   ```bash Terminal theme={null}
   upsun tunnel:open -y
   ```

   This opens an SSH tunnel to all the services for the app.
   You can use this connection to allow your local instance to communicate with live services as if they too were local.\

   To do so, you need to configure more environment variables.

4. Reopen your `.environment` file.
   Note the use of the `$PLATFORM_RELATIONSHIPS` environment variable to retrieve information about services and their credentials.\

   The tunnel you created gives you access to that same data,
   allowing you to generate a local `PLATFORM_RELATIONSHIPS` environment variable containing the same information.

   Set the following environment variable:

   ```bash .environment theme={null}
   export PLATFORM_RELATIONSHIPS="$(upsun tunnel:info --encode)"
   ```

   Since you now have this environmental variable set locally, you can reuse your `.environment` file for Upsun Cloud to recreate
   many of the other environmental variables you need to run locally.

5. Set the following environment variables as they aren't set via `PLATFORM_RELATIONSHIPS`:

   ```bash .environment theme={null}
   export PLATFORM_ENVIRONMENT_TYPE="production"
   export PORT=8888
   export PLATFORM_PROJECT_ENTROPY="$(openssl rand -base64 32)"
   ```

6. Source your `.environment` file to finish setting up all the environmental variables in your current bash:

   ```bash Terminal theme={null}
   source ./.environment
   ```

   You now have everything you need for Flask-Migrate to be able to connect to the database and generate your migration files.

### Generate database migrations

1. Initiate the migrations directory and prepare for the `migrate` command:

   ```bash Terminal theme={null}
   flask db init
   ```

2. Generate your migrations:

   ```bash Terminal theme={null}
   flask db migrate
   ```

3. Commit your generated migrations:

   ```bash Terminal theme={null}
   git add migrations/* && git commit -m "Adds migrations"
   ```

4. Instruct Upsun Cloud to run the Flask-migrate upgrade command when deploying
   so any migration changes are automatically applied.\

   To do so, re-open your `.upsun/config.yaml` file.
   Locate the `deploy` hook where you added `npm run build` previously and update it as follows:


   &#x20; applications:
   &#x20; \<APP\_NAME>:
   &#x20;   \#...
   &#x20;   \# Hooks allow you to customize your code/environment as the project moves through the build and deploy stages
   &#x20;   \# More information: /docs/configure-apps/app-reference#hooks
   &#x20;   hooks:
   &#x20;     \#...
   &#x20;     \# The deploy hook is run after the app container has been started, but before it has started accepting requests.
   &#x20;     \# More information: /docs/configure-apps/hooks/hooks-comparison#deploy-hook
   &#x20;     deploy: |
   &#x20;       set -eux
   &#x20;       npm run build
   &#x20;       flask db upgrade
   &#x20;&#x20;

5. Commit all your changes and push to Upsun Cloud.

   ```bash Terminal theme={null}
   git add .
   git commit -m "Add changes to complete my Upsun Cloud configuration"
   git push
   ```

## Further resources

### Documentation

* [Python documentation](https://developer.upsun.com/docs/languages/python)
* [Managing dependencies](https://developer.upsun.com/docs/languages/python/dependencies.md)
* [Configuring web servers](https://developer.upsun.com/docs/languages/python/server.md)

### Community content

* [Flask topics](https://support.platform.sh/hc/en-us/search?utf8=%E2%9C%93\&query=flask)
* [Python topics](https://support.platform.sh/hc/en-us/search?utf8=%E2%9C%93\&query=python)

### Blogs

* [Up(sun) and running with Django](https://upsun.com/blog/setting-up-django-on-upsun/)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.