Zend Framework 2 is an open-sourced object oriented PHP framework for building robust, high-performance web applications. While it is one of the older and more mature frameworks, it remains actively developed and has a wide user base.

Getting Started with AppFog

Before you can deploy anything to AppFog, you need to sign-up for a CenturyLink Cloud account and enable AppFog. If you don't have a CenturyLink Cloud account, migrate to the CenturyLink Platform with free onboarding assistance and receive a matching spend credit. The credit is based on your initial commitment period of platform use.

To get a good understanding of AppFog, you should read the Getting Started Guide. The following steps will get you up and running quickly.

Enable AppFog

First, enable AppFog in the CenturyLink Cloud Control Portal.

  1. Go to the Control Panel and log in.
  2. Select AppFog from the pull-down menu. Control Portal
  3. Select a Region to host your application. Host Region

Log into AppFog Using the Cloud Foundry Tools

Once AppFog is set up in your CenturyLink Cloud account, you need to log-in to AppFog using the Cloud Foundry CLI. The Control Panel gives you an exact command from the Overview tab.

  1. Find the cf login command on your AppFog control panel. It will look similar to this:
        cf login -a https://api.useast.appfog.ctl.io -o C007 -u your.username
  2. Run the command in your project directory.
  3. Enter your CenturyLink Cloud password when prompted. Password
  4. Select the Dev application space. Dev App

    Note: To learn more about logging into AppFog using the Cloud Foundry command-line tools, read this Knowledge Base article.

Download the Skeleton Application

We will begin by using Composer to install the Zend Framework 2 skeleton application. Run the following commands, replacing <your-project-directory> with the name of a new project working directory for the application.

    $ composer create-project --stability="dev" zendframework/skeleton-application <your-project-directory>
    $ cd <your-project-directory>
    $ composer install

You will see lots of output, particularly during the first command. Should the first command ask if you'd like to remove the existing VCS history, answer 'yes'. Note that some of the Composer commands can take a little while to complete, particularly on a slow connection.

Configure Zend Framework 2 for AppFog

AppFog uses buildpacks to install and deploy services that run and support your application. A buildpack is a package containing runtime support for a specific type of application. For instance, the PHP buildpack used in this tutorial includes a specific version of PHP, a web server, and several support utilities. The PHP buildpack works closely with Composer to install project dependencies, which integrate conveniently with the PHP framework we will look at in this tutorial.

Configuration for an AppFog project happens inside the project's working directory. Your project working directory is the directory you created in the previous section.

Create a Project Manifest

In your project working directory, create a file manifest.yaml with a text editor, and edit it to look like this:

     - name: <your-app-name>
       memory: 512M
       instances: 1
       buildpack: https://github.com/cloudfoundry/php-buildpack

Replace <your-app-name> with a project or application name. The name can’t contain any spaces. Note that because AppFog uses a global project namespace, this needs to be unique across AppFog.

Configure the PHP Buildpack

The PHP buildpack configuration is kept in a subdirectory of your project directory. This configuration allows us to specify PHP versions and configure different subsystems and services, such as Composer and Apache HTTPD. To configure the buildpack for your project, follow these instructions:

  1. Inside your project working directory, create a .bp-config directory:

       $ mkdir .bp-config
  2. With a text editor, create and edit .bp-config/options.json to look like this:

         "PHP_VERSION": "PHP_55_LATEST",
         "COMPOSER_VENDOR_DIR": "vendor",
         "WEBDIR": "public",
         "PHP_EXTENSIONS": [ "bz2", "zlib", "openssl", "curl", "mcrypt", "mbstring", "pdo", "pdo_mysql" ]
  3. From your project working directory, run the following command:

       $ mkdir -p .bp-config/httpd/extra
  4. With a text editor, create and edit .bp-config/httpd/extra/httpd-modules.conf to look like this:

       LoadModule authz_core_module modules/mod_authz_core.so
       LoadModule authz_host_module modules/mod_authz_host.so
       LoadModule log_config_module modules/mod_log_config.so
       LoadModule env_module modules/mod_env.so
       LoadModule setenvif_module modules/mod_setenvif.so
       LoadModule dir_module modules/mod_dir.so
       LoadModule mime_module modules/mod_mime.so
       LoadModule reqtimeout_module modules/mod_reqtimeout.so
       LoadModule unixd_module modules/mod_unixd.so
       LoadModule mpm_event_module modules/mod_mpm_event.so
       LoadModule proxy_module modules/mod_proxy.so
       LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
       LoadModule remoteip_module modules/mod_remoteip.so
       # Needed for PHP application framework
       LoadModule rewrite_module modules/mod_rewrite.so
       LoadModule access_compat_module modules/mod_access_compat.so

