On this page

Deploy a WordPress website

Overview

WordPress applications almost work out of the box on Clever Cloud, you just have a few adjustments to make.

First, you could check our global PHP documention.

This tutorial is mainly concerning a Git deployment. However, you can deploy using a classic FTP PHP app. Choose “FTP” when you create a new PHP app.

Create an application on Clever Cloud

With the web console

Refer to our getting started for more details on application creation via the console.

With the Clever Tools CLI

  1. Make sure you have clever-tools installed locally or follow our CLI getting started guide.
  2. In your code folder, do clever create --type <type> <app-name> --region <zone> --org <org> where :
    1. type is the type of technology you rely on
    2. app-name the name you want for your application,
    3. zone deployment zone (par for Paris and mtl for Montreal)
    4. org the organization ID the application will be created under.

Refer to clever create for more details on application creation with Clever Tools.

Setting up environment variables on Clever Cloud

With the Clever Cloud console

  1. Go to the Clever Cloud console, and find the app you want to fine tune under it’s organization.
  2. Find the Environment variables menu and select it.
  3. In this menu, you will see a form with VARIABLE_NAME and variable value fields.
  4. Fill them with the desired values then select Add.
  5. Don’t forget to “Update Changes” at the end of the menu.

With the Clever Tools CLI

  1. Make sure you have clever-tools installed locally. Refer to our CLI getting started.
  2. In your code folder, do clever env set <variable-name> <variable-value>

Refer to environment variables reference for more details on available environment variables on Clever Cloud.

You can of course create custom ones with the interface we just demonstrated, they will be available for your application.

Git Deployment on Clever Cloud

You need Git on your computer to deploy via this tool. Here is the official website of Git to get more information: git-scm.com

Setting up your remotes

  1. The “Information” page of your app gives you your Git deployment URL, it looks like this:

    1. git+ssh://git@push.clever-cloud.com/<your_app_id>.git
    2. Copy it in your clipboard
  2. Locally, under your code folder, type in git init to set up a new git repository or skip this step if you already have one

  3. Add the deploy URL with git remote add <name> <your-git-deployment-url>

  4. Add your files via git add <files path> and commit them via git commit -m <your commit message>

  5. Now push your application on Clever Cloud with git push <name> master

Refer to git deployments for more details.

FTP Deployment

Make sure you have Filezilla or an other FTP software installed in your machine.

When you have chosen to deploy your application via FTP at the application creation, a free FS Bucket has been created with an ID matching your application’s ID.

You will find the FTP credentials in the configuration tab of this particular FS Bucket.

Just follow the instructions of your FTP Software to send code to Clever Cloud.

Refer to our Quick Start - FTP deployment for more details.

Configure your WordPress application

Configuration file

Rename the file wp-config-sample.php to wp-config.php. All the PHP code for the configuration should be written in this file.

Configure your database

Make sure you have created a MySQL database add-on in the Clever Cloud console, and that it’s linked to your application. When it’s done, you will be able to access all of your add-on environment variables from the application. You can use them, in wp-config.php, as :

define( 'DB_NAME', getenv("MYSQL_ADDON_DB") );
define( 'DB_USER', getenv("MYSQL_ADDON_USER") );
define( 'DB_PASSWORD', getenv("MYSQL_ADDON_PASSWORD") );
define( 'DB_HOST', getenv("MYSQL_ADDON_HOST").':'.getenv("MYSQL_ADDON_PORT") );

SSL Configuration

Since your website is behind a reverse proxy managed by Clever Cloud, you need to detect specific headers like X_FORWARDED_PROTO or HTTP_X_FORWARDED_PROTO to enable SSL. To do so edit wp-config.php and add the following code above the last require_once call.

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
    $_SERVER['HTTPS'] = 'on';
} elseif (isset($_SERVER['X_FORWARDED_PROTO']) && $_SERVER['X_FORWARDED_PROTO'] == 'https') {
    $_SERVER['HTTPS'] = 'on';  
}

Using a CDN as Cloudflare with SSL (avoid infinite loops)

As with SSL configuration, you need to detect specific headers like X_FORWARDED_PROTO or HTTP_X_FORWARDED_PROTO to enable SSL. In this case, the chained proxies might concatenate those headers. As a result, headers values can look like php [HTTP_X_FORWARDED_PROTO] => https, https The previous code snippet would not enable SSL on the Clever Cloud application, resulting in mixed content or infinite loop.

In order to avoid this, you’ll need to add the following code snippet to your wp-config.php file (it replaces the previous code snippet). Add the following code above the last require_once call or at the beginning of the wp-config.php file.

To do so edit wp-config.php and add the following code above the last require_once call.

