Upload
vvaswani
View
369
Download
2
Embed Size (px)
Citation preview
Using PHP with IBM Bluemix
Vikram VaswaniAugust 2017
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
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
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, ...
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
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/
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
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
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
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
Development
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
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
Development: Dependencies
Sample co mpo se r. jso n file:
{
"require": {
"php": ">=7.0.0",
"slim/slim": "^3.8",
"slim/php-view": "^2.2"
}
}
Development: Service Discovery
Find services using the Bluemix Dashboard● 120+ services, IBM and third-party● Many services include a free tier
Development: Service Instantiation
Instantiate new services using the Bluemix Dashboard● Leave services unbound for local development● Obtain credentials from each service panel
Deployment
Pre-Deployment: Service Credentials● Available in Bluemix environment for all bound services
via VCAP_ SERVICES variable● Structured as JSON document
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'];
}
Deployment: Configuration Artifacts● Application manifest (manife st. yml)● Buildpack configuration data (. bp-co nfig /* )
Deployment: Application Manifest
Use manife st. yml to define application attributes:● Host name● Application name● Memory ● Number of instances● Buildpack
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
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
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}"
}
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/
Deployment: Code Push● Set API endpointcf api ENDPOINT
● Log incf login
● Push applicationcf push
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
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
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/
Continuous Delivery
Service Overview● Secure, automated, repeatable build/deployment
processes● Integrated toolchains
● Bluemix services● Third-party tools
– Slack– GitHub – Sauce Labs– PagerDuty
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/
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
Demonstration
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
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
Questions?
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/