|
| 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. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +* Install [Composer][composer] |
| 9 | +* Create a new Cloud Project on [Developers Console][dev-console] |
| 10 | +* Enable Billing on that project |
| 11 | +* [Enable Cloud SQL API][cloud-sql-api-enable] |
| 12 | +* Create App Engine default bucket at [App Engine Setting Page][app-engine-setting] |
| 13 | +* Install [Google Cloud SDK][gcloud-sdk] |
| 14 | + |
| 15 | +## Project preparation |
| 16 | + |
| 17 | +Configure Google Cloud SDK with your account and the Project. |
| 18 | + |
| 19 | +``` |
| 20 | +$ gcloud auth login |
| 21 | +... |
| 22 | +... |
| 23 | +$ gcloud config set project YOUR_PROJECT_ID |
| 24 | +``` |
| 25 | + |
| 26 | +Then configure the App Engine default GCS bucket for later use. The |
| 27 | +default App Engine bucket looks like |
| 28 | +YOUR_PROJECT_ID.appspot.com. Change the default acl of that bucket as |
| 29 | +follows: |
| 30 | + |
| 31 | +``` |
| 32 | +$ gsutil defacl ch -u AllUsers:R gs://YOUR_PROJECT_ID.appspot.com |
| 33 | +``` |
| 34 | + |
| 35 | +## Create and configure a Cloud SQL Second Generation instance |
| 36 | + |
| 37 | +In this guide, we use `wp` for various resource names; the instance |
| 38 | +name, the database name, and the user name. |
| 39 | + |
| 40 | +You can create a new Cloud SQL Second Generation instance with the |
| 41 | +following command: |
| 42 | + |
| 43 | +``` |
| 44 | +$ gcloud sql instances create wp \ |
| 45 | + --activation-policy=ALWAYS \ |
| 46 | + --tier=db-n1-standard-1 |
| 47 | +``` |
| 48 | + |
| 49 | +Then change the root password for your instance: |
| 50 | + |
| 51 | +``` |
| 52 | +$ gcloud sql instances set-root-password wp \ |
| 53 | + --password YOUR_INSTANCE_ROOT_PASSWORD # Don't use this password! |
| 54 | +``` |
| 55 | + |
| 56 | +To access this MySQL instance, we’ll use Cloud SQL Proxy. Please |
| 57 | +download an appropriate binary from |
| 58 | +[the download page][cloud-sql-proxy-download], make it executable. |
| 59 | + |
| 60 | +If you haven’t created a service account for the project, please |
| 61 | +create it on [the Credentials section][credentials-section] in the |
| 62 | +Console (Choose a new service account). Download the JSON key file and |
| 63 | +save it in a secure place. |
| 64 | + |
| 65 | +Run the proxy by the following command: |
| 66 | + |
| 67 | +``` |
| 68 | +$ cloud_sql_proxy \ |
| 69 | + -dir /tmp/cloudsql \ |
| 70 | + -instances=YOUR_PROJECT_ID:us-central1:wp=tcp:3306 \ |
| 71 | + -credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON |
| 72 | +``` |
| 73 | + |
| 74 | +Now you can access to the Cloud SQL instance with the normal MySQL |
| 75 | +client. Please create a new database and a user as follows: |
| 76 | + |
| 77 | +``` |
| 78 | +$ mysql -h 127.0.0.1 -u root -p |
| 79 | +mysql> create database wp; |
| 80 | +mysql> create user 'wp'@'%' identified by 'PASSWORD'; // Don't use this password! |
| 81 | +mysql> grant all on wp.* to 'wp'@'%'; |
| 82 | +mysql> exit |
| 83 | +Bye |
| 84 | +``` |
| 85 | + |
| 86 | +In the above example, I created a new database wp and a new user wp. |
| 87 | + |
| 88 | +## How to use |
| 89 | + |
| 90 | +First install the dependencies in this directory as follows: |
| 91 | + |
| 92 | +``` |
| 93 | +$ composer install |
| 94 | +``` |
| 95 | + |
| 96 | +If it complains about extensions, please install `phar` and `zip` PHP |
| 97 | +extesions and retry. |
| 98 | + |
| 99 | +Then run the helper command. |
| 100 | + |
| 101 | +``` |
| 102 | +$ php wordpress-helper.php setup |
| 103 | +``` |
| 104 | + |
| 105 | +The command asks you several questions, please answer them. Then |
| 106 | +you'll have a new WordPress project. By default it will create |
| 107 | +`my-wordpress-project` in the current directory. |
| 108 | + |
| 109 | +## Run WordPress locally and create a new user |
| 110 | + |
| 111 | +CD into your WordPress project directory and run the following command |
| 112 | +to run WordPress locally (be sure to keep the cloud SQL proxy |
| 113 | +running): |
| 114 | + |
| 115 | +``` |
| 116 | +$ cd my-wordpress-project |
| 117 | +$ php -S localhost:8000 -t wordpress wp.php |
| 118 | +``` |
| 119 | + |
| 120 | +Then access http://localhost:8000/. Follow the installation steps, |
| 121 | +create the admin user and its password. Login to the Dashboard and |
| 122 | +update if any of the plugins have update. I’m assuming that the local |
| 123 | +network is secure here. If you think differently, maybe you can first |
| 124 | +deploy the app (see below), and access the live site immediately, then |
| 125 | +create the first username and its password there. |
| 126 | + |
| 127 | +Now it’s ready for the first deployment. |
| 128 | + |
| 129 | +## Deployment |
| 130 | + |
| 131 | +Use the shell script wrapper for deployment as follows: |
| 132 | + |
| 133 | +``` |
| 134 | +$ sh deploy_wrapper.sh \ |
| 135 | + gcloud preview app deploy \ |
| 136 | + --promote --stop-previous-version app.yaml |
| 137 | +``` |
| 138 | + |
| 139 | +Then access your site, use the username and the password you created |
| 140 | +locally. The URL is: https://PROJECT_ID.appspot.com/ |
| 141 | + |
| 142 | +Go to the Dashboard, and in the Plugins page, activate the GCS media |
| 143 | +plugin. Try uploading a media and confirm the image is uploaded to the |
| 144 | +GCS bucket. |
| 145 | + |
| 146 | +## Check if the Batcache plugin is working |
| 147 | + |
| 148 | +On the plugin page in the WordPress dashboard, you should see 2 |
| 149 | +drop-ins are activated; `advanced-cache.php` and `object-cache.php`. |
| 150 | + |
| 151 | +To make sure it’s really working, you can open an incognito window and |
| 152 | +visit the site because the cache plugin only serves from cache to |
| 153 | +anonymous users. Also you should access the site several times because |
| 154 | +the plugin only caches pages which are considered popular. You will |
| 155 | +see the following Batcache stats in the HTML source: |
| 156 | + |
| 157 | +``` |
| 158 | +<!-- |
| 159 | + generated 31 seconds ago |
| 160 | + generated in 0.165 seconds |
| 161 | + served from batcache in 0.009 seconds |
| 162 | + expires in 269 seconds |
| 163 | +--> |
| 164 | +``` |
| 165 | + |
| 166 | +## Various workflows |
| 167 | + |
| 168 | +### Install/Update plugins/themes |
| 169 | + |
| 170 | +Because the wp-content directory on the server is read-only, you have |
| 171 | +to do this locally. Run WordPress locally and update plugins/themes in |
| 172 | +the local Dashboard, then deploy, then activate them in the production |
| 173 | +Dashboard. |
| 174 | + |
| 175 | +### Remove plugins/themes |
| 176 | + |
| 177 | +First Deactivate them in the production Dashboard, then remove them |
| 178 | +completely locally. The next deployment will remove those files from |
| 179 | +the production environment. |
| 180 | + |
| 181 | +### Update WordPress itself |
| 182 | + |
| 183 | +Most of the case, just download the newest WordPress and overwrite the |
| 184 | +existing wordpress directory. It is still possible that the existing |
| 185 | +config files are not compatible with the newest WordPress, so please |
| 186 | +update the config file manually in that case. |
| 187 | + |
| 188 | +### Update the base image |
| 189 | + |
| 190 | +We sometimes release the security update for |
| 191 | +[the php-docker image][php-docker]. Then you’ll have to re-deploy your |
| 192 | +WordPress instance to get the security update. |
| 193 | + |
| 194 | +Enjoy your WordPress installation! |
| 195 | + |
| 196 | +[composer]: https://getcomposer.org/ |
| 197 | +[dev-console]: https://console.cloud.google.com/ |
| 198 | +[cloud-sql-api-enable]: https://console.cloud.google.com/flows/enableapi?apiid=sqladmin |
| 199 | +[app-engine-setting]: https://console.cloud.google.com/appengine/settings |
| 200 | +[gcloud-sdk]: https://cloud.google.com/sdk/ |
| 201 | +[cloud-sql-proxy-download]: https://cloud.google.com/sql/docs/sql-proxy#installing |
| 202 | +[credentials-section]: https://console.cloud.google.com/apis/credentials/ |
| 203 | +[php-docker]: https://github.com/googlecloudplatform/php-docker |
0 commit comments