> ## 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 Bedrock-based WordPress on Upsun Cloud

> Complete the last required steps to successfully deploy Bedrock-based WordPress on Upsun Cloud using Bedrock.


> [!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 following materials.

For WordPress to successfully deploy and operate, **after completing the [Getting started guide](https://developer.upsun.com/docs/get-started/here)**,
you still need to add some required files and 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>

> [!NOTE]
>
> Assumptions
> There are many ways you can set up a WordPress site or Upsun Cloud project.
> The instructions on this page were designed based on the following assumptions:
>
> * You are building a Bedrock-based WordPress site using Roots.io [Bedrock boilerplate](https://roots.io/bedrock/).
> * You have an existing Bedrock-based codebase or created a new Composer project using `roots/bedrock` during the Getting
>   started guide.
> * You selected PHP as your runtime, and MariaDB as a service during the Getting Started guide. It's also assumed that
>   while using the Getting Started guide you named the project `myapp`, which you will notice is the top-level key in all configuration below.

## 1. Configure your root location

Locate the `web:locations` section and update the root (`/`) location as follows:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       source:
&#x20;         root: "/"
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       \<snip>
&#x20;       web:
&#x20;         locations:
&#x20;           "/":
&#x20;             root: "web"
&#x20;             \# The front-controller script to send non-static requests to.
&#x20;             passthru: "/wp/index.php"
&#x20;             \# Wordpress has multiple roots (wp-admin) so the following is required
&#x20;             index:
&#x20;               \- "index.php"
&#x20;             \# The number of seconds whitelisted (static) content should be cached.
&#x20;             expires: 600
&#x20;             scripts: true
&#x20;             allow: true
&#x20;             rules:
&#x20;               ^/composer\\.json:
&#x20;                 allow: false
&#x20;               ^/license\\.txt\$:
&#x20;                 allow: false
&#x20;               ^/readme\\.html\$:
&#x20;                 allow: false
&#x20;           "/wp/wp-content/cache":
&#x20;             root: "web/wp/wp-content/cache"
&#x20;             scripts: false
&#x20;             allow: false
&#x20;           "/wp/wp-content/uploads":
&#x20;             root: "web/app/uploads"
&#x20;             scripts: false
&#x20;             allow: false
&#x20;             rules:
&#x20;               \# Allow access to common static files.
&#x20;               '(?\<!\\-lock)\\.(?i:jpe?g|gif|png|svg|bmp|ico|css|js(?:on)?|eot|ttf|woff|woff2|pdf|docx?|xlsx?|pp\[st]x?|psd|odt|key|mp\[2-5g]|m4\[av]|og\[gv]|wav|mov|wm\[av]|avi|3g\[p2])\$':
&#x20;                 allow: true
&#x20;                 expires: 1w

## 2. Set up a location for uploads

Application containers are read-only by default; WordPress needs a writable location to store uploaded media.
To make the location writable, set up [a mount](https://developer.upsun.com/docs/configure-apps/image-properties/mounts.md). To do so,
locate the `mounts:` section that is commented out, and update it as follows:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       source:
&#x20;         root: "/"
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       mounts:
&#x20;         "web/app/wp-content/cache":
&#x20;           source: storage
&#x20;           source\_path: "cache"
&#x20;         "web/app/uploads":
&#x20;           source: storage
&#x20;           source\_path: "uploads"

## 3. Install dependencies during the build hook

To ensure your Composer dependencies are installed during the [build stage](https://developer.upsun.com/docs/core-concepts/build-deploy.md#the-build),
locate the `build:` section (below the `hooks:` section).\

Update the `build:` section as follows:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       source:
&#x20;         root: "/"
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       ...
&#x20;       hooks:
&#x20;         build: |
&#x20;           set -eux
&#x20;           composer install --prefer-dist --optimize-autoloader --apcu-autoloader --no-progress --no-ansi --no-interaction

You can adjust the `composer install` command to meet your specific requirements.

## 4. Launch tasks during the deploy hook

Once the images for our application have been built, there are a few key tasks that must be completed before our newly-built application can receive requests. These tasks include:

* Flushing the object cache, which might have changed between current production and newly deployed changes
* Running the WordPress database update procedure, in case core is being updated with the newly deployed changes
* Running any due cron jobs

To perform these tasks, we'll utilize  the [deploy hook](https://developer.upsun.com/docs/core-concepts/build-deploy.md#deploy-steps). Locate the
`deploy:` section (below the `build:` section). Update the `deploy:` and `post_deploy:` sections as follows:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       source:
&#x20;         root: "/"
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       ...
&#x20;       hooks:
&#x20;         deploy: |
&#x20;           set -eux
&#x20;           \# Flushes the object cache
&#x20;           wp cache flush
&#x20;           \# Runs the WordPress database update procedure
&#x20;           wp core update-db
&#x20;         post\_deploy: |
&#x20;           wp cron event run --due-now

## 5. Update App container dependencies

Add the wp-cli tool to your application build by locating the `dependencies:` section that is commented out,
and updating it as follows:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       source:
&#x20;         root: "/"
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       dependencies:
&#x20;         php:
&#x20;           wp-cli/wp-cli-bundle: "^2.4"

## 6. Configure your default route

Locate the `routes:` section, and beneath it, the `"https://{default}/":` route. Update the route as follows:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       source:
&#x20;         root: "/"
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       ...

&#x20;   routes:
&#x20;     "https\://\{default}/":
&#x20;       type: upstream
&#x20;       upstream: "\<APP\_NAME>:http"
&#x20;       cache:
&#x20;         enabled: true
&#x20;         cookies:
&#x20;           \- '/^wordpress\_\*/'
&#x20;           \- '/^wp-\*/'

Matching the application name `APP_NAME` with the `upstream` definition `APP_NAME:http` is the most important setting to ensure at this stage.
If these strings aren't the same, the WordPress deployment will not succeed.

## 7. Add your crons

Under your application configuration you can now add a cron.


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       source:
&#x20;         root: "/"
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       ...
&#x20;       crons:
&#x20;         wp-cron:
&#x20;           spec: '\*/10 \* \* \* \*'
&#x20;           commands:
&#x20;             start: wp cron event run --due-now
&#x20;           shutdown\_timeout: 600
&#x20;&#x20;

## 8. Update `.environment`

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

```bash .environment theme={null}
# Set database environment variables
export DB_HOST="$MARIADB_HOST"
export DB_PORT="$MARIADB_PORT"
export DB_PATH="$MARIADB_PATH"
export DB_DATABASE="$DB_PATH"
export DB_USERNAME="$MARIADB_USERNAME"
export DB_PASSWORD="$MARIADB_PASSWORD"
export DB_SCHEME="$MARIADB_SCHEME"
export DATABASE_URL="${DB_SCHEME}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_PATH}"
```

To configure the remaining environment variables that WordPress needs to run smoothly, follow these steps.

1. Open the `.environment` file for editing
2. Add the following at the end of the file:

   ```bash .environment theme={null}
    # Routes, URLS, and primary domain
    export SITE_ROUTES="$(echo "$PLATFORM_ROUTES" | base64 --decode)"
    export UPSTREAM_URLS="$(
      echo "$SITE_ROUTES" | jq -r --arg app "$PLATFORM_APPLICATION_NAME" \
        'map_values(select(.type == "upstream" and .upstream == $app)) | keys'
    )"
    export DOMAIN_CURRENT_SITE="$(
      echo "$SITE_ROUTES" | jq -r --arg app "$PLATFORM_APPLICATION_NAME" \
        'map_values(select(.primary == true and .type == "upstream" and .upstream == $app))
          | keys | .[0]
          | if (.[-1:] == "/") then (.[0:-1]) else . end'
    )"

    export WP_HOME="${DOMAIN_CURRENT_SITE}"
    export WP_SITEURL="${WP_HOME}/wp"
    export WP_DEBUG_LOG="/var/log/app.log"
    # Uncomment this line if you would like development versions of WordPress on non-production environments.
    # export WP_ENV="${PLATFORM_ENVIRONMENT_TYPE}"
    export AUTH_KEY="${PLATFORM_PROJECT_ENTROPY}AUTH_KEY"
    export SECURE_AUTH_KEY="${PLATFORM_PROJECT_ENTROPY}SECURE_AUTH_KEY"
    export LOGGED_IN_KEY="${PLATFORM_PROJECT_ENTROPY}LOGGED_IN_KEY"
    export NONCE_KEY="${PLATFORM_PROJECT_ENTROPY}NONCE_KEY"
    export AUTH_SALT="${PLATFORM_PROJECT_ENTROPY}AUTH_SALT"
    export SECURE_AUTH_SALT="${PLATFORM_PROJECT_ENTROPY}SECURE_AUTH_SALT"
    export LOGGED_IN_SALT="${PLATFORM_PROJECT_ENTROPY}LOGGED_IN_SALT"
    export NONCE_SALT="${PLATFORM_PROJECT_ENTROPY}NONCE_SALT"
   ```

## 9. Commit, Push, and Deploy

You can now commit all the changes made to `.upsun/config.yaml` and `.environment` and push to Upsun Cloud.

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

## 10. Routinely run WP Cron (optional)

If your site does not receive enough traffic to ensure [WP Cron jobs](https://developer.wordpress.org/plugins/cron/) run
in a timely manner, or your site uses caching heavily such that WP Cron isn't being triggered, you might consider adding
a [cron job](https://developer.upsun.com/docs/configure-apps/image-properties/crons.md) to your project's configuration to have WP CLI
run those scheduled tasks on a routine basis. To do so, locate the `crons:` section that is commented out, and update it
as follows:


&#x20;   applications:
&#x20;     \<APP\_NAME>:
&#x20;       source:
&#x20;         root: "/"
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       crons:
&#x20;         wp-cron:
&#x20;           spec: '\*/15 \* \* \* \*'
&#x20;           commands:
&#x20;             start: wp cron event run --due-now
&#x20;           shutdown\_timeout: 600
&#x20;&#x20;

The above example will trigger the wp-cli every 15th minute to run WP Cron tasks that are due. Feel free to adjust based
on your individual requirements.

> [!NOTE]
>
> When uncommenting, pay attention to the indentation and make sure that the `crons` key aligns with other sibling keys (e.g. `hooks`, `dependencies`, etc.)

## Further resources

* [All example files (`.environment`, `.upsun/config.yaml`)](https://github.com/upsun/snippets/tree/main/examples/wordpress-bedrock)

### Documentation

* [PHP documentation](https://developer.upsun.com/docs/languages/php)

* [Extensions](https://developer.upsun.com/docs/languages/php/extensions.md)

* [Performance tuning](https://developer.upsun.com/docs/languages/php/tuning.md)

* [PHP-FPM sizing](https://developer.upsun.com/docs/languages/php/fpm.md)

* [Authenticated Composer](https://developer.upsun.com/docs/languages/php/composer-auth.md)

### Community content

* [PHP topics](https://support.platform.sh/hc/en-us/search?utf8=%E2%9C%93\&query=php)
* [WordPress topics](https://support.platform.sh/hc/en-us/search?utf8=%E2%9C%93\&query=wordpress)

### Blogs

* [To Upsun, a WordPress migration story](https://upsun.com/blog/to-upsun-a-wordpress-migration-story/)


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