Bluemix fundamentals: Deploy a sample PHP application to the cloud

Intro to PHP web application development on IBM Bluemix



Get started working in the cloud using the language and tools you're already familiar with as a web developer. In this tutorial, learn how to run, modify, test, and deploy a simple PHP app to the cloud. Once you deploy the app to Bluemix, IBM's cloud development platform, anyone on the Internet can access it.

This tutorial is part of the "Bluemix fundamentals" tutorial series on developerWorks.


What you'll do in this tutorial

Step 1. Download the code

Step 2. Deploy the app to Bluemix

Step 3. Examine the code structure

Step 4. Run the app with the built-in server

Step 5. Modify the code and rerun the app

Step 6. Deploy the changed code to Bluemix

Quiz time!

What's next?

Let's get started

IBM Bluemix is where enterprise developers build, run, scale, and manage applications. Ready to start creating your own apps on Bluemix? This tutorial walks you through the steps for hosting a PHP application. You start with a sample PHP app, run it on your local system, modify the code, and then deploy the app to Bluemix so that anyone who's online can use it.

Here's a quick overview of how you'll work with Bluemix in this tutorial.

Overview diagram showing relationship between local and cloud components for deploying the sample app
Overview diagram showing relationship between local and cloud components for deploying the sample app

You run your own instance of the Apache web server to support your PHP app in the cloud. Once the server is deployed successfully, it runs continuously and isn't shared by anyone.

You also run an instance of the built-in PHP server locally, with the app loaded, on your computer. With this setup, you can quickly test and debug your app without connecting to the Internet and uploading code every time you make a change.

After you get your code running on the local server and are satisfied that it's ready for prime time, you upload your web app to the cloud (the process represented by the dotted black arrow in the diagram) using the Cloud Foundry command-line interface. Bluemix then sets your app up behind an Apache server with PHP module. Your app is then available through Bluemix, accessible over the Internet by anyone with a browser.

Knowledge that you need for this tutorial

  • Working knowledge of PHP development
  • General knowledge of client/server system basics and terminology

Software that you need for this tutorial

Step 1. Download the sample PHP web app

  1. Click this button to download the code for the sample PHP web app:
  2. Save the sample PHP web app to your computer and extract its contents, which consists of the following files and directories: Screenshot of the directory structure of the sample app's code
    Screenshot of the directory structure of the sample app's code
    • index.php, the main program, routes incoming requests to one of the templates in the views directory.
    • vars.php contains variables used in this PHP project. One variable contains the text for the website's title.
    • views is a directory that contains four templates — .tpl files — for the pages that constitute the app. Each template file can contain dynamic elements that are rendered on-the-fly with incoming requests.
    • static is a directory that contains all the static assets of the app, which can include CSS, images, and client-side JavaScript code that runs on the browser.

Step 2. Deploy the app to Bluemix

This app, like most PHP web apps, can be deployed immediately to Bluemix with no additional modification or configuration. To deploy it now to Bluemix:

  1. If you're not already logged in to Bluemix, run these commands from your OS command prompt to log in:
    cf api
    cf login
  2. Upload the app to Bluemix by running the command:

    cf push your app name

    The name you choose for your application must be unique on Bluemix — not used by any other Bluemix user. You'll get an error if the name (called a route) is taken.

    The command that you just ran:

    1. Uploads the app to Bluemix
    2. Runs the Cloud Foundry buildpack for PHP on Bluemix
    3. Starts an Apache web server instance, with PHP and your app loaded, on Bluemix
    4. Maps a route to your running app, enabling the app to be accessed over the Internet at the URL https://your app
  3. Open https://your app in your browser to try out the app — a simple web store called Lauren's Lovely Landscapes. The store currently sells three prints; each print's page displays the associated name, image, and price.
  4. Stop and reflect. You just finished deploying a working web application to the cloud! All you needed was the cf command-line tool and a Bluemix account.

See the glossary for additional details about how the buildpack works together with Bluemix to stage and deploy your app.

Starting with the next step, you'll begin to examine and modify the code. A syntax-highlighting editor with multiple-tabs support makes it much easier to work with the multiple PHP and template source code files.

Step 3. Examine the code structure

Take a look at the views directory from the extracted ZIP file. You can see the four template files that make up the website.

This diagram shows how the app works:

Diagram show how the sample app works
Diagram show how the sample app works

Each web request for a page of the Lauren's Lovely Landscapes store is routed by your code to one of the templates. When routing to the template, your code attaches a PHP variable named $site_title that contains website title information. Each template uses this object to render its title (Lauren's Lovely Landscapes).

In index.php, you can see the code that routes requests to a template, together with a variable containing the title from the $site_title object:

$lll_route = trim("$_SERVER[REQUEST_URI]", "/");

if (file_exists("views/$lll_route.tpl")) {
	$lllpage = ob_get_contents();

	echo $lllpage;

} elseif ...

In this case, the code first extracts the base name of the view from the URI and loads it into the $lll_route variable. Then it checks for the existence of a template file that matches the route name (alaska, say) and contains the .tpl file extension (alaska.tpl) — under the views subdirectory (views/alaska.tpl).

If the .tpl file exists, the contents are read into the output buffer and stored in the $lllpage variable. Reading the file into the output buffer enables index.php to process the PHP code in the template file. Once the HTML page with the processed PHP code is loaded into a variable ($lllpage), it can then be sent to the browser with the echo command.

