Developing software in the Real World

Deploying a PHP application to Cloud Foundry

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:

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:

composer.json:

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.

manifest.yml:

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.

.bp-config/options.json:

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:

public/.htaccess:

That's it with configuring our application. The additional files we've created can be commited to git and we're good to go.

Deploy our CF application

To deploy our Cloud Foundry application we need to log into Cloud Foundry and then push our application.

Log in to the CF cli

We log in using cf login -a {api url}, -o {organisation} -s {space} which will ask us for our username and password.

You can get the API endpoint you need from bx regions if you're using Bluemix. For the UK, it's https://api.eu-gb.bluemix.net, my organisation is 19FT and I'm using my demo space:

It will ask you for your username and password and then print out some summary information.

Deploy our application

Deployment is trivially simple:

You'll see it create the app and a route and then bind the route to the app. Then it uploads the our files and starts the app. This involves downloading a lot of components and then it runs composer for us. When it finishes you'll get some status information like this:

At this point, your public/index.php file is being served on the URL it has created, http://slim-bookshelf.eu-gb.mybluemix.net in my case.

If you need to make any changes to your app, just cf push again.

Viewing logs

To view the logs, use cf logs {app name}. This will tail the logs, so run the command and then hit refresh to find out what went wrong

Next steps

As you may have noticed, in our buildpack options file, we enabled the PostgreSQL PHP extension. Next up is to configure and use a PostgreSQL database with our app and also change that domain name to something more personal.

I'll cover these points in separate articles.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *