It's easy to get started developing PHP apps that run on Google Cloud Platform. And because the apps you create will run on the same infrastructure that powers all of Google's products, you can be confident that they will scale to serve all of your users, whether there are a few or millions of them. This tutorial gets you going fast by deploying a simple Hello World app.
This tutorial assumes that you are familiar with PHP and that you have PHP 5.6, 7.0, or 7.1 installed. You also need to download Composer. This tutorial assumes the Composer executable is installed globally. This is part of the Getting Started with PHP on Google Cloud Platform series . After you complete the prerequisites listed under Before you begin, the tutorial takes about 10 minutes to complete.Before you begin
Before running this sample, take the following steps:
-
Use the GCP Console to create a new GCP project,
create an App Engine application, and enable billing:
Go
to App Engine
When prompted, select the region where you want your App Engine application located and then enable billing.
- Install the following prerequisites locally:
- Download and install git.
- Download and install Composer. Ensure the Composer executable is installed globally.
-
Download, install, and initialize the Google Cloud SDK:
Download the SDK
- Alternatively: You can use Google Cloud Shell, which comes with these prerequisites already installed.
Running PHP apps in the Google App Engine flexible environment
The App Engine flexible environment allow you to easily deploy and run standard PHP web apps. The flexible environment provides the scaling, monitoring, and load balancing infrastructure, so you can focus on building your app.
To run a PHP app in the flexible environment, you need:
- An
app.yaml
file that describes your app's runtime configuration
Download and run the app
A simple Hello World sample app written in PHP is available to help you quickly get a feel for deploying an app to Cloud Platform. After you've completed the prerequisites, you can download and deploy the Hello World sample app. This section guides you through getting the code and running the app locally.
Clone the Hello World app
Copy the Hello World sample app to your local machine:
git clone https://github.com/GoogleCloudPlatform/getting-started-php.git
Go to the directory that contains the sample code:
cd getting-started-php/1-hello-world
Alternatively, you can download the sample as a zip and extract it.
Run the app on your local computer
To run the Hello World app on your local computer:
Install dependencies:
composer install
Start a local web server.
php -S localhost:8000 -t web
In your web browser, enter this address:
http://localhost:8000
You can see the Hello World message from the sample app displayed in the page. This page is delivered by a web server running on your computer.
When you are ready to move forward, press Ctrl+C to terminate the local web server process.
Hello World code review
The index.php
file contains the PHP code to start a server and respond to
requests. This example uses
Silex,
but you can use other
PHP web frameworks.
This code creates a new Silex Application object and then defines
two GET
routes:
Running Hello World on Cloud Platform
This section guides you through deploying the Hello World app to the App Engine flexible environment. This diagram shows the process of deploying and running the app on Cloud Platform.
The App Engine flexible environment runs containers to provision Compute Engine instances. The number of instances scales in response to traffic. To learn more, see App Engine flexible environment. For information about alternatives to the flexible environment, see About the Google Cloud Platform Services.
Deploy the app to Cloud Platform
Enter this command to deploy the sample:
gcloud app deploy
See the app run in the cloud
In your web browser, enter this address:
https://<your-project-id>.appspot.com
This time, the page that displays the Hello World message is delivered by a web server running on Google Cloud Platform.
If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources. For information about deleting or stopping your VM instances, see Cleaning up.
Congratulations! You've deployed your first PHP app to Google Cloud Platform!
The next section discusses the configuration necessary for the app to run on Google Cloud Platform.
Configuring the App Engine flexible environment
As mentioned in the introduction, an app running in the flexible environment requires these files:
app.yaml
Dockerfile
This section explores each requirement.
App configuration
The app.yaml
file describes the app's deployment configuration.
This app.yaml
file specifies that the app uses a custom runtime and the
flexible environment. For details about
configuration settings for the flexible environment, see
App Engine flexible environment.
Docker
The Dockerfile
describes the runtime environment used by the app. Because this
app runs in Docker, it can be run without changes on any Docker host, such as
Google Kubernetes Engine
or
Kubernetes.
Cleaning up
If you're done with the tutorials and want to clean up resources that you've allocated, see Cleaning Up.