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.

Create an application on Clever Cloud

via the web console

  1. Create a new app by clicking on the Add an Application button, in the sidebar.
  2. Select a brand new instance (or a repository from GitHub if your account is linked).
  3. Then select PHP in the platform list.
  4. Configure your scaling options.
  5. Enter your application’s name and description and click “Next”. You can also select the region you want (North America or Europe).

Refer to https://www.clever-cloud.com/doc/getting-started/ for more details on application creation via the console.

via the Clever Tools CLI

  1. Make sure you have clever-tools installed locally. Report to https://www.clever-cloud.com/doc/reference/clever-tools/getting_started/
  2. In your code folder, do clever create --type <type> <app-name> --region <zone> --org <org> where type is the type of technology you rely on, app-name the name you want for your application, zone deloyment zone (par for Paris and mtl for Montreal), and org the organization ID the application will be created under.

Refer to https://www.clever-cloud.com/doc/reference/clever-tools/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. Fill them with the desired values then select Create.

With the Clever Tools CLI

  1. Make sure you have clever-tools installed locally. Report to https://www.clever-cloud.com/doc/reference/clever-tools/getting_started/
  2. In your code folder, do clever env set <variable-name> <variable-value>

Refer to https://www.clever-cloud.com/doc/reference/reference-environment-variables/ 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.

Configure your WordPress application

Mandatory configuration

  • Rename the file wp-config-sample.php to wp-config.php.
  • Replace in wp-config.php the host (for example: bj79c949bvl2deb6.mysql.clvrcld.net), database name, username and password using the environment variables of the add-on.

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']);
}

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 [the New Relic section of the documentation] to configure it for your application.

Environment injection

Clever Cloud injects environment variables from your application settings as mentioned in setting up environment variables and is also injecting in your application production environment, those from your linked add-ons.

Linking a database or any other add-on to your application

By linking an application to an add-on, the application has the add-on environment variables in its own environment by default.

On add-on creation

Many add-ons do exist on Clever Cloud, please refer to the full list and check add-ons dedicated pages for full instructions.

During add-on creation, you will see Applications screen, with a list of your applications. You can toggle the button to Link and click next. If you finish the process of add-on creation, the application will automatically be linked to it.

Add-on already exists

In the Clever Cloud console, under the Service Dependencies menu of your application, you can use the Link addons dropdown menu to select the name of the add-on you want to link and use the add button to finish the process.

You can also link another application from the same page in the Clever Cloud console, using the Link applications dropdown menu.

Configure your database

Make sure you have created a 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 as DATABASE_URL=$MYSQL_ADDON_URI.

Configure storage

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.

Create a new environment variable called CC_FS_BUCKET and set /storage/app:<bucket-host> as its value.

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" : "/wp-content/uploads"
      }
    ]

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: git+ssh://git@push.clever-cloud.com/<your_app_id>.git. 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 https://www.clever-cloud.com/doc/getting-started/quickstart/ for more details on git deployments.

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.

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 do send code to Clever Cloud.

Refer to https://www.clever-cloud.com/doc/getting-started/quickstart/ for more details on FTP deployments.

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.