Configure the Cloud Foundry CLI Tool

Since all of our application dependencies will be handled by Composer, we can configure the Cloud Foundry CLI tool to exclude those from upload commands. This can save time and resources by significantly reducing the number of files uploaded by the tool. Configure the CLI tool by following these steps:

  1. In your project working directory, create and edit .cfignore to look like this:

  2. If your text editor uses different filenames for backups, temporary files, or lock files, add those patterns to the .cfignore file.

  3. If your application uses temporary files, installation-specific configuration files, or other files that don't need to be uploaded to AppFog, add those filenames or patterns to the .cfignore file.

Add CenturyLink MySQL

Next, upload your application for the first time and prepare a CenturyLink MySQL instance for it. Follow these steps:

  1. Upload your application without launching it by running this command in your project directory.

       $ cf push --no-start
  2. Provision a MySQL instance and attach it to your application using the following commands:

       $ cf create-service ctl_mysql micro <your-database-name>
       $ cf bind-service <your-app-name> <your-database-name>

    Replace <your-database-name> and <your-app-name> with the name of your AppFog application and a database name.

  3. Find your MySQL configuration and authentication information using the cf env command.

       $ cf env <your-app-name>
        "VCAP_SERVICES": {
         "ctl_mysql": [
           "credentials": {
            "certificate": "-----BEGIN CERTIFICATE-----{...}-----END CERTIFICATE-----",
            "dbname": "{dbname}",
            "hostname": "{hostname}",
            "password": "{password}",
            "port": {port},
            "username": "{username}"
           "label": "ctl_mysql",
           "name": "<your-database-name>",
           "plan": "micro",
           "tags": []
  4. Record the dbname, hostname, password, port, and username, for use in configuring your application.

  5. Open config/autoload/global.php with a text editor.
  6. Find the section that begins return array( and edit it to look like this:

       return array(
           'db' => array(
               'driver'         => 'Pdo',
               'dsn'            => 'mysql:dbname={dbname};host={hostname};port={port}',
               'driver_options' => array(
                   PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
           'service_manager' => array(
               'factories' => array(
                   => 'Zend\Db\Adapter\AdapterServiceFactory',
  7. Replace {dbname}, {hostname}, and {port} with the values you recorded in Step 4.

  8. Create config/autoload/local.php with a text editor and edit it to look like this:
       return array(
           'db' => array(
               'username' => '{username}',
               'password' => '{password}',
  9. Replace {username} and {password} with the values you recorded in Step 4.

For more information on using CenturyLink MySQL with AppFog applications, read this Knowledge Base article. For more information on configuring Zend Framework 2 database modules, see the Zend Framework User Guide.

Deploy and Run Your Application

Now, it's time to deploy your application to AppFog. Run the following command:

   $ cf push

Continue Following the Tutorial

Now that your application has been configured to run in AppFog and use CenturyLink MySQL, you're ready to follow the rest of the Zend Framework introduction guide. Head to Getting Started: A Skeleton Application and follow along. When you are ready to deploy and test your application, run the cf push command from the "Deploying and Running Your Application" section.

For More Information

Be sure to check out the CenturyLink Cloud blog and our Knowledge Base for more information and helpful tutorials.

Other Framework Resources

We have tutorials available for other popular frameworks as well. You might want to check them out.

What's Next?

This brief tutorial provides a thorough outline of what's needed to get an application written for a PHP framework to run on CenturyLink AppFog. Expanding on the tutorial, you can add CenturyLink Object Storage, Autoscale, and other robust cloud services to give your application the performance edge it needs.

Sign-up for our Developer-focused newsletter CODE. Designed hands-on by developers, for developers. Keep up to date on topics of interest: tutorials, tips and tricks, and community building events.

We’re a different kind of cloud provider – let us show you why.