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