function check_proto_set_ssl($forwarded_protocols){
	$secure = 'off';
	if ( strstr($forwarded_protocols , ",") ) {
		$previous = null;
		foreach ( explode(",", $forwarded_protocols) as $value ) {
			if ( $previous ) {
				trim($value) == $previous && trim($value) == 'https' ? $secure = 'on' : $secure = 'off';
			}
			$previous = trim($value);
		}
		$_SERVER["HTTPS"] = $secure;
	}else{
		$forwarded_protocols == 'https' ? $_SERVER["HTTPS"] = 'on' : $_SERVER["HTTPS"] = $secure = 'off';
	}
}

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
	check_proto_set_ssl($_SERVER['HTTP_X_FORWARDED_PROTO']);
} elseif (isset($_SERVER['X_FORWARDED_PROTO'])) {
	check_proto_set_ssl($_SERVER['X_FORWARDED_PROTO']);
}

Configure storage

To store static files, you need to configure a FS Bucket.

Create a FS Bucket add-on and link it to your application. Note its host (you can see it from the addon configuration panel, or in the environment variables exported by the addon). It looks like bucket-01234567-0123-0123-0123-012345678987-fsbucket.services.clever-cloud.com.

To use the bucket in your wordpress app, there are 2 methods :

Environment variables

Create a new environment variable called CC_FS_BUCKET and set /<path-to-static-files>:<bucket-host> as its value.

If you need to have many associated buckets with your app, you need to create en new encironment variable, with a suffix : CC_FS_BUCKET_1, CC_FS_BUCKET_2

JSON

At the root of your application, create a clevercloud/buckets.json file (create a clevercloud folder in which you create a buckets.json file). Add the following lines in this file. Do not forget to replace bucketId by the bucketId displayed in the information section of the FS Bucket add-on.

    [
      {
        "bucket" : "bucketId",
        "folder" : "/<path-to-static-files>"
      }
    ]

Install a WordPress plugin with Git

If you choose git deployment over FTP, the code of your plugins won’t be tracked by git. This implies that you will not be able to install plugins from the administration panel and persist them between two deployments.

To solve this problem, we recommend to install the plugin manually by copying the content of the plugin to the /wp-content/plugins/ folder, add the new files to git and then deploy your application.

The plugin will then be available in the Extensions section of your admin panel and you will be able to manage it as others WordPress plugins. To uninstall the plugin, the procedure is the same as before except that you have to delete the folder corresponding to the plugin you want to delete. The extension will be automatically disabled, but we recommend you to delete it from you admin panel before removing the file, in order to clean your database and all files that the plugin could have created.

Optimise and speed-up your WordPress

There are multiple ways to optimise your WordPress and speed-up its response time. We provide different tools and software to help you in this task as Varnish for the HTTP cache, and Redis for the object caching.

Performance plugins

HTTP Cache with Varnish

Enabling Varnish for your application is very simple. All instances of PHP provide Varnish, you just have to configure your application to use it.

  1. To use Varnish in your application, you have to create a varnish.vcl file in the clevercloud folder of your application. If this folder doesn’t exist, create it in the root of your project.

  2. Copy this code into the varnish.vcl file you just created. It’ll configure Varnish to work with your WordPress.

  3. To properly purge the Varnish cache of your application when a post is created/updated, a comment is posted, … we recommend you to install the Varnish HTTP Purge plugin to your WordPress. It’ll purge the Varnish cache for you and give you the possibility to purge it manually.

If you need to manually purge the Varnish cache, the plugin provides a Purge Varnish cache button on the top bar of your website.

Object cache with Redis

Redis is an add-on that offers you a good way to speed-up your application by caching some of the objects of your application, as the result of SQL queries of your application, improving the response time.

To enable Redis for your WordPress, you need to disable other Object Cache and Data Cache of your application (as those provided by W3 Total Cache for example). Make sure they aren’t enabled to avoid conflicts and performance problems.

  1. Create a Redis add-on for your application.

  2. Add the following lines to your wp-config.php file. Make sure they are before the require_once(ABSPATH . 'wp-settings.php'); line, otherwise the Redis connexion will not work for your application and your application will return only white pages!

define('WP_CACHE_KEY_SALT', 'tvm_');
define('WP_REDIS_CLIENT', 'pecl');
define('WP_REDIS_HOST', getenv('REDIS_HOST'));
define('WP_REDIS_PORT', getenv('REDIS_PORT'));
define('WP_REDIS_PASSWORD', getenv('REDIS_PASSWORD'));
  1. Download this file who manages the connexion with Redis and WordPress and moves it to your /wp-content folder. The file must be named object-cache.php.

Redis should now work with your WordPress.

Monitor your application with New Relic

You can use New Relic to monitor your application on Clever Cloud.

Please refer to our New Relic documentation to configure it for your application.

Deploy WordPress the immutable way

Discover a new way to deploy Wordpress using Composer with Bedrock’s boilerplate : our tutorial on GitHub

More configuration

Need more configuration? To run a script at the end of your deployment? To add your private SSH key to access private dependencies?

Go check the Common configuration page.

You may want to have an advanced usage of your application, in which case we recommend you to read the Administrate documentation section.

If you can’t find something or have a specific need like using a non supported version of a particular software, please reach out to the support.

Comments