building scalable php applications using google's app engine

39
Building Scalable PHP Applications Using Google’s App Engine Mandy Waite google.com/+MandyWaite @tekgrrl Amy Unruh google.com/+AmyUnruh @amygdala

Upload: dinhthu

Post on 07-Jan-2017

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Building Scalable PHP Applications Using Google's App Engine

Building Scalable PHP ApplicationsUsing Google’s App Engine

Mandy Waite google.com/+MandyWaite@tekgrrl

Amy Unruhgoogle.com/+AmyUnruh@amygdala

Page 2: Building Scalable PHP Applications Using Google's App Engine

Scale on Demand

Analytics

Fast Access to Cloud-Scale Services

Global Availability

Focus on developing your

App

Why Cloud for PHP?

Page 3: Building Scalable PHP Applications Using Google's App Engine

App Engine

Simple to Scale- AutoScale

Trivial to manage- Fully managed- No patches- 24x7 operation by Google SREs

Easy to develop- Free to start- Local dev environment- Service abstractions

Page 4: Building Scalable PHP Applications Using Google's App Engine

Handling variable load

Volatile Demand Fluctuation Steady Demand Growth

Inefficiency

With App Engineonly pay for what you use

With App Enginescale with efficiency and reliability

Downtime

Inefficiency

Page 5: Building Scalable PHP Applications Using Google's App Engine

App Engine’s Cloud Scale Services

Cloud SQL

...and more

PageSpeedCloud Datastore

Cloud Storage

Cron JobsMemcache

Page 6: Building Scalable PHP Applications Using Google's App Engine

Why PHP?

Top feature request for App Engine

Page 7: Building Scalable PHP Applications Using Google's App Engine

Real World Example

Converting joind.in to App Engine

Page 8: Building Scalable PHP Applications Using Google's App Engine
Page 9: Building Scalable PHP Applications Using Google's App Engine

PHP on App Engine:what’s the environment like?

github.com/GoogleCloudPlatform/appengine-php - 5.4.19http://php-minishell.appspot.com/phpinfo: Production phpinfo()

Page 10: Building Scalable PHP Applications Using Google's App Engine

How do we start?

application: <your-app-name>version: unoruntime: phpapi_version: 1

handlers:- url: /inc static_dir: inc- url: /.* script: index.php

app.yamlappengine.google.com

Page 11: Building Scalable PHP Applications Using Google's App Engine

What is the environment like?

google_app_engine.enable_functions = "php_sapi_name"

php.ini

Page 12: Building Scalable PHP Applications Using Google's App Engine

How do we log?

Page 13: Building Scalable PHP Applications Using Google's App Engine

How do we log?function write_log($level = 'error', $msg, $php_error = FALSE){ if ($this->_enabled === FALSE) { return FALSE; } $level = strtoupper($level); if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) { return FALSE; }

syslog($level == 'ERROR' ? LOG_ERR : LOG_INFO, $msg); return TRUE;}

Page 14: Building Scalable PHP Applications Using Google's App Engine

How do we log?

Page 15: Building Scalable PHP Applications Using Google's App Engine

Hmmm… now we have a database error

Page 16: Building Scalable PHP Applications Using Google's App Engine

What about the database?

Page 17: Building Scalable PHP Applications Using Google's App Engine

What about the database?

$db['default']['hostname'] = ":/cloudsql/<PROJECT>:<INSTANCE>";$db['default']['username'] = "joindin";$db['default']['password'] = "password";$db['default']['database'] = "joindin";$db['default']['dbdriver'] = "mysql";$db['default']['dbprefix'] = "";$db['default']['pconnect'] = TRUE;

Page 18: Building Scalable PHP Applications Using Google's App Engine

How do we call APIs?

Recommended:

use App Engine’s URLFetch service via standard HTTP stream wrappers and functions like file_get_contents()

Page 19: Building Scalable PHP Applications Using Google's App Engine

Calling APIs?