If you examine one of the templates — alaska.tpl, for example — you can see the use of the $site_title PHP template variable to render the title:

            <title> <?php echo $site_title; ?> </title>

Step 4. Run the app with the built-in server

As of PHP version 5.4.0, an internal web server is included with every PHP install. This minimal server makes coding, testing, and demonstration of PHP apps considerably simpler. You no longer have to set up and configure an Apache or Ngnix server just to code in PHP.

You're now ready to run the app locally on your computer:

  1. At the root directory of your app, run:

    php -S localhost:8000

  2. Point a browser to the local server at http://localhost:8000/.
  3. Try out this instance of the application and see if you notice any difference from the Bluemix-hosted one. Because you're looking at the same app, produced with the same code, there should be no noticeable differences between the two.

Step 5. Modify the code and rerun the app

In this step, you'll modify the price of a print and see it updated on the locally running website right away.

  1. In your text editor, open up the antarctica.tpl file and look for the price in the source code.
  2. Change the price from 100.00 to 80.00 and save the file. The changed line should look like:
    <div id="price">80.00</div>
  3. Run the app locally again:

    php -S localhost:8000

  4. Point a browser to the PHP server.
  5. Select the Antarctica print and note the print's changed price.

Step 6. Deploy the changed code to Bluemix

To let everyone on the Internet know about the Antarctica print's new price, you'll deploy the changed app to Bluemix.

In Step 2, you saw how simple it is to deploy a PHP program to Bluemix. Again, run this command from the root directory of your code:

cf push your app name

After successful deployment, try out the app by pointing any web browser to:

https://your app

Glossary and status messages

Let's review some terms and status messages you're likely to encounter as you use Bluemix.


  • Droplet— A bundle ready to run in the cloud, including everything needed (for instance, a bundle with JVM, Liberty profile server, and your app) except an operating system.
  • Buildpack— An executable that takes the code or packaged server that you push, and bundles it up into a droplet.
  • Manifest— An optional file, named manifest.yml, that you can add to your project. The manifest file configures various parameters that affect the deployed server — including memory size, buildpack to use during deployment, services that are required, the disk space consumed, and so on. For simple PHP apps, you don't need a manifest; the system automatically detects and uses the Cloud Foundry buildpack and applies a default configuration.
  • Staging— The process handled by the buildpack, bundling what you uploaded with system components and dependencies into a valid droplet.
  • Droplet Execution Agent (DEA)— The system piece that's responsible for reconstituting the droplet and running your app in the cloud.
  • Warden— A mechanism to ensure that your app is isolated and secured from other running apps.

Status messages

When you issue the cf push CLI command you see a series of status messages. If you examine them carefully, you'll see the following sequential phases:

  1. Your push successfully uploaded the app source code to the staging area.
  2. If an existing instance of your app is already running, it's stopped before staging begins.
  3. The buildpack starts the staging process, which can include:
    1. Downloading and installing various system components (Apache web server and PHP module for Apache, for example)
    2. Downloading and installing runtime tools
    3. Putting your app into place
    4. Setting up the environment
    5. Bundling everything up to create the droplet
    To speed up these steps, staging makes heavy use of cache, so you might also see some reuse-from-cache messages in the mix.
  4. The DEA tries to start your app from the droplet, running under supervision of a warden (container).


Test your knowledge of what you've learned in this tutorial. Answers are below.

  1. You must install PHP of at least version 5.4.0 on your system before deploying PHP applications to Bluemix.
    1. True
    2. False; any version of PHP will do
    3. False; you don't need to install PHP at all
  2. The PHP built-in web server is useful because:
    1. It's more powerful and versatile than an old Apache web server
    2. It integrates with Bluemix to make hosting apps over the Internet easy
    3. Its command line is very short
    4. All of the above
    5. None of the above
  3. The application name you choose for your Lauren's Lovely Landscape Bluemix deployment must be:
    1. Long and important sounding
    2. Short and concise
    3. Unique among all your other apps
    4. Unique among all apps at the same data center
    5. Unused by any other Bluemix users in the entire world
  4. The ready-to-run bundle created by Bluemix during staging after you push your application is called a:
    1. Bluemix dropping
    2. Mullet
    3. Droplet
    4. Read-to-run bundle
    5. Bluemix zip file
  5. In Bluemix status messages, the term staging refers to:
    1. A Broadway play
    2. A bundling process handles by the buildpack, resulting in a droplet
    3. Loading of a buildpack
    4. The validation process before deployment
    5. A simulated run of your app prior to actual deployment to the cloud

Answers to quiz

1. c, 2. e, 3. e, 4. c, 5. b


What's next?

In this tutorial, you:

  • Deployed a PHP app from source code to Bluemix using the Cloud Foundry CLI
  • Reviewed the structure of the Lauren's Lovely Landscapes PHP web app
  • Modified the application code and used PHP's built-in server to run and test your modified app locally
  • Redeployed the modified code to Bluemix with the Cloud Foundry CLI and tested the app over the Internet

Now, continue learning "Bluemix fundamentals" on developerWorks with this next tutorial. You'll learn how to set up team collaboration and automated deployments for your Node.js app:

More Bluemix resources

For a steady stream of tutorials and other technical resources to help you learn, develop, and connect with Bluemix, see the Bluemix page on developerWorks.

Downloadable resources

Related topics


Sign in or register to add and subscribe to comments.

Zone=Cloud computing, Java development, Web development
ArticleTitle=Bluemix fundamentals: Deploy a sample PHP application to the cloud