Digital Developer Conference on Data and AI: Essential data science, machine learning, and AI skills and certification Register for free

Deploy a Laravel application using Cloud Foundry

This tutorial shows how to start building your very first Laravel application and deploy it on public cloud. First, I’ll introduce you to the Laravel framework and then outline the development and deployment processes.

Laravel is an open source PHP web application framework with expressive, elegant syntax that eases common development tasks used in many web projects. It is extremely powerful for crafting web applications. You can do anything from small to huge enterprise projects with Laravel and it has a fantastic community behind it that is available to help.

Other technologies featured in this tutorial are:

  • IBM Cloud, a suite of cloud computing services that offers both platform as a service (PaaS) and infrastructure as a service (IaaS) capabilities.
  • Artisan CLI, a command-line interface (CLI) included with Laravel that provides a number of helpful commands for developing your application.
  • Cloud Foundry, an open source cloud application PaaS that runs on IBM Cloud, enabling you to deploy and scale apps without managing servers.
  • Composer, a tool for dependency management in PHP that allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Prerequisites

This tutorial assumes you understand the fundamentals of Laravel development and have a basic familiarity with PHP. Before starting, make sure that everything you need is ready:

Estimated time

Completing this tutorial should take about 30 minutes.

Step 1: Install Laravel

Laravel utilizes Composer to manage its dependencies. A complete getting started guide is available to help you successfully install Composer on your system. You can then install Laravel by using either Composer dependency manager or Laravel installer.

Option 1: Use Composer dependency manager

In order for this to work, you must place the Composer system vendor bin directory in your $PATH after downloading Composer.

  • MacOS: $HOME/.composer/vendor/bin
  • GNU / Linux distributions: $HOME/.config/composer/vendor/bin
  • Windows: %USERPROFILE%\AppData\Roaming\Composer\vendor\bin

Next, create a fresh Laravel skeleton project with Composer:

  • Point your CMD/Terminal to your preferred destination using the following command:

     cd + "DIRECTORY NAME"
    
  • Issue the Composer create-project command to freshly install a new Laravel project:

     composer create-project --prefer-dist laravel/laravel blog "5.8.*""
    

Option 2: Use Laravel installer

To create a fresh Laravel application by using the installer, do the following:

  • Download installer using Composer by issuing this command from your CMD/Terminal:

     composer global require laravel/installer
    
  • You can test it out by typing the laravel command on your CMD/Terminal after installation.

    Screen capture of Laravel CLI test

  • Create a fresh Laravel installation in the directory you specified before using the following command:

     Laravel new [ProjectName]
    

    This creates a directory with the specified project name containing a new Laravel installation with all Laravel dependencies installed.

Step 2: Test your new Laravel project

Point inside your new Laravel directory from your CMD/Terminal by issuing the following command:

cd [Laravel Directory Name]

To make it easy to test the application, an interesting thing in Laravel is that it comes with PHP’s built-in development server to serve your application with the following command:

php artisan serve

This command starts a development server at http://localhost:8000.

Step 3: Edit your Laravel application

  • Open your text editor or IDE and import your Laravel project directory.
  • Under your imported project directory, expand resources > views.
  • Open the welcome.blade.php file.

    Screen capture of welcome.blade.php file

    Blade is a simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade does not add overhead to your application.

    Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.

  • Make a small change to the <div class="title m-b-md"> content (as you can see in the screen capture below, I added Welcome to IBM Cloud text).

    Screen capture of blade template change

  • Save the file.

  • Test your change on the development server by executing the following command:

     php artisan serve
    

    This command starts a development server at http://localhost:8000.

Step 4: Deploy your Laravel application

To deploy your app on IBM Cloud, you have to use the Cloud Foundry PHP Buildpack and PHP 7.0, which deploys PHP applications to Cloud Foundry-based systems.

  • In the same application directory, create a new manifest.yml file that describes the procedures and guidance for deploying your app. Use this format for guidance:

     ---
     applications:
     - name: [CF AppName]
       memory: Specify the memory needed.
       buildpack: Specify the dedicated buildback.[ https://github.com/cloudfoundry/php-buildpack ]
       random-route: true  #For testing purposes a newly generated URL will be used instead of specifying a dedicated one.
       env:
         APP_DEBUG: false
         CF_STAGING_TIMEOUT: 15
         CF_STARTUP_TIMEOUT: 15
    

    You will need to modify that manifest.yml file and add your own [AppName] to launch it. Additionally, you can set the APP_DEBUG=true to debug any Laravel issues if necessary.

    You must also add a .pb-config folder under the project root directory and then include the options.json file inside of it. This configuration file is used for the buildpack itself, as it instructs the buildpack of what to download, where to download it from, and how to install it. It allows you to configure package names and versions.

    You can use the following definition:

     {
         "PHP_VERSION": "{PHP_70_LATEST}",
         "COMPOSER_VENDOR_DIR": "vendor",
         "WEBDIR": "public",
         "PHP_EXTENSIONS": [ "bz2", "zlib", "openssl", "curl", "mcrypt", "mbstring", "pdo"]
      }
    
  • Log in to your IBM Cloud account

  • Open the Navigation Menu on the header and select Cloud Foundry.

    Screen capture of IBM Cloud navigation menu

  • Scroll down to the Application Runtimes section and select .php.

    Screen capture of application runtimes section

  • On the Create a Cloud Foundry Sample App page, select a region from the list.

  • Enter a unique name for your application in the App name field.
  • Click Create.

To deploy your app onto the cloud, return back to the CMD/Terminal.

  • Save the changes made on your project.
  • Log in to IBM Cloud from your CMD/Terminal using the following command:
ibmcloud login
  • Enter your IBM Cloud account credentials.
  • Select a region to deploy.
  • Use ibmcloud target --cf to target the Cloud Foundry org and space interactively.
  • Use cf login to specify a target API endpoint, login credentials, an org, and a space.
  • Use CF push + [APP_NAME] to deploy your application.
  • Wait until the deployment process completes and then you will be able to view the app URL value associated with the routes key.

Congratulations! Now you have a Laravel application fully up and running over IBM Cloud.

Screen capture of Laravel app landing page

Conclusion

The goal of this tutorial is to build a simple PHP Laravel web application and deploy it to the public cloud using Cloud Foundry. You can learn more by trying some additional Cloud Foundry tutorials available on IBM Developer.