Subscribe to PHP Freaks RSS

Deploying a PHP application to Cloud Foundry

syndicated from on August 30, 2017

I recently had a requirement to deploy a Slim application somewhere. As I already have a Bluemix account, it seemed sensible to deploy it to their Application Runtimes service which is an installation of the Open Source Cloud Foundry project.

This turned out to be quite easy, but there are a number of steps involved, so I'm documenting it here.

Setup the CLI tools

I'm a command line person, so did it all via the command line. There's a Cloud Foundry CLI and also there's the Bluemix CLI tool too.

If you have bx installed, then you can use bx cf to run Cloud Foundry commands. This appears to proxy to the cf client. Regardless, the same commands seem to work in both tools. As I tend to prefer the Open Source option when I can, I used the cf tool. If you're using the Bluemix CLI, just prefix all my commands with bx and you should be fine.

On Mac, I used Homebrew to install cf:

$ brew install cloudfoundry/tap/cf-cli

Follow the relevant instructions for your operating system.

Prepare the PHP application

There are a number of things we need to do to set up our PHP application for deployment. In my case, I'm deploying a Slim application, but practically, these steps work for any PHP app.

1. Select PHP version

CF uses buildpacks which control the environment. In our case, we'll use the PHP buildpack which comes with a variety of PHP versions. To select the one that we will use, we use the standard Composer require statement.

I want to use PHP 7.1, need to add "php" : "7.1.*" to the require section of composer.json like this:


    "require": {
        "php" : "7.1.*"
        "slim/slim": "^3.0",

2. Create the manifest file

Your Cloud Foundry environment is controlled by the manifest file, manifest.yml which must be in the root directory of your application.


    - name: slim-bookshelf
      buildpack: php_buildpack
      memory: 64M
      instances: 1
      host: slim-bookshelf

Every CF app needs a name, and this is minimum requirement for a valid manifest file. Note that the app name may be used on the command line, so it's easier if it doesn't have a space in it. Every other option is used to override the defaults and I find it useful to ensure that I know what will be configured.

I've set up:

  • buildpack: Which build pack to use. This can be a name from cf buildpacks or a GitHub URL. I've picked the PHP buildpack that's supplied with Bluemix.
  • memory: Memory limit for the application. Memory is expensive in cloud apps, so keep this as low as you can.
  • instances: The number of instances to initially start.
  • host: The subdomain name for this application.

That's all we need. However the full list of options is in the docs should you need them.

3. Set buildpack options

The PHP buildpack can be configured using the .bp-config/options.json file. Weirdly this is a JSON file rather than YAML, but whatever :)

In this file, we can set which directory to use as the public root of our project. Slim Bookshelf is a standard Slim application, so it's public root directory is public, so we need to set this. We can also enable PHP extensions here.


    "WEBDIR": "public",
    "PHP_EXTENSIONS": ["gd", "pdo", "pgsql", "pdo_pgsql"]

There are other options available; check the docs.

4. Setup Rewrite rules

The PHP buildpack runs Apache by default, so can you create a public/.htacces file to configure rewriting of URLs to index.php. For example:


<IfModule mod_rewrite.c>
    RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST

Truncated by Planet PHP, read more at the original (another 4311 bytes)