A couple of years back if you needed to run PHP on Google App Engine you were required to use a open source tool like Quercus, a 100% Java implementation of PHP, to run your PHP applications on the App Engine. However, as you would have guessed, it was not easy to work as with using a native PHP implementation. Now that App Engine natively supports PHP and MySQL, you can easily write PHP applications.
Installing the Google App Engine SDK
Getting started with developing for the App Engine couldn’t be easier. All the required tools and libraries are included in the App Engine SDK and are available for Linux, Windows and Mac OS X. As we are focusing on PHP, you can download the corresponding SDK from the following URL.
Choose the appropriate SDK depending on your operating system. As I primarily use Windows, the examples will target for that particular platform. But as the App Engine is a cloud based service, only the installation instructions will be different for each platform.
The SDK requires that you install Python 2.7 first before installing the actual PHP SDK. This is required for running a local server and deployment of your PHP apps on the App Engine. If you already have Python installed than you need to only install the PHP SDK. (Note: if you have Python 3 or higher installed, you will still need to install Python 2.7). The SDK includes binaries for the PHP 5.4 runtime, including all enabled extensions, so there is no need to download PHP separately for developing with App Engine. Also as most PHP applications use MySQL, make sure that it is installed on your local machine. The PHP SDK includes a local development environment that lets you develop and test complete App Engine applications before deploying it to the Google App Engine servers.
Once the SDK is installed you will see a Google App Engine Launcher shortcut created on your desktop, which you can use to create and launch new applications on your local server. You can also work using the command line but if you are new to the App Engine development the App Launcher makes it easier to start with.
Developing a PHP application
We will first create a simple ‘Hello World’ application to check that our App Engine works as intended. Primarily there are 3 steps to developing a Google App Engine application:
1. Write an application.
2. Test your application on your local server using the App Engine SDK.
3. Deploy the working application to the Google App Engine servers.
Creating a simplest application requires you to create 2 files in directory: a configuration file called app.yaml and a PHP script file that handles the web requests. You can use the Google App Launcher to create a new project by selecting the File Menu, New Application… This will create two files in your project directory as mentioned above.
By default, the app.yaml will contain the following configuration code.
application: helloworld version: 1 runtime: php api_version: 1 handlers: - url: /.* script: helloworld.php
Taking from the top, this configuration file says the following about our first application:
• The application identifier is helloworld. Every new application on App Engine has a unique application identifier. You’ll choose the identifier for your application when you register it in the next step. Until then you can just leave the value here set to helloworld because this value is not important when developing locally. However when uploading to the Google App Engine servers, this must be globally unique.
• This is version number 1 of this application’s code. If you adjust this before uploading new versions of your application software, App Engine will retain previous versions, and let you roll back to a previous version using the administrative console.
• This code runs in the php runtime environment, version “1”. Additional runtime environments and languages may be supported in the future.
• Every request to a URL whose path matches the regular expression /.* (all URLs here) should be handled by the helloworld.php script.
Of course, this is just the tip of the yaml configuration, more options can be found in the documentation.
There will also be a ‘helloworld.php’ script file. This is the handler script which will handle all the web requests from the client. This, by default, will have the following code.
That’s it; this is all that is required to launch a minimal Google App Engine application. Now that we have created a simple application, the next step is to test it locally. In the Google App Engine Launcher, set up the application by selecting the File menu, Add Existing Application..., and then select the helloworld directory. Select the application in the app list, click the Run button to start the application, and then click the Browse button to view it. Clicking Browse simply loads (or reloads) http://localhost:8080/ in your default web browser. This will now display the 'Hello World!' greeting in the browser.
Now comes the important part – deploying you app on the Google App Engine servers. For that first you will need to register your application. You create and manage App Engine web applications from the App Engine Administration Console. Sign in to App Engine using your Google account at the following URL:https://appengine.google.com/
To create a new application, click the "Create an Application" button. Follow the onscreen instructions to register an application ID, which is a name unique to this application. If you choose to use the free appspot.com domain name, the full URL for the application will be http://your_app_id.appspot.com/. So for example my app id is codediesel than the full urls will be the following.http://codediesel.appspot.com
Note that you can also purchase a top-level domain name for your app, or use one that you have already registered to serve your application. So, instead of the appspot.com domain, you can use any other domain you have.
Once you have finalized the app id, edit your app.yaml file and change the value of the application: id from 'helloworld' to the new one.
You can now select the app in the App Launcher and upload it to the Google servers by clicking on the Deploy button. Once it is completed, you can now watch your application run on App Engine. The URL for your website begins with your application ID:http://your_app_id.appspot.com
You have just created your first Google App Engine application and deployed it to the cloud.
Advantages of Using Google App Engine
The primary reason most people will opt for App Engine. This is one of the important factors for the success of any web application. Google App Engine has an automatic scaling feature that requires you to merely write your application code and Google takes care of the rest. As your application garners more users, App Engine automatically allocates more resources for your application and manages the use of these resources. Your application itself does not need to know and handle the resources it is using.
Unlike traditional hosting, you only pay for the resources you use (CPU, storage, bandwidth etc). So you can start with the lowest denominator required to run your application and scale up as required as your application grows. This can be specially useful when you are building a SAS application.
Google has perhaps one of the most secured infrastructures across the world and your application code and data are stored on highly secured servers that prevent any kind of unauthorized access (in theory). Of course this does not mean that you can get away with designing buggy and insecure software and hoping that App Engine will somehow take care of it. Here we are talking about infrastructure security.
Reliability and Performance
When we talk about reliability and performance of the web applications we need to keep in mind the unmatched reputation of Google as one of the leading brand names in the world cutting across Industries. Google App Engine offers you the same performance and reliability as any other product of Google.
Disadvantages of Using Google App Engine
Almost everything good has its drawbacks. Google App Engine is no different. It is always better to be aware the flaws before you host your application on App Engine.
The first and most important issue is vendor lock-in. App Engine normally requires you to spend significant efforts to develop and optimize specifically for the platform to run your applications nicely and cost effectively. If you have been developing on the usual hosting platforms the migrating to Google App Engine will require some new learning.
Using WordPress on Google App Engine
Google App Engine can also host WordPress sites, and there is a nice tutorial that explains on doing that. However at present it can be difficult to get everything working correctly the first time. Also, there are some reports that the latest versions of WordPress do not work with the App Engine.
Google has also released a plugin that adds overrides to the core functionality in WordPress to use App Engine infrastructure, such as the Mail functionality, and uploading media to Google Cloud Storage.
A web application without a storage facility is practically worthless. App Engine provides two options for data storage:
Google Cloud SQL: A relational SQL database based on the MySQL database.
Google Cloud Storage: A storage service for objects and files, and accessible to App Engine apps via the Google Cloud Storage client library.
Google Cloud SQL is a MySQL database that lives in Google's cloud, and has all the capabilities and functionality of the standard MySQL distribution, with a few additional features and a few unsupported features. Google Cloud SQL is easy to use, doesn't require any software installation or maintenance and is ideal for small to medium-sized applications. Note that Cloud SQL is not free and you have to enable billing in your App Engine console to start using it. This also means that for giving WordPress a trial run on App Engine you will have to spend some extra cash.
Sessions in App Engine
By default the App Engine runtime uses memcache to store session information using the
MemcacheSessionHandlerclass. You can however override this default by specifying your own session handler using PHP's session_set_save_handler() method. Memcache allows session data to be saved and retrieved quickly, meaning the overhead on your request is minimal. However data in App Engine memcache may be flushed periodically, meaning any session information will be lost. For longer-lived sessions, it may be preferable to use an alternative storage service such as Cloud SQL.
There are many other changes you need to be aware when developing PHP applications on Google App Engine which we will cover in another post.