How to run your PHP Application on Google App Engine?

With the help of Google App Engine (GAE), you can deploy and host web applications in Google’s data centers, belonging to the platform as a service (PaaS) category of cloud computing.

In this article, we will tell you how you can:

  • Deploy your own PHP Application on Google App Engine
  • Debug the app
  • Configure the app
  • Troubleshoot the app

We will ask you to replicate code. However, you can hire PHP developers and deploy the app you want as well. Hiring PHP developers means you can host your company’s app on Google cloud. This ensures that your app:

  • Can be efficiently managed 
  • Has good loading time and performs well
  • Enjoy top-notch security

6 easy steps you need to follow 

1. Set up Google Cloud

Good news before you begin.

If you are new to Google Cloud, you can create an account and get $300 in free credits to run, test, and deploy workloads.

Here are the steps you need to follow.

  1. Initialize your App Engine app with your project and choose its region: gcloud app create –project=[YOUR_PROJECT_ID]
  2. Install the following prerequisites:
  3. Download and install Git
  4. Install PHP
  5. To manage dependencies, download Composer and make sure the Composer executable is installed globally.

2. Create a new project 

PHP Application

  1. Enable the Cloud Build API.
  2. Install and initialize the Google Cloud CLI.

3. Get the app’s code 

Google has created a simple Hello World app for App Engine so you can quickly get a feel for deploying an app to the Google Cloud. 

Get the app’s code

Go to github and clone the Hello World sample app repository to your local machine. Or you can download the sample as a zip file and extract it. 

You can also hire PHP developers and put your own app’s code on the platform.

hire PHP developers

4. Run the app

You can install the dependencies with composer install and start a local web server php -S localhost:8080 -t web/.

Enter: http://localhost:8080/

The ‘hello world’ message should be displayed on the page.

5. Deploy and run Hello World on App Engine

Now you can deploy your app to the App Engine flexible environment.

This is how:

  1. Run gcloud app deploy from the appengine/flexible/helloworld directory,
  2. You can view the app by launching it at use gcloud app browse

You can also deploy the app to the Google PHP Runtime Environment.

3. From the main menu, select Tools | Google App Engine for PHP | Upload App Engine PHP app.

4. Visit the application at http://<your-application-id>

6. Clean up 

This is an important step to avoid incurring charges.

  1. In the Google Cloud console, go to the Manage resources page.
  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Note – following these steps will delete everything in the project. Your custom project ID will also be lost. To preserve the URLs that use the project ID, such as an URL, delete selected resources inside the project instead of deleting the whole project.

How to debug the app?

To debug your app follow these steps:

  1. Create an App Engine for PHP run configuration as described above.
  2. Set the breakpoints in your code. Breakpoints are special markers that suspend program execution at a specific point. You can examine the program state and behavior with them.

How to debug the issues

  • Line breakpoints: suspend the program upon reaching the line of code where the breakpoint was set. This type of breakpoints can be set on any executable line of code.
  • Method breakpoints: suspend the program upon entering or exiting the specified method or one of its implementations, allowing you to check the method’s entry/exit conditions.
  • Exception breakpoints: suspend the program when Exception or its subclasses are thrown. They apply globally to the exception condition and do not require a particular source code reference.

3. Choose the configuration from the list and click the Debug button

4. In the Debug tool window that opens, step through the program, pause and resume the program, examine it when suspended, and so on.

How to create a run configuration of the type App Engine for PHP?

Here are the steps you need to follow.

  1. Select Run | Edit Configurations from the menu, click App general add and select App Engine for PHP from the list to open the configuration settings.
  2. Specify the host to run the development server and the application on (the default is localhost).
  3. In the Port field, specify the port through which PhpStorm will communicate with the development server (the default port is 8080).
  4. You can also hire PHP developers to specify the settings for running and debugging your application on the PHP development server in the command-line mode:
  • Interpreter options – Specify the options to be passed to the PHP executable file of the built-in PHP interpreter. For more details visit Command-Line Arguments.
  • Yami files – Specify the .yaml configuration files to use. This field is optional, use it when your application consists of several modules and therefore several .yaml configuration files are used.
  • Custom working directory – Specify the location of the files that are outside the folder with your sources and are referenced through relative paths. Type the path manually or click Browse the Browse button and select the desired folder in the dialog that opens.
  • Environment variables – Specify the environment variables be passed to the built-in server. Visit Environment Variables in Apache for details.

Common error messages and how to resolve them

In this section we will help you troubleshoot your app. 

  1. PERMISSION_DENIED: Operation not allowed. The “appengine.applications.create” permission is required.

This happens because your project does not include the required App Engine application. Switch to an account with the Owner role to create App Engine applications.

2. 502 Bad Gateway

The app.yaml is misconfigured. Check the app logs

3. An internal error occurred.

If you are deploying your service using a network configuration that uses a Shared VPC arrangement, this error may occur.

Make sure your App Engine flexible environment complies with all the specifications needed for this configuration.

Next, confirm that your project contains the specified service accounts required for this setup; if not, you will need to restore the accounts. 

Keep in mind that the Shared VPC host project’s subnet’s region must match the location where your App Engine environment was set up. 

After making sure your app.yaml configuration is correct, re-deploy your service using the Google Cloud SDK, adding the —verbosity=debug flag, and contact GCP Support by sending them the command’s output if the problem still continues.

4. IP space of {USER_SUBNETWORK_NAME} is exhausted and needs to be expanded.

This means that the network configured for the App Engine service doesn’t have addresses left to allocate, for the new instances of the service

Expand the VPC ranges on the subnet configured for your App Engine flexible environment service to fix the problem.