Introducing "PaaS in a Box" – Scalable, Flexible, Portable PHP in the Cloud
Building Scalable PHP Applications Using Google's App Engine
Transcript of Building Scalable PHP Applications Using Google's App Engine
![Page 1: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/6.jpg)
Why PHP?
Top feature request for App Engine
![Page 7: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/7.jpg)
Real World Example
Converting joind.in to App Engine
![Page 8: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/8.jpg)
![Page 9: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/12.jpg)
How do we log?
![Page 13: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/14.jpg)
How do we log?
![Page 15: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/15.jpg)
Hmmm… now we have a database error
![Page 16: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/16.jpg)
What about the database?
![Page 17: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/20.jpg)
How do we cache?
![Page 21: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/22.jpg)
Can we upload files?
![Page 23: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/23.jpg)
Uploads?
cloud.google.com/console
![Page 24: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/24.jpg)
App Engine Access to Cloud Storage
![Page 25: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/25.jpg)
$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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/28.jpg)
Can we send email?
![Page 29: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/30.jpg)
How can we send email in the background?
Web Request Worker
Task Queue
![Page 31: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/33.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/35.jpg)
![Page 36: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/36.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/37.jpg)
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 38: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/38.jpg)
'Unofficial' GAE PHP tips BlogThe I/O session talkDocumentationCloud Platform BlogGetting started with other apps:WordPress WP starter project on GitHub with the App Engine WordPress pluginLaravelDrupal
Q & A… and Resources
![Page 39: Building Scalable PHP Applications Using Google's App Engine](https://reader033.fdocuments.in/reader033/viewer/2022052514/5870da951a28ab982b8bfeed/html5/thumbnails/39.jpg)
Thank you!
</end>