Upsun User Documentation

Set environment variables

Upsun Beta access

Test and provide feedback for our newest offering - Upsun!

You can register for the Beta by clicking here and completing the form.

Sign up for Beta access

To ensure your Flask app has everything it needs to function properly, you need to set a few more environment variables via script. To do so, make the following changes to the .environment file automatically generated by the Upsun CLI.

  1. If you’ve added a database to your project, update the DATABASE_URL environment variable to connect your app and database. To do so, locate the following line:

    .environment
    export DATABASE_URL="${DB_CONNECTION}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}"

    And amend it to specify the type of your database. For instance, if you have a PostgreSQL database, change the line to:

    .environment
    export DATABASE_URL="postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}"
  2. To be able to set variables that change depending on the type of environment (production, development, or staging), set the PLATFORM_ENVIRONMENT_TYPE environment variable first. To so, add the following line:

    .environment
    export FLASK_ENV="${PLATFORM_ENVIRONMENT_TYPE}"
  3. To enable the Flask debug mode on preview environments only, add the following line:

    .environment
    export FLASK_DEBUG=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo 0 || echo 1)

    This command returns 0 (disabled) on a production environment, and 1 (enabled) on a preview environment.

  4. To adjust the log level depending on the type of environment, add the following line:

    .environment
    export LOG_LEVEL=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo "info" || echo "debug")

    This command sets the log level to info for your production environment, and to debug for your preview environments.

  5. To adjust the cache control maximum age depending on the type of environment, add the following line:

    .environment
    export SEND_FILE_MAX_AGE_DEFAULT=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo 31556926 || echo 0)

    This command sets the maximum age to 31556926 for your production environment, and to 0 for your preview environments.

  6. Point the SECRET_KEY environment variable to the Upsun-provided PLATFORM_PROJECT_ENTROPY variable.

    .environment
    export SECRET_KEY="${PLATFORM_PROJECT_ENTROPY}"

    The SECRET_KEY environment variable ensures the session cookie is signed securely, and can be used for any other security-related needs by extensions or your app.

  7. If you want to switch from the Flask built-in server to Gunicorn later on, add the following line:

    .environment
    export GUNICORN_WORKERS=1
  8. To commit and push your changes, run the following command:

    Terminal
    git add .environment && git commit -m "Adds needed flask environmental variables"

    Your Flask app is now ready to be pushed to Upsun.

Is this page helpful?