• Trellis
  • Bedrock

Deploying to Kinsta with Trellis

Kinsta is a managed WordPress host that comes with SSH access, Composer, and WP-CLI out of the box.

Starting at $30/month, Kinsta’s plans make sense for anything from basic brochure sites, to WooCommerce stores, to your Bedrock-based WordPress applications.

Kinsta supports our open-source projects both on their infrastructure and by financially contributing to the development efforts. Long before our relationship with Kinsta, they’ve been the only managed WordPress host that we’ve ever felt comfortable recommending. Several Roots team members have trusted Kinsta with hosting their sites that they don’t ever want to have to worry about going down.

First, make sure your public SSH keys are added to the MyKinsta dashboard.

Trellis can deploy to Kinsta with just a few updates. Since Kinsta provides everything from the web server standpoint, provisioning your staging and production environments do not apply.

The one-command deploys in Trellis work with Kinsta with a little configuration. Once configured, you’ll be able to deploy your WordPress sites by running the deploy playbook in Trellis:

$ ansible-playbook deploy.yml -e env=staging -e site=example.com --limit=kinsta_staging

Bring up your MyKinsta dashboard and navigate to the WordPress site that you’re setting up with Bedrock and Trellis, along with your code editor opened to the trellis directory in your project.

First edit trellis/ansible.cfg to add the following to [defaults] at the top:

forks = 3
host_key_checking = False

Staging configuration

Make sure that trellis/group_vars/staging/wordpress_sites.yml is configured with the proper canonical for your staging site:

wordpress_sites:
  example.com:
    site_hosts:
    - canonical: staging-example.kinsta.com

Then open up trellis/group_vars/staging/main.yml and add the following to the end of the file:

project_root: /www/example_123/public
www_root: /www/example_123/public
web_user: example
web_group: www-data

Replace example_123 in the paths above with the correct path provided from the MyKinsta dashboard. Make sure you have selected your Kinsta staging environment.

Developer path from MyKinsta dashboard

Next, open trellis/group_vars/staging/vault.yml for editing by running ansible-vault edit group_vars/staging/vault.yml.

We need to add db_userdb_name, and db_password to env. You can find the values for these on the main info screen for your site in the MyKinsta dashboard.

MyKinsta credentials

vault_wordpress_sites:
  example.com:
    env:
      db_user: "example"
      db_name: "example"
      db_password: "xxxxxxxxxxxxxxx"
      # Generate your keys here: https://roots.io/salts.html
      auth_key: ""
      secure_auth_key: ""
      logged_in_key: ""
      nonce_key: ""
      auth_salt: ""
      secure_auth_salt: ""
      logged_in_salt: ""
      nonce_salt: ""

Finally, open trellis/hosts/staging and replace the contents with:

kinsta_staging ansible_host=104.154.94.123 ansible_ssh_port=12345 ansible_ssh_extra_args='-o StrictHostKeyChecking=no'

[web]
kinsta_staging

[staging]
kinsta_staging

Make sure that the host and SSH port match what is listed in the MyKinsta dashboard.

MyKinsta host and SSH (staging)

Production configuration

Now, let’s repeat the same process above for the production environment. Make sure to toggle to your “live” environment in the MyKinsta dashboard.

Live environment

Open up trellis/group_vars/production/main.yml and add the following to the end of the file:

project_root: /www/example_123/public
www_root: /www/example_123/public
web_user: example
web_group: www-data

Next, open trellis/group_vars/production/vault.yml for editing by running ansible-vault edit group_vars/production/vault.yml:

vault_wordpress_sites:
  example.com:
    env:
      db_user: "example"
      db_name: "example"
      db_password: "xxxxxxxxxxxxxxx"
      # Generate your keys here: https://roots.io/salts.html
      auth_key: ""
      secure_auth_key: ""
      logged_in_key: ""
      nonce_key: ""
      auth_salt: ""
      secure_auth_salt: ""
      logged_in_salt: ""
      nonce_salt: ""

Finally, open trellis/hosts/production and replace the contents with:

kinsta_production ansible_host=104.154.94.123 ansible_ssh_port=12345 ansible_ssh_extra_args='-o StrictHostKeyChecking=no'

[web]
kinsta_production

[production]
kinsta_production

Modifying the deploy tasks

Trellis deploys try to reload php-fpm, which we need to remove from attempting to run on Kinsta’s servers. We also need to trigger the clearing of Kinsta’s cache on a deploy.

Open trellis/roles/deploy/hooks/finalize-after.yml and scroll to the bottom. Remove the last task for Reload php-fpmand add the following:

- name: Clear Kinsta cache
  uri:
    url: "{{ site_env.wp_home }}/ask-support-rep/"
    method: GET

Replace ask-support-rep above after asking a Kinsta support rep for the URL to clearing the cache on your site.

Optional: Install Composer dependencies

If you’re getting a screen that tells you to run ‘Composer Install,’ add the following right before the “Clear Kinsta cache” code above:

  name: Install Composer dependencies
  composer:
  command: install
  working_dir: >/www/example123/public/final-path

Adding kinsta-mu-plugins to Bedrock

Kinsta sites come with mu-plugins installed automatically. With Bedrock installs you’ll need to bring in the kinsta-mu-plugins package.

Open site/composer.json and add the following within the repositories array:

{
  "type": "package",
  "package": {
    "name": "kinsta/kinsta-mu-plugins",
    "type": "wordpress-muplugin",
    "version": "2.0.15",
    "dist": {
      "url": "https://kinsta.com/kinsta-tools/kinsta-mu-plugins.zip",
      "type": "zip"
    }
  }
}

Then run the following from your Bedrock/site directory:

$ composer require kinsta/kinsta-mu-plugins:2.0.15

Final steps with Kinsta support

The last thing you need to do is inform Kinsta of what to set the document root to. Hop on MyKinsta and ask the support team for your document root be updated to public/current/web.

If you didn’t already get the clear cache URL earlier, also ask your support rep for this, and make sure that trellis/roles/deploy/hooks/finalize-after.yml is updated with the correct URL to clear Kinsta’s cache on a successful deploy.

Once this change has been made you’ll be able to deploy to both your staging and production environments with a single line:

# Deploy staging
$ ansible-playbook deploy.yml -e env=staging -e site=example.com --limit=kinsta_staging

# Deploy production
ansible-playbook deploy.yml -e env=production -e site=example.com --limit=kinsta_production

Better yet… setup a continuous integration service, such as CircleCI, to automatically run the deploy for you when you commit to either staging or master!

Originally published on the Kinsta blog: How to Use Bedrock and Trellis at Kinsta (WordPress Development)

Start the discussion on Roots Discourse

Join over 5,800 subscribers on our newsletter to get the latest Roots updates, along with occasional tips on building better WordPress sites.

Looking for WordPress plugin recommendations, the newest modern WordPress projects, and general web development tips and articles?

“Easily the best WordPress email I get.” Colin OBrien

Follow us on Twitter @rootswp

Ready to checkout?