I wanted to try out Google Cloud for a long time and now I have a good reason to do it. My AWS 1 year trial period is over, I used it to run my site pitank.com. It used wordpress and one instance with 1 Gb usually was enough for a low volume site like this. But this solution is not scalable: database and apache are run on the same node, wp data is stored on the system disk volume.
When I started to look into Google Cloud I saw it was quite different from AWS. It is related to the pricing model and how instances are sold to you (Google gives you much better price if you take instance for a long period of time).
Migrating WordPress from AWS to Google Cloud: Simple Way
If you want to create new a single-node wordress installation you can easily do it in a few clicks using Pre-Built Software Packages. Here is a nice step-by-step instruction to deploy single-node wordpress:
Even if you migrate from the existing wordpress you can export all data (posts, comments, images) from your current WP to a fresh one. To do it:
- Log in to your $old_site/wp-admin/
- Go to Tools->Export.
- “Choose what to export” -> All content
- Press “Download Export File”
- Then log in into you new wordpress installation $new_site/wp-admin/
- Go to Tools->Import.
- Choose WordPress.
- Then press “Install Now” to install “WordPress Importer”
- After that you will be able to import file with content you have previously downloaded from $old_sile
Using this way you import all the content from your old site, but all the plugins and custom theme changes and settings are not imported. So you will have to spend some time to make your new installation to look and feel exactly the same as your old version.
Is all-in-one WordPress installation really scalable?
The issue with this way is that you get the same all-in-one installation. You can scale it only by making your instance bigger, For Compute Engine you can scale your instance from 1 vCPU and 0.6 Gb of RAM to 32 vCPU and 120GB of RAM. Well that is probably enough for 99% of WordPress blogs in terms of computing power. But it has following disadvantages:
- Scaling up and down is possible only when the instance is stopped. It is typically manual operation and leads to downtime during the change. And it is really far from what you can call autoscalable.
- Your instance is a single point of failure. Well it is not easy to get rid of a single point of failure while deploying your applications, but we can probably do several steps in that direction.
- We don’t use any benefits of truly cloud applications with a current design. Cloud applications (Google Bucket Storage, Cloud SQL) are highly reliable and autoscalable by design,
So I decided to migrate to Google Cloud infrastructure and tried to make my WordPress a bit more ready to become scalable.
The ultimate goal is to decompose static content from dynamic content and scale them separately.
How to make WordPress more scalable?
To achieve scalability you need to solve at least several problems:
- WordPress database should be decomposed from other web services (apache/php). So it should be scaled independently from other pieces, and there should be a single entry point to database.
- Wp-content (all uploadable content) ideally have to be moved to the cloud storage.
To solve problem  Google has a Cloud SQL. Cloud SQL is a MySQL database that lives in Google’s cloud. It hugely simplifies SQL sever management, makes it reliable, secure and easily configurable. You don’t need to manage it over ssh, all DB related operations like import/export data, configuring regular backups and enabling binary logging (for point-in-time recovery and replication) can be done from Google Cloud Platform UI.
The feature I like about Cloud SQL is “Enable automatic storage increase”.”Adds storage capacity whenever space is low. Up to 25 GB per increase. All increases are permanent. Learn more“. No need to extend your volume manually, login to the instance, run fdisk to resize the partition to make FS aware of the disk increase. No more Pain. Awesome!
To connect Cloud SQL with WordPress you need to use cloud_sql_proxy.
I followed this official Google Guide to set up and configure cloud_sql_proxy. Once you are done with that, you need to configure WordPress to use cloud_sql_proxy instead of local mysql. It is as simple as changing one line in your wp-config.php:
Make sure to replace variables $project, $region, $cloud_sql_name with your project, region and cloud sql instance name. At this point connection between wordpress and cloud sql should work fine.
Starting Cloud SQL Proxy at boot time
To establish connection from the local machine to cloud sql you had to execute cloud_sql_proxy manually from the shell. Obviously, after reboot you will have to do it manually again. To automate connecting cloud_sql_proxy to the cloud sql instance you can write a simple script. Debian/Ubunty images in Google Cloud use systemd as service manager, so let’s write systemd unit to do the job.
Create service file “/usr/lib/systemd/system/cloud_sql.service” with the following content:
[Unit] Description=Cloud SQL Proxy After=network.target [Service] ExecStart=/$path_to_cloud_proxy/cloud_sql_proxy -dir=/cloudsql -instances=$project:$region:$cloud_sql_name Restart=always RestartSec=30 [Install] WantedBy=multi-user.target
It will be responsible for starting Cloud SQL Proxy and restarting it if dies. Just don’t forget to update path to the cloud_sql_proxy and parameters to reach cloud sql instance (project, region, instance-name).
Run the following commands to enable autostarting cloud_sql_proxy:
sudo systemctl daemon-reload sudo systemctl enable cloud_sql.service
To solve the problem  you can use wp-stateless plugin for the WordPress. The WP-Stateless plugin copies your media uploads to Google Cloud Storage in real-time as you add items to your Media Library. The uploaded files are then served directly from the Google bucket, making your media files load quicker from the distributed Google servers. The plugin will handle all media uploads including image thumbnails, PDF documents, audio files, and more.
What is great about WP-Stateless plugin is that you offload all your media to Google Cloud Storage, which is autoscalable by design and helps your site to work faster. Also it means you need smaller instance to serve your site, since part of the work is already served by Google Cloud Storage.
Google Cloud technologies like Cloud Storage and Cloud SQL helps to improve your site reliability, speed and simplicity of control/backup. This post just briefly describes what possibilities exist to use cloud power and in future post I plan to describe each option with more details.