38
Using PHP with IBM Bluemix Vikram Vaswani August 2017

Using PHP with IBM Bluemix

Embed Size (px)

Citation preview

Page 1: Using PHP with IBM Bluemix

Using PHP with IBM Bluemix

Vikram VaswaniAugust 2017

Page 2: Using PHP with IBM Bluemix

Vikram Vaswani

Founder, Melonfire● Product design and implementation with open source

technologies● 20+ products launched for customers worldwideAuthor and active PHP community participant● 7 books for McGraw-Hill USA● 250+ tutorials for IBM dW, Zend Developer Zone and

others

More information at http://vikram-vaswani.in

Page 3: Using PHP with IBM Bluemix

Vikram Vaswani

Active IBM Bluemix user since 2014● IBM Cloud Champion● 15+ PHP applications on IBM Bluemix

● Stray assist: http://stray-assist.mybluemix.net/● Invoice generator: https://invoice-generator.mybluemix.net/● ATM finder: http://atm-finder.mybluemix.net/● Document indexer: https://pdf-keyword-search.mybluemix.net

Code samples at https://github.com/vvaswani

Page 4: Using PHP with IBM Bluemix

Bluemix● Cloud PaaS

● Secure and scalable● Pay-per-use

● Multiple programming languages● PHP, Java, Go, Ruby, Swift, ...

● Multiple services (own and third-party)● Text to Speech, Object Storage, Spark, Sendgrid, ...

Page 5: Using PHP with IBM Bluemix

Bluemix● Serverless computing

● Apache OpenWhisk● Integrated continuous delivery toolchains

● Own and externally-hosted repositories● Web IDE● Third-party integrations: Slack, GitHub● Blue-green deployment and rollback

Page 6: Using PHP with IBM Bluemix

Bluemix and PHP● PHP runtime via Cloud Foundry PHP buildpack● Key features

● Choice of Web servers: Apache or nginx● Choice of PHP versions: PHP 5 or PHP 7● Support for various PHP extensions and modules● Support for Composer and custom startup scripts● Debug logs

PHP buildpack docs at http://docs.cloudfoundry.org/buildpacks/php/

Page 7: Using PHP with IBM Bluemix

Development Approaches

Use the PHP starter application as base● Deploy the starter application to Bluemix● Download the source code and modify locally● Incorporate local or remote services● Redeploy the final application to Bluemix and bind

services as neededBenefits● Simple, easy to understand● Best for first-time users

Page 8: Using PHP with IBM Bluemix

Development Approaches

Start from scratch ● Develop the application locally● Incorporate local or remote services● Deploy the final application to Bluemix and bind

services as neededBenefits● Greater flexibility and customization options● Best for experienced developers

Page 9: Using PHP with IBM Bluemix

Development Steps

1.Define and install application dependencies 2.Configure application with local service credentials3.Find and instantiate any remote services needed for

development4.Configure application with remote service

credentials5.Develop, test and finalize

Page 10: Using PHP with IBM Bluemix

Deployment Steps

1.Find and instantiate remote services 2.Update application to retrieve remote service

credentials from Bluemix3.Define application manifest4.Push application5.Bind remote services6.Test application

Page 11: Using PHP with IBM Bluemix

Development

Page 12: Using PHP with IBM Bluemix

Development: Prerequisites● Bluemix account● Third-party service accounts (if needed)● Local PHP development environment● CloudFoundry CLI● Application source code (obviously!)

CloudFoundry CLI installation instructions at https://docs.cloudfoundry.org/cf-cli/install-go-cli.html

Page 13: Using PHP with IBM Bluemix

Development: Dependencies● Use co mpo se r. jso n to

● Specify the PHP version● Declare dependencies and version constraints

● Use co mpo se r. lo ck to● Lock your application to specific versions of

dependencies

Page 14: Using PHP with IBM Bluemix

Development: Dependencies

Sample co mpo se r. jso n file:

{

"require": {

"php": ">=7.0.0",

"slim/slim": "^3.8",

"slim/php-view": "^2.2"

}

}

Page 15: Using PHP with IBM Bluemix

Development: Service Discovery

Find services using the Bluemix Dashboard● 120+ services, IBM and third-party● Many services include a free tier

Page 16: Using PHP with IBM Bluemix

Development: Service Instantiation

Instantiate new services using the Bluemix Dashboard● Leave services unbound for local development● Obtain credentials from each service panel

Page 17: Using PHP with IBM Bluemix

Deployment

Page 18: Using PHP with IBM Bluemix

Pre-Deployment: Service Credentials● Available in Bluemix environment for all bound services

via VCAP_ SERVICES variable● Structured as JSON document

Page 19: Using PHP with IBM Bluemix

Pre-Deployment: Service CredentialsSample code:<?php

// if BlueMix VCAP_SERVICES environment available

// overwrite local credentials with BlueMix credentials

if ($services = getenv("VCAP_SERVICES")) {

$json = json_decode($services, true);

$config['db']['hostname'] = $json['cleardb'][0]['credentials']['hostname'];

$onfig['db']['username'] = $json['cleardb'][0]['credentials']['username'];

$config['db']['password'] = $json['cleardb'][0]['credentials']['password'];

$config['db']['name'] = $json['cleardb'][0]['credentials']['name'];

}

