Creating a simple PHP application on Google App Engine!

kerion
5 min readSep 19, 2018

--

PHP was my first language when I started web development years ago. Just by installing XAMPP or MAMP, I could get started with writing my first Hello World page, and creating applications receiving inputs and storing them on a database.

Google Cloud Platform (GCP) has been increasingly popular over the last 2 years, and having attended the 1st birthday conference of the Google Data Center in Singapore some months ago, I was motivated to try my hands on GCP.

Hence I figured I would get started by trying to create a simple PHP application on GCP’s App Engine (GAE). Oh boy was it an experience.

Reading through the documentation and tutorials from GCP itself helped a fair bit, but I found the tutorial code examples a tad too complicated. In my opinion, a simple PHP application does not need to include additional PHP frameworks/libraries like Silex, Symfony and others. I prefer to keep it as vanilla as possible. Hence I started writing my own simple PHP script and tried deploying to GAE.

Looks kinda complicated!

The “simple” app

While a “Hello World” page may be the simplest objective to achieve, I thought an application that could connect to a database and receive file uploads would be my definition of a “simple” app, simple enough to demonstrate the common functionalities required of an application.

Part of the source code

The source code here may look simple, but getting there was quite a journey. Let me start by getting the GCP components I need.

Creating your components’ instances

There were 3 components I needed for this application:
a) Google App Engine to host my PHP script and to serve the landing page to the user,
b) Cloud Storage to store files uploaded by the user,
c) Cloud SQL to host my database.

I decided to do a series of screen captures to illustrate what I have done, so do scroll through and read the captions too, where I describe additional details for each screen.

1. I created a New Project on GCP.

The project name would translate into my site name, e.g. try-phpappeng.appspot.com .

2. After creating my project, I linked it up to my billing account.
3. I went to Google App Engine and created an instance.

After successfully creating an App Engine, I clicked on the button to download the Cloud SDK and install it into my computer. This SDK was needed to deploy my project codes into GCP.

I checked that there were default buckets (with my site url) created for me in the Cloud Storage.

The last component I needed to create was the Cloud SQL instance.

I selected MySQL, and selected the Second Generation.
I followed the steps to create my instance ID and root password. I would need these information in my codes later.
I proceeded to create a database. I named it “testdb”.

Lastly, I enabled the Cloud SQL Admin API so that my application can communicate with the database.

With the steps above, I have created instances of the components I needed. I would proceed with writing the PHP codes and configurations needed for deployment.

Codes and Configurations

The project directory structure was simple. Firstly, I stated the required libraries I needed to install via composer.

I also needed to create an app.yaml file for GCP deployments.

You can find the rest of my codes in my Github repo.

Deploying to GCP

With the codes ready, it was time for deployment. As I have installed the Cloud SDK in the earlier step, I just needed to open my terminal and perform a gcloud init.

I followed the steps until I completed the initialization.
After which, I gcloud app deploy
And waited for several minutes (about 15 minutes) until the deployment completed.

Voila! The app is running!

And with that, when I accessed the site url as indicated on the terminal, I was greeted with my simple application page. My application could connect with the Cloud SQL MySQL database, and could allow users to upload .txt files into the Cloud Storage.

Yay!

Ending thoughts

I definitely did not get all my codes and configurations right on my first try. I spent a couple of nights after work reading through GCP’s documentation, trying to traverse the many various pages to piece up what I needed.

One of the toughest nut to crack, was to connect to the Cloud SQL MySQL database. The documentation was using PDO as the database connector, and the database port configuration value did not work when I use MySQLi. Apparently, there were also a number of queries on Google Groups forum, that pointed out how others were facing the same issue as I did: https://groups.google.com/forum/#!topic/google-appengine/IZgep9t3LbI

I was quite exasperated, so I posted my own question on Stackoverflow too. (it has yet been properly answered.) https://stackoverflow.com/questions/51838713/mysqli-works-fine-on-local-machine-but-not-on-google-app-engine-whats-wrong

Adamant to find a solution, I took my chance by searching on Youtube, and found a video that was 2 years ago. And voila, I found the answer I have been searching for. https://youtu.be/QIWBxx0mE00 (the magic sauce starts after the 8:00 mark)

Hence, this is why I decided to document my steps down, so that fellow devs who wish to explore GAE can use this guide, and need not go through many rounds of googling to deploy their first PHP apps!

Hope this helps!

--

--

kerion

Freelance Web Designer/Developer. Always awed by clean UI / UX. ♥ Japan culture, Chage&Aska, 80s music, PSB, Eurodance.