In this article, I’ll show you how to take advantage of Google’s Platform as a Service (PaaS) in order to create a scalable, highly available and secure PHP web application. Note: The instructions are for Mac OSX, but may work for Linux too. Slight modifications may be needed for Windows.
Google Cloud Platform
To get started you need to have a Google account. Then download the cloud SDK:
curl https://sdk.cloud.google.com/ | bash
You may need to start a new terminal. Then authenticate yourself by typing:
gcloud auth login
You’ll be asked to allow some access. Allow it!
Finally install the App Engine PHP package by typing:
gcloud components update gae-python
Don’t be alarmed by the “python” suffix. This is the right package, which includes both PHP and Python.
This concludes the preliminary steps to get ready for general Google App Engine development with PHP.
Google App Engine
What is this Google App Engine and why would you want to use it as the deployment target for your PHP application? There are many good reasons:
• Very solid and mature deployment platform for web applications
• You get auto scaling if demand fluctuates
• Your infrastructure is managed 24/7 by Google according to industry best practices
• App Engine works with many IDEs so it’s easy to develop
• The price for small applications is right (free)
Google Cloud Storage
Note: If you want to use Google Cloud Storage you’ll need to start a free trial, which requires providing Google with billing information as verification (you will not be billed unless you explicitly upgrade).
Google provides several data storage options and Google cloud Storage is one of the simplest and most popular. Here are some reasons you may be interested to keep your data in Google cloud storage:
• Google stores your data for you
• Google management through console
• Good access control
• REST APIs
• The price up to 5GB is right (free)
Create a PHP Application on GAE
Let’s dive right in and create a project directory (e.g. ~/random_meme) and put these files in it:
-- -- start app.yaml -- -- - application: random-meme version: 1 runtime: php55 api_version: 1 handlers: - url: /.* script: main.php -- -- end app.yaml -- -- - -- - start main.php -- -- - <html> <head> <title>Random Meme</title> </head> <body> <?php $input = array( "http://d2ws0xxnnorfdo.cloudfront.net/meme/300.jpg", "http://d2ws0xxnnorfdo.cloudfront.net/meme/301.jpg", "http://d2ws0xxnnorfdo.cloudfront.net/meme/302.jpg", "http://d2ws0xxnnorfdo.cloudfront.net/meme/303.jpg", "http://d2ws0xxnnorfdo.cloudfront.net/meme/305.jpg", "http://d2ws0xxnnorfdo.cloudfront.net/meme/307.jpg"); $rand_key = array_rand($input, 1); $image = $input[$rand_key]; echo '<img src="' . $image . '">'; ?> </body> </html> -- - end main.php -- -- -
This is it! You just created a random meme web application. Congratulations!
Let’s upload it to the Google App Engine:
cd <parent dir of the randome-meme directory> appcfg.py -A random-meme update randome-meme/
Now you can browse to random-meme.appspot.com and view your random meme. Refresh to get a new one.
Upload the Memes to Google Cloud Storage
I created a bucket called random-meme-bucket
Now, I can use the Google console to upload the list of memes from the previous section as a simple text file. Each line will have one meme:
-- -- - start memes.txt -- -- -- http://d2ws0xxnnorfdo.cloudfront.net/meme/300.jpg http://d2ws0xxnnorfdo.cloudfront.net/meme/301.jpg http://d2ws0xxnnorfdo.cloudfront.net/meme/302.jpg http://d2ws0xxnnorfdo.cloudfront.net/meme/303.jpg http://d2ws0xxnnorfdo.cloudfront.net/meme/305.jpg http://d2ws0xxnnorfdo.cloudfront.net/meme/307.jpg -- -- - end memes.txt -- -- --
You can do that from Google Developer’s console:
Connect Your Application to Google Cloud Storage
Now, that we have our memes in the cloud we can fetch them from Google cloud storage instead of hard-coding them as an array inside our application.
Here is the code:
<html> <head> <title>Random Meme</title> </head> <body> <?php $memes = file_get_contents('gs://random-meme-bucket/memes.txt'); $input = preg_split("/[s,]+/", $memes); $rand_key = array_rand($input, 1); $image = $input[$rand_key]; echo '<img src="' . $image . '">'; ?> </body> </html>
file_get_contents()PHP function on Google App Engine supports Google Cloud Storage object URLs, so we fetch the contents of memes.txt as a string. Then, the next line parses it into an array of strings each one is a URL to a meme image. The rest if the same: Choose a random image and output it as an img tag into the page. Boom!
Don’t forget to update your app:
appcfg.py -A random-meme update randome-meme/
Or check out my version here: random-meme.appspot.com.