$opts = array( 'http' => array( 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-length: ".strlen($msg)."\r\n", 'content' => $msg));$ctx = stream_context_create($opts);

$resp = file_get_contents( 'http://api.defensio.com' . $loc, false, $ctx);

Page 20: Building Scalable PHP Applications Using Google's App Engine

How do we cache?

Page 21: Building Scalable PHP Applications Using Google's App Engine

Output Cachingfunction _write_cache($output) { $CI =& get_instance(); $path = $CI->config->item('cache_path'); $cache_path = ($path == '') ? BASEPATH . 'cache/' : $path; $uri = $CI->config->item('base_url') . $CI->config->item('index_page') . $CI->uri->uri_string(); $cache_path .= md5($uri);

$mc = new Memcache; $mc->set($cache_path, $output, $this->cache_expiration * 60); return;}

Page 22: Building Scalable PHP Applications Using Google's App Engine

Can we upload files?

Page 23: Building Scalable PHP Applications Using Google's App Engine

Uploads?

cloud.google.com/console

Page 24: Building Scalable PHP Applications Using Google's App Engine

App Engine Access to Cloud Storage

Page 25: Building Scalable PHP Applications Using Google's App Engine

$options = [ 'gs_bucket_name' => 'joindin' ];

$upload_url = CloudStorageTools::createUploadUrl( '/event/edit/'.$this->edit_id, $options);

echo form_open_multipart($upload_url);

Uploads?

Page 26: Building Scalable PHP Applications Using Google's App Engine

Uploads?— and Cloud Storage stream wrapper

$gs_name = $_FILES[$field]['tmp_name'];$gs_type = $_FILES[$field]['type'];

$options = [ "gs" => [ "Content-Type" => $gs_type, "acl" => 'public-read']];$ctx = stream_context_create($options);

rename($gs_name, $this->upload_path . $this->file_name, $ctx);

Page 27: Building Scalable PHP Applications Using Google's App Engine

Public URLs from Cloud Storage Files?

<?php $path = "gs://joindin/inc/img/event_icons/';$img = ( !empty($event->event_icon) && is_file($path.$event->event_icon)) ? escape($event->event_icon) : 'none.gif'; $public_url = CloudStorageTools::getPublicUrl($path.$img, true);?>

Page 28: Building Scalable PHP Applications Using Google's App Engine

Can we send email?

Page 29: Building Scalable PHP Applications Using Google's App Engine

Can we send email?

require_once 'google/appengine/api/mail/Message.php';use google\appengine\api\mail\Message;

... $message = new Message($mail_options); $message->send();

Page 30: Building Scalable PHP Applications Using Google's App Engine

How can we send email in the background?

Web Request Worker

Task Queue

Page 31: Building Scalable PHP Applications Using Google's App Engine

foreach ($send_to as $email) { // Send mail in a worker. $task = new PushTask('/tasks/email', [ 'from' => $from, 'to' => $email, 'subject' => $subj, 'msg' => $msg ]); // Use the default queue. $task_name = $task->add();}

Using Task Queues to send email in the background

Page 32: Building Scalable PHP Applications Using Google's App Engine

The task handler mapping

handlers:- url: /inc static_dir: inc- url: /tasks/email script: workers/email.php login: admin

app.yaml

Page 33: Building Scalable PHP Applications Using Google's App Engine

Can we send email? - the task handler

require_once 'google/appengine/api/mail/Message.php';use google\appengine\api\mail\Message;

if (!isset($_SERVER['HTTP_X_APPENGINE_QUEUENAME'])) { exit(); }...

Page 34: Building Scalable PHP Applications Using Google's App Engine

Can we send email? - the task handler (cont.)

...$mail_options = [ "sender" => $_POST['from'], "to" => $_POST['to'], "subject" => $_POST['subject'], "htmlBody" => $_POST['msg']];

try { $message = new Message($mail_options); $message->send();} catch (\InvalidArgumentException $e) { syslog(LOG_ERR, $e->getMessage());}

Page 35: Building Scalable PHP Applications Using Google's App Engine
Page 36: Building Scalable PHP Applications Using Google's App Engine

Migrating a Typical Application

PHP

Use streams API for network calls

Cache to Memcache

Mail API to send email

Cron, Task Queues for background jobs

MySQL to Cloud SQL,or Cloud Datastore for NoSQL

File Writes and Uploads to Google Cloud Storage

Create App Engine application

Configureapp.yaml, php.ini

Page 37: Building Scalable PHP Applications Using Google's App Engine

Apply at goo.gl/Bc1sA

Use Promo Code: fphpp-con

Google Cloud Platform Starter Pack allows developers from affiliated partners to receive $2,000 of credit - $1,000 for Google App Engine and $1,000 for Google Compute Engine and other Cloud Platform services

Google Cloud Platform Starter Pack

Page 39: Building Scalable PHP Applications Using Google's App Engine

Thank you!

</end>