Quickstart for PHP App Engine Standard Environment

This quickstart shows you how to create a small App Engine application that displays a short message.

Before you begin

Before running and deploying this quickstart, you must install the Cloud SDK and create or use an existing Cloud Platform Console project:

  1. Create a new Cloud Platform Console project or retrieve the project ID of an existing project from the Google Cloud Platform Console:

    Go to the Projects page

    Tip: Retrieve a list of your existing project IDs with gcloud.

  2. To install the gcloud tool, you install and then initialize the Google Cloud SDK:

    Download the SDK

Listing your Cloud Platform Console project IDs with gcloud

From the command line, run:

gcloud projects list

Download the Hello World app

We've created a simple Hello World app for PHP so you can quickly get a feel for deploying an app to Google Cloud Platform. Follow these steps to download Hello World to your local machine.

  1. Clone the Hello World sample app repository to your local machine in a directory called helloworld:

    git clone -b phase0-helloworld https://github.com/GoogleCloudPlatform/appengine-php-guestbook.git helloworld
    
  2. Then go to the directory that contains the sample code:

    cd helloworld
    

Alternatively, you can download the sample as a zip file and extract it into a directory called helloworld.

Test the application

Test the application using the local development server (dev_appserver.py), which is included with the SDK.

  1. From within the helloworld directory, start the local development server with the following command:

    dev_appserver.py .

    The local development server is now running and listening for requests on port 8080.

  2. Visit http://localhost:8080/ in your web browser to view the app.

For more information about running the local development server, see the PHP Development Server reference.

Running the local development server (dev_appserver.py)

To run the local development server, you can either run dev_appserver.py by specifying the full directory path or you can add dev_appserver.py to your PATH environment variable:

  • If you installed the App Engine SDK, the tool is located at [PATH_TO_APP_ENGINE_SDK]/dev_appserver.py.

  • If you installed the Google Cloud SDK, the tool is located at [PATH_TO_CLOUD_SDK]/google-cloud-sdk/bin/dev_appserver.py.

    Tip: To add the Google Cloud SDK tools to your PATH environment variable and enable command-completion in your shell, you can run:

    [PATH_TO_CLOUD_SDK]/google-cloud-sdk/install.sh
    

Make a change

You can leave the development server running while you develop your application. The development server watches for changes in your project files, and reloads them if necessary.

  1. Try it now: Leave the development server running, then edit helloworld.php to change Hello, World! to something else.
  2. Reload http://localhost:8080/.

Deploy your app

To deploy your app to App Engine, run the following command from within the root directory of your application where the app.yaml file is located:

gcloud app deploy

Optional flags:

  • Include the --project flag to specify an alternate Cloud Platform Console project ID to what you initialized as the default in the gcloud tool. Example: --project [YOUR_PROJECT_ID]

  • Include the -v flag to specify a version ID, otherwise one is generated for you. Example: -v [YOUR_VERSION_ID]

To learn more about deploying your app from the command line, see Deploying a PHP App.

View your application

To launch your browser and view the app at http://[YOUR_PROJECT_ID].appspot.com, run the following command:

gcloud app browse

Congratulations!

You have completed this quickstart.

See the following sections for information about cleaning up after this quickstart as well as links to the possible next steps you can take with your deployed applications.

To learn more about this Hello World app, see the Hello World code review section.

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Go to the Cloud Platform Console.
  2. Click the trash can icon next to the project you want to shut down: this schedules the project for deletion.

What's next

Use a custom domain

You can serve your App Engine app using your own custom domain instead of appspot.com. For more information, see Using Custom Domains and SSL.

Hello World code review

Hello World is the simplest possible App Engine app: it contains only one service, has only one version, and all of the code is located within the app's root directory. This section describes each of the app files in detail.

helloworld.php

The helloworld.php script responds to any HTTP request with the message Hello, World!.

<?php

echo 'Hello, World!';

app.yaml

The app.yaml configuration file describes which handler scripts should be used for which URLs.

runtime: php55
api_version: 1

handlers:
- url: /.*
  script: helloworld.php

From top to bottom, this configuration file says the following about this application:

  • This code runs in the php55 runtime environment, API version 1.
  • This application is threadsafe so the same instance can handle several simultaneous requests. Threadsafe is an advanced feature and can result in erratic behavior if your application is not specifically designed to be threadsafe.
  • Every request to a URL whose path matches the regular expression /.* (all URLs) should be handled by the helloworld.php script.

The syntax of this file is YAML. For a complete list of configuration options, see the app.yaml reference.

Troubleshooting

The following is a common error message that you might encounter:

The development server must be started with the --php_executable_path flag set to the path of the php-cgi binary.

If you encounter this error when running dev_appserver.py, you need to rerun dev_appserver.py with the flag php_executable_path and specify the path to your php-cgi binary. You can find the path by running the following command:

  which php

Example of the dev_appserver.py command with the --php_executable_path flag set to the /usr/bin/php-cgi path:

    dev_appserver.py helloworld/ --php_executable_path /usr/bin/php-cgi

Learn more

Learn the whole platform

Now that you know what it's like to develop and deploy App Engine apps, you can stretch out and see the rest of Google Cloud Platform. For a guided walkthrough that teaches you how to create an application that uses the entire platform, not just App Engine, check out our Creating a Guestbook tutorial in which you expand this simple application to become a fully-fledged Guestbook application that lets authenticated Google accounts post messages to a public page.

Learn more about App Engine standard environment

Here are some topics to help you to continue learning about App Engine.

Send feedback about...

App Engine standard environment for PHP