This tutorial shows how to deploy a small WordPress site to the App Engine
flexible environment.

## Objectives

- Create a Cloud SQL Second Generation instance.
- Configure a sample WordPress site.
- Deploy the sample WordPress site to the App Engine flexible environment.

## Costs


> [!IMPORTANT]
> **Important:** In this document, you use the following billable components of Google Cloud:
>
> - [App Engine](https://docs.cloud.google.com/appengine/pricing)
> - [Cloud SQL](https://docs.cloud.google.com/sql/pricing)
>
> To generate a cost estimate based on your projected usage, use the [pricing calculator](https://docs.cloud.google.com/products/calculator).

## Before you begin

1. Create credentials by following these steps:
   - In the Google Cloud console, go to the **Credentials** page.

     [Open the API Credentials Page](https://console.cloud.google.com/apis/credentials/)
   - Click **Create credentials** and select **Service account key**.
   - Select **Service account \> App Engine default service account**.
   - Click **Create**.
   - Save the downloaded key in a secure place.
2. Install [PHP](http://php.net/manual/en/install.php) and [Composer](https://getcomposer.org).
3. [Download the Cloud SQL Proxy](https://docs.cloud.google.com/sql/docs/mysql-connect-proxy#install) and make it executable. Also add the location of the Cloud SQL Proxy executable file to your `PATH` environment variable.
4. Install a [MySQL client](https://www.mysql.com) and verify that the location of the `mysql` executable file is in your `PATH` environment variable.

## Creating and configuring a Cloud SQL Second Generation instance

1. Create a Cloud SQL Second Generation instance:

   ```php
   gcloud sql instances create tutorial-sql-instance \
       --activation-policy=ALWAYS \
       --tier=db-n1-standard-1 \
       --region=us-central1
   ```
2. Set the root password for your instance:

   ```php
   gcloud sql users set-password root --instance tutorial-sql-instance \
       --password [YOUR_SQL_ROOT_PASSWORD] \
       --host %
   ```

   where `[YOUR_SQL_ROOT_PASSWORD]` is a secure password of your choice.
3. Download and run the [Cloud SQL Proxy](https://docs.cloud.google.com/sql/docs/mysql/sql-proxy.md.txt#install):

   ```bash
   ./cloud-sql-proxy \
       -dir /tmp/cloudsql \
       -instances=[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance \
       -credential_file=[PATH_TO_YOUR_SERVICE_ACCOUNT_JSON]
   ```

   where
   - `[YOUR_PROJECT_ID]` is your Google Cloud project ID.

   - `[PATH_TO_YOUR_SERVICE_ACCOUNT_JSON]` is the path to the service
     account JSON file that you downloaded previously.

   The following output indicates that the proxy is ready for new connections:

   ```php
   Listening on 127.0.0.1:3306 for [YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance
   Ready for new connections
   ```
4. In another terminal window, create a new database and a user:

   ```bash
   mysql -h 127.0.0.1 -u root --password=[YOUR_SQL_ROOT_PASSWORD]
   mysql> create database tutorialdb;
   mysql> create user 'tutorial-user'@'%' identified by '[YOUR_DATABASE_PASSWORD]';
   mysql> grant all on tutorialdb.* to 'tutorial-user'@'%';
   mysql> exit
   ```

   where:
   - `[YOUR_SQL_ROOT_PASSWORD]` is the root password for your Cloud SQL instance.
   - `[YOUR_DATABASE_PASSWORD]` is a secure password of your choice.

## Setting up the WordPress project

1. Clone the sample repository:

   ```php
   git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
   ```
2. Go to the directory that contains the sample code:

   ```php
   cd php-docs-samples/appengine/flexible/wordpress
   ```
3. Install dependencies:

   ```php
   composer install
   ```
4. Run the helper script:

   ```bash
   php wordpress.php setup -n \
       --dir=./wordpress-project \
       --db_instance=tutorial-sql-instance \
       --db_name=tutorialdb \
       --db_user=tutorial-user \
       --project_id=[YOUR_PROJECT_ID] \
       --db_password=[YOUR_DATABASE_PASSWORD]
   ```

   where:
   - `[YOUR_PROJECT_ID]` is your project ID.
   - `[YOUR_DATABASE_PASSWORD]` is your database password.

   The `-dir` parameter specifies the location of your
   WordPress project.
5. The helper script writes information to
   `wordpress-project/wordpress/wp-config.php`. Inspect the content of
   `wp-config.php` to verify that your names, project ID, and database password
   are correct.

   ```bash
   if ($onGae) {
       /** Production environment */
       define('DB_HOST', ':/cloudsql/[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance');
       /** The name of the database for WordPress */
       define('DB_NAME', 'tutorialdb');
       /** MySQL database username */
       define('DB_USER', 'tutorial-user');
       /** MySQL database password */
       define('DB_PASSWORD', '[YOUR_DATABASE_PASSWORD]');
   } else {
       /** Local environment */
       define('DB_HOST', '127.0.0.1');
       /** The name of the database for WordPress */
       define('DB_NAME', 'tutorialdb');
       /** MySQL database username */
       define('DB_USER', 'tutorial-user');
       /** MySQL database password */
       define('DB_PASSWORD', '[YOUR_DATABASE_PASSWORD]');
   }
   ```

## Deploying the WordPress project to the App Engine flexible environment

1. Go to your WordPress project directory:

   ```php
   cd wordpress-project
   ```
2. Deploy the WordPress project:

   ```php
   gcloud app deploy \
       --promote --stop-previous-version app.yaml cron.yaml
   ```
3. In your browser, enter the following URL:

   `https://PROJECT_ID.REGION_ID.r.appspot.com`

   Replace the following:
   - `PROJECT_ID`: Your Google Cloud project ID
   - `REGION_ID`: A [code that App Engine assigns
     to your app](https://docs.cloud.google.com/appengine/docs/flexible/php/how-requests-are-routed#region-id)

## Updating WordPress, plugins, and themes

It's important to keep WordPress, plugins, and themes up to date. You can keep
these items updated by using the `wp` tool. After an update, you need to re-
deploy the WordPress project.

1. Update WordPress itself:

   ```php
   vendor/bin/wp core update --path=wordpress
   ```
2. Update plugins:

   ```php
   vendor/bin/wp plugin update --all --path=wordpress
   # Just in case it updates any of the dropins, copy the files:
   cp wordpress/wp-content/plugins/batcache/advanced-cache.php \
       wordpress/wp-content/plugins/memcached/object-cache.php \
       wordpress/wp-content
   ```
3. Update themes:

   ```php
   vendor/bin/wp theme update --all --path=wordpress
   ```
4. Deploy the project again:

   ```php
   gcloud app deploy \
       --promote --stop-previous-version app.yaml cron.yaml
   ```

## Clean up


To avoid incurring charges to your Google Cloud account for the resources used in this
tutorial, either delete the project that contains the resources, or keep the project and
delete the individual resources.

### Delete the project


The easiest way to eliminate billing is to delete the project that you
created for the tutorial.

To delete the project:

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

1. In the Google Cloud console, go to the **Manage resources** page.

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
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.

<br />

<br />

### Delete non-default versions of your app

If you don't want to delete your project, you can reduce costs by deleting the
non-default versions of your app.


To delete an app version:

1. In the Google Cloud console, go to the **Versions** page for App Engine.

   [Go to Versions](https://console.cloud.google.com/appengine/versions)
2. Select the checkbox for the non-default app version that you want to delete.

   > [!NOTE]
   > **Note:** The only way you can delete the default version of your App Engine app is by deleting your project. However, you can [stop the default version in the
   > Google Cloud console](https://console.cloud.google.com/appengine/versions). This action shuts down all instances associated with the version. You can restart these instances later if needed.
   >
   > <br />
   >
   > In the App Engine standard environment, you can stop the default version only if your app has
   > manual or basic scaling.
   >
3. To delete the app version, click **Delete**.

<br />

<br />

### Delete your Cloud SQL instance


To delete a Cloud SQL instance:

1. In the Google Cloud console, go to the **Instances** page.

   [Go to Instances](https://console.cloud.google.com/sql/instances)
2. Click the name of the SQL instance you that want to delete.
3. To delete the instance, click **Delete**, and then follow the instructions.

<br />

<br />

## What's next

- Learn how to
  [run the PHP Bookshelf sample app in the App Engine flexible environment](https://docs.cloud.google.com/php/getting-started/tutorial-app).

- Learn how to
  [run the PHP Bookshelf sample app on Google Kubernetes Engine](https://docs.cloud.google.com/php/tutorials/bookshelf-on-kubernetes-engine).

- Learn how to
  [run the PHP Bookshelf sample app on Compute Engine](https://docs.cloud.google.com/php/tutorials/bookshelf-on-compute-engine).

- Learn about
  [WordPress](https://wordpress.org/).

-

  Explore reference architectures, diagrams, and best practices about Google Cloud.
  Take a look at our
  [Cloud Architecture Center](https://docs.cloud.google.com/architecture).