PHP remains one of the most popular web development languages. Modern application frameworks for PHP have sophisticated features. Many of them bootstrap and manage dependencies via Composer and contain RDBMS-backed object model frameworks. Applications developed on top of these frameworks are ideal for deployment to CenturyLink AppFog, which is a Platform-as-a-Service (PaaS) backed by Cloud Foundry. In this tutorial, we cover the Laravel framework.

Tools Used

AppFog allows you to easily host cloud-native applications without worrying about provisioning, maintaining servers, and separate services. AppFog applications deploy quickly and scale automatically while having access to the full range of CenturyLink Cloud products.

CenturyLink MySQL is a cloud-hosted, managed relational database solution. It is backed by 100% flash storage and automatically scales to handle even the most demanding of workloads.

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 Cloud Platform with free onboarding assistance and receive a matching spend credit. The credit is based on your initial commitment period of platform use.

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

Enable AppFog

First, enable AppFog using the CenturyLink Cloud Control Portal.

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

Log in to AppFog Using the Cloud Foundry Tools

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

  1. Find the cf login command on the Control Portal. It looks similar to the following:

      cf login -a https://api.useast.appfog.ctl.io -o C007 -u your.username
    

    Login

  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: For more information on logging into AppFog using the Cloud Foundry CLI, read this Knowledge Base article.

Deploying a Laravel Application

Laravel is a PHP web application framework based on a model-view-controller (MVC) paradigm. It includes a number of helpful features, such as a modular packaging system, a database query builder, and an object-relational mapping. It refers to itself a the PHP framework for web artisans.

Getting the Laravel Quickstart Application

It's pretty simple to get up and running with Laravel. This tutorial uses the Laravel quickstart guide. You will need to follow along with both tutorials, so read the Laravel quickstart guide here. To download the quickstart application and prepare the project working directory, run the following commands:

    $ git clone https://github.com/laravel/quickstart-basic quickstart
    $ cd quickstart
    $ composer instal

Configuring Laravel 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 frameworks the tutorial uses.

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

Creating a Project Manifest

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

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

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.

Configuring 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
    

Configuring the Cloud Foundry CLI Tool

Since all 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:
        vendor/
        *~
        *.sample
        Gruntfile.js
        .git/
    
  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.

Adding CenturyLink MySQL

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

  1. Upload your application without launching it by using these commands 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>
        ...
        System-Provided:
        {
         "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 later in configuring your Laravel application.

For more information on using CenturyLink MySQL with AppFog applications, read this Knowledge Base article.

Configuring Laravel's Database Connection

Read more about database configuration in the Laravel documentation.

  1. With a text editor, open config/database.php.

  2. Find the line containing DB_CONNECTION and make sure it looks like this:

        'default' => env('DB_CONNECTION', 'mysql'),
    
  3. Find the section labeled connections and edit the mysql subsection so it looks like this:

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => '{hostname}',
            'port'      => '{port}',
            'database'  => '{dbname}',
            'username'  => '{username}',
            'password'  => '{password}',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
            'engine'    => null,
        ],
    

    Replace the variables in curly brackets with the values you saved from the cf env command earlier.

Launching Your Laravel Application on AppFog

Now, you are ready to deploy your Laravel application and run it in AppFog. Run the following commands:

    $ php artisan migrate
    $ cf push

If the deployment is successful, you will see output similar to this: Success

Finally, if you visit the URL listed in the urls: line of the output, you should see your Laravel quickstart application running. Laravel URL

For More Information

The Laravel documentation contains a wealth of knowledge about building and customizing your web application. In addition, there is more information about deploying Laravel applications to AppFog in our Knowledge Base. To learn more, read this article.

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

Other Framework Resources

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

What's Next?

These brief tutorials provides a thorough outline of what's needed to get an application written for a PHP framework to run on CenturyLink AppFog. Expanding on these tutorials, 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.