|
| 1 | +# Deploy WordPress to App Engine for PHP 7.2 |
| 2 | + |
| 3 | +This is a small command line tool for downloading and configuring |
| 4 | +WordPress on App Engine for PHP 7.2. The script allows you to create a |
| 5 | +working WordPress project for the |
| 6 | +[App Engine standard environment][appengine-standard]. For deploying |
| 7 | +WordPress to the [App Engine flexible environment][appengine-flexible], |
| 8 | +refer to the example at [appengine/standard/wordpress][../../flexible/wordpress] |
| 9 | + |
| 10 | +## Common Prerequisites |
| 11 | + |
| 12 | +* Install [Composer][composer] |
| 13 | +* Create a new Cloud Project using the [Cloud Console][cloud-console] |
| 14 | +* Enable Billing on that project |
| 15 | +* [Enable Cloud SQL API][cloud-sql-api-enable] |
| 16 | +* Install [Google Cloud SDK][gcloud-sdk] |
| 17 | +* Install the [mysql-client][mysql-client] command line tool |
| 18 | + |
| 19 | +## Project preparation |
| 20 | + |
| 21 | +Configure Google Cloud SDK with your account and the appropriate project ID: |
| 22 | + |
| 23 | +``` |
| 24 | +$ gcloud init |
| 25 | +``` |
| 26 | + |
| 27 | +Create an App Engine application within your new project: |
| 28 | + |
| 29 | +``` |
| 30 | +$ gcloud app create |
| 31 | +``` |
| 32 | + |
| 33 | +Then configure the App Engine default GCS bucket for later use. The default App |
| 34 | +Engine bucket is named YOUR_PROJECT_ID.appspot.com. Change the default Access |
| 35 | +Control List (ACL) of that bucket as follows: |
| 36 | + |
| 37 | +``` |
| 38 | +$ gsutil defacl ch -u AllUsers:R gs://YOUR_PROJECT_ID.appspot.com |
| 39 | +``` |
| 40 | + |
| 41 | +### Create and configure a Cloud SQL for MySQL 2nd generation instance |
| 42 | + |
| 43 | +Note: In this guide, we use `wordpress` for the instance name and the database |
| 44 | +name. We use `root` for the database user name. |
| 45 | + |
| 46 | +Create a new Cloud SQL for MySQL Second Generation instance with the following |
| 47 | +command: |
| 48 | + |
| 49 | +```sh |
| 50 | +$ gcloud sql instances create wordpress \ |
| 51 | + --activation-policy=ALWAYS \ |
| 52 | + --tier=db-n1-standard-1 |
| 53 | +``` |
| 54 | + |
| 55 | +Note: you can choose `db-f1-micro` or `db-g1-small` instead of |
| 56 | +`db-n1-standard-1` for the Cloud SQL machine type, especially for the |
| 57 | +development or testing purpose. However, those machine types are not |
| 58 | +recommended for production use and are not eligible for Cloud SQL SLA |
| 59 | +coverage. See our [Cloud SQL SLA](https://cloud.google.com/sql/sla) |
| 60 | +for more details. |
| 61 | + |
| 62 | +Then change the root password for your instance: |
| 63 | + |
| 64 | +```sh |
| 65 | +$ gcloud sql users set-password root \ |
| 66 | + --host=% \ |
| 67 | + --instance wordpress \ |
| 68 | + --password=YOUR_INSTANCE_ROOT_PASSWORD # Don't use this password! |
| 69 | +``` |
| 70 | + |
| 71 | +You will also need to create the database you want your WordPress site to use: |
| 72 | + |
| 73 | +```sh |
| 74 | +$ gcloud sql databases create wordpress --instance wordpress |
| 75 | +``` |
| 76 | + |
| 77 | +## SetUp |
| 78 | + |
| 79 | +First install the dependencies in this directory as follows: |
| 80 | + |
| 81 | +```sh |
| 82 | +$ composer install |
| 83 | +``` |
| 84 | + |
| 85 | +If it complains about extensions, please install `phar` and `zip` PHP |
| 86 | +extensions and retry. |
| 87 | + |
| 88 | +### Create a new WordPress Project |
| 89 | + |
| 90 | +To download WordPress and set it up for Google Cloud, run the `create` command: |
| 91 | + |
| 92 | +```sh |
| 93 | +$ php wordpress.php create |
| 94 | +``` |
| 95 | + |
| 96 | +The command asks you several questions, please answer them. Then you'll have a |
| 97 | +new WordPress project. By default it will create `my-wordpress-project` in the |
| 98 | +current directory. |
| 99 | + |
| 100 | +### Update an existing WordPress Project |
| 101 | + |
| 102 | +If you are migrating an existing project to Google Cloud, you can use the |
| 103 | +`update` command: |
| 104 | + |
| 105 | +```sh |
| 106 | +$ php wordpress.php update path/to/your-wordpress-site |
| 107 | +``` |
| 108 | + |
| 109 | +The command asks you several questions, please answer them. This will copy the |
| 110 | +files in the [`files`](files/) directory and write the proper configuration. |
| 111 | +Then your WordPress project will be ready to deploy to Google Cloud! |
| 112 | + |
| 113 | +## Deploy to Google Cloud |
| 114 | + |
| 115 | +CD into your WordPress project directory and run the following command to |
| 116 | +deploy: |
| 117 | + |
| 118 | +```sh |
| 119 | +$ gcloud app deploy \ |
| 120 | + --promote --stop-previous-version app.yaml cron.yaml |
| 121 | +``` |
| 122 | + |
| 123 | +Then access your site, and continue the installation step. The URL is: |
| 124 | +https://PROJECT_ID.appspot.com/ |
| 125 | + |
| 126 | +Go to the Dashboard at https://PROJECT_ID.appspot.com/wp-admin. On the Plugins page, activate the following plugins: |
| 127 | + |
| 128 | + - Google App Engine for WordPress (also set the e-mail address in its |
| 129 | + settings page) |
| 130 | + |
| 131 | +After activating the plugins, try uploading a media object in a new post |
| 132 | +and confirm the image is uploaded to the GCS bucket by visiting the |
| 133 | +[Google Cloud console's Storage page][cloud-storage-console]. |
| 134 | + |
| 135 | +## Local Development |
| 136 | + |
| 137 | +To access this MySQL instance, use Cloud SQL Proxy. [Download][cloud-sql-proxy-download] |
| 138 | +it to your local computer and make it executable. |
| 139 | + |
| 140 | +Go to the [the Credentials section][credentials-section] of your project in the |
| 141 | +Console. Click 'Create credentials' and then click 'Service account key.' For |
| 142 | +the Service account, select 'App Engine app default service account.' Then |
| 143 | +click 'Create' to create and download the JSON service account key to your |
| 144 | +local machine. Save it to a safe place. |
| 145 | + |
| 146 | +Run the proxy by the following command: |
| 147 | + |
| 148 | +```sh |
| 149 | +$ cloud_sql_proxy \ |
| 150 | + -dir /cloudsql \ |
| 151 | + -instances=YOUR_PROJECT_ID:us-central1:wordpress \ |
| 152 | + -credential_file=/path/to/YOUR_SERVICE_ACCOUNT_JSON_FILE.json |
| 153 | +``` |
| 154 | + |
| 155 | +Now you can access the Cloud SQL instance with the MySQL client in a separate |
| 156 | +command line tab. |
| 157 | + |
| 158 | +``` |
| 159 | +$ mysql --socket /cloudsql/YOUR_PROJECT_ID:us-central1:wordpress -u root -p |
| 160 | +mysql> use database wordpress; |
| 161 | +mysql> show tables; |
| 162 | +mysql> exit |
| 163 | +``` |
| 164 | + |
| 165 | +## Various workflows |
| 166 | + |
| 167 | +### Install/Update Wordpress, plugins, and themes |
| 168 | + |
| 169 | +Because the wp-content directory on the server is read-only, you have |
| 170 | +to do this locally. Run WordPress locally and update plugins/themes in |
| 171 | +the local Dashboard, then deploy, then activate them in the production |
| 172 | +Dashboard. You can also use the `wp-cli` utility as follows (be sure to keep |
| 173 | +the cloud SQL proxy running): |
| 174 | + |
| 175 | +``` |
| 176 | +# Install the wp-cli utility |
| 177 | +$ composer require wp-cli/wp-cli |
| 178 | +# Now you can run the "wp" command to update Wordpress itself |
| 179 | +$ vendor/bin/wp core update --path=wordpress |
| 180 | +# You can also update all the plugins and themes |
| 181 | +$ vendor/bin/wp plugin update --all |
| 182 | +$ vendor/bin/wp theme update --all |
| 183 | +``` |
| 184 | + |
| 185 | +If you get the following error: |
| 186 | + |
| 187 | +``` |
| 188 | +Failed opening required 'google/appengine/api/urlfetch_service_pb.php' |
| 189 | +``` |
| 190 | + |
| 191 | +You can set a `WP_CLI_PHP_ARGS` environment variable to add |
| 192 | +`include_path` PHP configuration for wp-cli. |
| 193 | + |
| 194 | +``` |
| 195 | +$ export WP_CLI_PHP_ARGS='-d include_path=vendor/google/appengine-php-sdk' |
| 196 | +``` |
| 197 | + |
| 198 | +Then try the above commands again. |
| 199 | + |
| 200 | +### Remove plugins/themes |
| 201 | + |
| 202 | +First Deactivate them in the production Dashboard, then remove them |
| 203 | +completely locally. The next deployment will remove those files from |
| 204 | +the production environment. |
| 205 | + |
| 206 | +### Update the base image |
| 207 | + |
| 208 | +We sometimes release a security update for |
| 209 | +[the php-docker image][php-docker]. You have to re-deploy your |
| 210 | +WordPress instance to get the security update. |
| 211 | + |
| 212 | +Enjoy your WordPress installation! |
| 213 | + |
| 214 | +[appengine-standard]: https://cloud.google.com/appengine/docs/about-the-standard-environment |
| 215 | +[appengine-flexible]: https://cloud.google.com/appengine/docs/flexible/ |
| 216 | +[sql-settings]: https://console.cloud.google.com/sql/instances |
| 217 | +[mysql-client]: https://dev.mysql.com/doc/refman/5.7/en/mysql.html |
| 218 | +[composer]: https://getcomposer.org/ |
| 219 | +[cloud-console]: https://console.cloud.google.com/ |
| 220 | +[cloud-storage-console]: https://www.console.cloud.google.com/storage |
| 221 | +[cloud-sql-api-enable]: https://console.cloud.google.com/flows/enableapi?apiid=sqladmin |
| 222 | +[app-engine-setting]: https://console.cloud.google.com/appengine/settings |
| 223 | +[gcloud-sdk]: https://cloud.google.com/sdk/ |
| 224 | +[cloud-sql-proxy-download]: https://cloud.google.com/sql/docs/mysql/connect-external-app#install |
| 225 | +[credentials-section]: https://console.cloud.google.com/apis/credentials/ |
| 226 | +[php-docker]: https://github.com/googlecloudplatform/php-docker |
0 commit comments