Page 20: Using PHP with IBM Bluemix

Deployment: Configuration Artifacts● Application manifest (manife st. yml)● Buildpack configuration data (. bp-co nfig /* )

Page 21: Using PHP with IBM Bluemix

Deployment: Application Manifest

Use manife st. yml to define application attributes:● Host name● Application name● Memory ● Number of instances● Buildpack

Page 22: Using PHP with IBM Bluemix

Deployment: Application Manifest

Sample manife st. yml file:

---

applications:

- name: myapp-[initials]

memory: 256M

instances: 1

host: myapp-[initials]

buildpack: https://github.com/cloudfoundry/php-buildpack.git

stack: cflinuxfs2

Page 23: Using PHP with IBM Bluemix

Deployment: Buildpack Configuration● Use . bp-co nfig /o ptio ns. jso n to:

● Configure the Web server and document root● Set the PHP version (if conflict, co mpo se r. jso n gets priority)● Enable PHP modules and Zend extensions

● Use . bp-co nfig /php/php . ini. d/* . ini to:● Configure PHP settings● Enable PHP extensions

● Use . bp-co nfig /httpd/httpd. co nf to:● Configure Apache settings

Page 24: Using PHP with IBM Bluemix

Deployment: Buildpack Configuration

Sample . bp-co nfig /o ptio ns. jso n file:

{

"WEB_SERVER": "httpd",

"COMPOSER_VENDOR_DIR": "vendor",

"WEBDIR": "public",

"PHP_VERSION": "{PHP_70_LATEST}"

}

Page 25: Using PHP with IBM Bluemix

Deployment: Buildpack Configuration

Sample . bp-co nfig /php/php. ini. d/php. ini file:

extension=mysqli.so

default_charset="UTF-8"

display_errors="1"

display_startup_errors="1"

error_reporting="E_ALL"

List of PHP buildpack extensions enabled by default at https://github.com/cloudfoundry/php-buildpack/releases/

Page 26: Using PHP with IBM Bluemix

Deployment: Code Push● Set API endpointcf api ENDPOINT

● Log incf login

● Push applicationcf push

Page 27: Using PHP with IBM Bluemix

Deployment: Code Push ResultsWhen a PHP application is pushed:● The application metadata is stored and a route and record is created for it.● The application assets are uploaded.● The PHP buildpack is downloaded and executed.● The PHP buildpack downloads and configures the Web server and PHP binary.● The PHP buildpack uses Composer to install application dependencies.● The staged PHP application is packaged into a "droplet".● The droplet is uploaded and the staged PHP application is started.

More information at https://docs.cloudfoundry.org/concepts/how-applications-are-staged.html

Page 28: Using PHP with IBM Bluemix

Deployment: Service Binding● Bind services:cf bind-service APP-NAME SERVICE-NAME

● Set custom environment variables:cf set-env APP-NAME VARIABLE VALUE

● Restage application:cf restage APP-NAME

Page 29: Using PHP with IBM Bluemix

Post-Deployment: Debugging● View logs:cf logs APP-NAME cf logs APP-NAME --recent

● Start secure shell session:cf ssh APP-NAME

Learn more at http://vikram-vaswani.in/weblog/2015/03/19/debugging-php-errors-on-ibm-bluemix/

Page 30: Using PHP with IBM Bluemix

Continuous Delivery

Page 31: Using PHP with IBM Bluemix

Service Overview● Secure, automated, repeatable build/deployment

processes● Integrated toolchains

● Bluemix services● Third-party tools

– Slack– GitHub – Sauce Labs– PagerDuty

Page 32: Using PHP with IBM Bluemix

Service Features● Toolchain templates● Pipelines● Web IDE● Hosted repository● Custom scripting

Learn more at http://vikram-vaswani.in/weblog/2017/08/22/test-and-deploy-php-applications-automatically-on-ibm-bluemix/

Page 33: Using PHP with IBM Bluemix

Example

Tests pass?

NewPR

Approve and

merge PR

Reject and

return PR

Pull andbuild code

Deploy code

Source code repositorydev-master branch Bluemix environment

dev-master.mybluemix.net

Performcustomactions

Bluemix webhook

Bluemix continuous delivery service

Y

N

Page 34: Using PHP with IBM Bluemix

Demonstration

Page 35: Using PHP with IBM Bluemix

Demonstration Overview● Clone application source code● Configure with local development MySQL database● Create ClearDB Managed MySQL Database service on Bluemix● Update application code to use service credentials in Bluemix

environment● Create application manifest● Configure Cloud Foundry PHP buildpack● Deploy application to Bluemix● Bind ClearDB Managed MySQL Database service to application● Test and debug deployment● Implement continuous delivery toolchain

Page 36: Using PHP with IBM Bluemix

Sample Application● PHP+MySQL application using Slim micro-

framework● Uses Bluemix ClearDB Managed MySQL service● Uses customized buildpack configuration

Code at https://github.com/vvaswani/bluemix-cities

Page 37: Using PHP with IBM Bluemix

Questions?

Page 38: Using PHP with IBM Bluemix

Contact Information

Email:● vikram-vaswani.in/contactWeb:● www.melonfire.com● vikram-vaswani.inSocial networks:● plus.google.com/100028886433648406825MySQL/PHP user group:● https://plus.google.com/+mmpugindia/