How to get a full-WordPress for 0€

JC / December 26, 2017

In this post I want to speak about to deploy a full wordpress blog in Google Cloud with zero cost. For this proposite I combine Google App Engine and Google Compute Engine, the first for to deploy the PHP code, and the second for to deploy a MySQL database.

Google App Engine for the to deploy de PHP code is easy: the free tier is under the few thousand of visits daily, if you blog pass this limit, you only pay a little extra cost.

The decision to the deploy database in Compute Engine is that Google offer a free low performance machine (only in some zones). It is a 700 Mb RAM machine, enought for a database for personals blogs

Besides all this, Google Cloud also offer a Google Cloud Shell for to deploy and SSL free for the url.

Prerequisites

Create a Google Compute Instance

In Compute Engine, select a new instance. The type of instance should be “Micro (1 CPU shared)” and the zone us-east1. Are also valid us-west1 and us-west1, because they’re the only areas where the first 700 hours per month are free. Before you accept, check at the right of the page that is zero cost. I ever select a ubuntu machine, but you can select your SO favorite.

After, two things:

  • Enter at the settings of the instance and  create new static IP and assing the new static IP instead the ephemeral IP (only for external IP, the IP internal dont have use for us)
  • Open the MySQL port (3306): You must create a new rule of the firewall in the “VPC network” section and assign it a new tag. Now, put the new tag in the “network labels” section, inside compute instance settings.

Install MySQL

Connect  at the new machine with web Shell, and create a MySQL instance:

$ sudo apt-get install mysql-server

and enter and config:

$  mysql -u root -p

The MySQL commands:

CREATE DATABASE wp CHARACTER SET UTF8;
CREATE USER wp@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wp.* TO wp@'%';
FLUSH PRIVILEGES;

Change ‘password’ for real password. Modify file /etc/mysql/mysql.conf.d/mysqld.cnf and comment the line:

#bind-address = 127.0.0.1

for allow acces at App Engine. Finally, restart the mysql server:

$ sudo service mysql restart

Download and configure WordPress

This post speak about to install wordpress with a tool for downloading and configuring WordPress. We are going to do the same steps.

Enter in the Google Cloud Shell and clone the repo:

$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git

install composer:

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
$ sudo mv composer.phar /usr/local/bin/composer

install the wordpress tool:

$ cd php-docs-samples/appengine/wordpress
$ composer install

Finally, download wordpress for App Engine:

$ php wordpress-helper.php setup -d ~/my-wordpress-project

Select any zone for cloud SQL. You must select “Standar Enviorement” The rest of params of the BBDD are the same of the last topic, and the project-id is the same of the you google cloud project.

At the end you will have a full wordpress at ‘~/my-wordpress-project’ directory. The last part, in the wp-config.php file, modify the db_host IP just like that:

if ($onGae) {
 /** The name of the Cloud SQL database for WordPress */
 define('DB_NAME', 'wp');
 /** Production login info */
 define('DB_HOST', 'X.X.X.X');
 define('DB_USER', 'wp');
 define('DB_PASSWORD', 'password');
} else {
 /** The name of the local database for WordPress */
 define('DB_NAME', 'wp');
 /** Local environment MySQL login info */
 define('DB_HOST', 'X.X.X.X');
 define('DB_USER', 'wp');
 define('DB_PASSWORD', 'password');
}

changing X.X.X.X with the external IP of the compute engine machine (currently, google does not support connections with internal network between App Engine and Compute Engine)

Run WordPress Locally and Deploy

For run locally (In google cloud shell)

$ sudo apt-get install php-horde-memcache
$ vendor/bin/wp server --path=wordpress

For deploy in App Engine:

$ gcloud app deploy --promote --stop-previous-version app.yaml cron.yaml

and you can enter in the web myproject.appspot.com for initialite your full cloud wordpress! (the first time is slower than usual)

I recommend visit this post for check batcache and install plugins.

Bonus

You can get a free HTTPS domain with Google Cloud. You have to configure your domain in the “Personal Domains” panel of the “configure” menu within App Engine!

any suggest for improvement this architecture or any doubt tell me!