How install WordPress on Google Cloud ?
If you’ve ever explored hosting your own website or blog, chances are you’ve come across (or maybe even installed) WordPress. There’s not a lot of debate about WordPress’s popularity, with millions of people relying on it for their websites and blogs, but many public blogs are hosted by other companies, such as HostGator, BlueHost, or WordPress’s own hosted service, WordPress.com
To demonstrate the simplicity of Google Cloud, this article is going to walk you through installing WordPress yourself using Google Compute Engine and Google Cloud SQL to host your infrastructure.
First, let’s put together an architectural plan for how we’ll deploy WordPress using all the cool new tools
System layout overview
Before we get down to the technical pieces of turning on machines, let’s start by looking at what we need to turn on. We’ll do this by looking at the flow of an ideal request through our future system. We’re going to imagine a person visiting our future blog and look at where their request needs to go to give them a great experience. We’ll start with a single machine, shown in figure 1, because that’s the simplest possible configuration.
As you can see here, the flow is
- Someone asks the WordPress server for a page.
- The WordPress server queries the database
- The database sends back a result (for example, the content of the page)
- The WordPress server sends back a web page
When static content is requested, it doesn’t reuse the same flow. In this configuration, your WordPress server modifies references to static content so that rather than requesting it from the WordPress server, the browser requests it from Google Cloud Storage and the request flow you can see in figure 2
Digging into the database
Tons of databases are available, but one of the most popular open source databases is MySQL, which you’ve probably heard of. MySQL is great at storing relational data and has plenty of knobs to turn for when you need to start squeezing more performance out of it.
In the early days of cloud computing, the standard way to turn on a database like MySQL was to create a virtual machine, install the MySQL binary package, and then manage that virtual machine like any regular server. But as time went on, cloud providers started noticing that databases all seemed to follow this same pattern, so they started offering managed database services, where you don’t have to configure the virtual machine yourself but instead turn on a managed virtual machine running a specific binary.
All of the major cloud-hosting providers offer this sort of service—for example, Amazon has Relational Database Service (RDS), Azure has SQL Database service, and Google has Cloud SQL service. Managing a database via Cloud SQL is quicker and easier than configuring and managing the underlying virtual machine and its software, so we’re going to use Cloud SQL for our database. This service isn’t always going to be the best choice but for our WordPress deployment, which is typical, Cloud SQL is a great fit. It looks almost identical to a MySQL server that you’d configure yourself, but is easier and faster to set up.
Turning on a Cloud SQL instance
The first step to turning on our database is to jump into the Cloud Console by going to the Cloud Console (cloud.google.com/console) and then clicking SQL in the left side navigation, underneath the Storage section. You’ll see the blue Create instance button, shown in figure 3
When you select a Second Generation instance, you’ll be taken to a page where you can enter some information about your database. See figure 4. The first thing you should notice is that this page looks a little bit like the one you saw when creating a virtual machine. This is intentional—you’re creating a virtual machine that Google will manage for you, as well as install and configure MySQL for you. Like with a virtual machine, you need to name your database. For this exercise, let’s name the database wordpress-db (also like VMs, the name has to be unique inside your project, so you can have only one database with this name at a time).
Next let’s choose a password to access MySQL. Cloud Console can automatically generate a new secure password, or you can choose your own. We’ll choose my-very long-password! as our password. Finally, again like a VM, you have to choose where (geographically) you want your database to live. For this example, we’ll use us-central1-c as our zone.
To do any further configuration, click Show configuration options near the bottom of the page. For example, we might want to change the size of the VM instance for our database (by default, this uses a db-n1-standard-1 type instance) or increase the size of the underlying disk (by default, Cloud SQL starts with a 10 GB SSD disk). You can change all the options on this page later—in fact, the size of your disk automatically increases as needed—so let’s leave them as they are and create our instance. After you’ve created your instance, you can use the gcloud command-line tool to show that it’s all set with the gcloud sql command
$ gcloud sql instances list
NAME REGION TIER ADDRESS STATUS
wordpress-db – db-n1-standard-1 18.104.22.168 RUNNABLE
Securing your Cloud SQL instance
Before you go any further, you should probably change a few settings on your SQL instance so that you (and, hopefully, only you) can connect to it. For your testing phase you will change the password on the instance and then open it up to the world. Then, after you test it, you’ll change the network settings to allow access only from your Compute Engine VMs. First let’s change the password. You can do this from the command line with the gcloud sql users set-password command:
$ gcloud sql users set-password root “%” –password “my-changed-long-password-2!” –instance wordpress-db
Updating Cloud SQL user…done.
In this example, you reset the password for the root user across all hosts. (The MySQL wildcard character is a percent sign.) Now let’s (temporarily) open the SQL instance to the outside world. In the Cloud Console, navigate to your Cloud SQL instance. Open the Authorization tab, click the Add network button, add “the world” in CIDR notation (0.0.0.0/0, which means “all IPs possible”), and click Save. See figure 5.
Connecting to your Cloud SQL instance
you don’t have a MySQL client, the first thing to do is install one. On a Linux environment like Ubuntu you can install it by typing the following code
$ sudo apt-get install -y mysql-client
On Windows or Mac, you can download the package from the MySQL website: http://dev.mysql.com/downloads/mysql/. After installation, connect to the database by entering the IP address of your instance (you saw this before with gcloud sql instances list). Use the username “root”, and the password you set earlier. Here’s this process on Linux
$ mysql -h 22.214.171.124 -u root -p Enter password: # <I typed my password here> Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 59 Server version: 5.7.14-google-log (Google) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Next let’s run a few SQL commands to prepare your database for WordPress.
Configuring your Cloud SQL instance for WordPress
Let’s get the MySQL database prepared for WordPress to start talking to it. Here’s a basic outline of what we’re going to do:
- Create a database called wordpress.
- Create a user called wordpress.
- Give the wordpress user the appropriate permissions
The first thing is to go back to that MySQL command-line prompt. As you learned, you can do this by running the mysql command. Next up is to create the database by running this code:
mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.10 sec)
Then you need to create a user account for WordPress to use for access to the database:
mysql> CREATE USER wordpress IDENTIFIED BY ‘very-long-wordpress-password’;
Query OK, 0 rows affected (0.21 sec)
Next you need to give this new user the right level of access to do things to the database (like create tables, add rows, run queries, and so on)
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO wordpress;
Query OK, 0 rows affected (0.20 sec)
Finally let’s tell MySQL to reload the list of users and privileges. If you forget this command, MySQL would know about the changes when it restarts, but you don’t want to restart your Cloud SQL instance for this:
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.12 sec)
Installing the WordPress VM
Let’s start by turning on the VM that will host our WordPress installation. As you learned, you can do this easily in the Cloud Console, so let’s do that once more. See figure 6
Take note that the check boxes for allowing HTTP and HTTPS traffic are selected because we want our WordPress server to be accessible to anyone through their browsers. Also make sure that the Access Scopes section is set to allow default access. After that, you’re ready to turn on your VM, so go ahead and click Create
The first thing to do now that your VM is up and running is to connect to it via SSH. You can do this in the Cloud Console by clicking the SSH button, or use the Cloud SDK with the gcloud compute ssh command. For this walkthrough, you’ll use the Cloud SDK to connect to your VM:
$ gcloud compute ssh --zone us-central1-c wordpress Warning: Permanently added 'compute.6766322253788016173' (ECDSA) to the list of known hosts. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.13.0-1008-gcp x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 0 packages can be updated. 0 updates are security updates. The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. jjg@wordpress:~$
After you’re connected, you need to install a few packages, namely Apache, MySQL Client, and PHP. You can do this using apt-get:
jj@wordpress:~$ sudo apt-get update jj@wordpress:~$ sudo apt-get install apache2 mysql-client php7.0-mysql php7.0 libapache2-mod-php7.0 php7.0-mcrypt php7.0-gd
When prompted, confirm by typing Y and pressing Enter. Now that you have all the prerequisites installed, it’s time to install WordPress. Start by downloading the latest version from wordpress.org and unzipping it into your home directory
jj@wordpress:~$ wget http://wordpress.org/latest.tar.gz jj@wordpress:~$ tar xzvf latest.tar.gz
You’ll need to set some configuration parameters, primarily where WordPress should store data and how to authenticate. Copy the sample configuration file to wp-config .php, and then edit the file to point to your Cloud SQL instance.
jj@wordpress:~$ cd wordpress jj@wordpress:~/wordpress$ cp wp-config-sample.php wp-config.php jj@wordpress:~/wordpress$ vim wp-config.php
After editing wp-config.php, it should look something like the following listing
WordPress configuration after making changes for your environment
<?php /** * The base configuration for WordPress * * The wp-config.php creation script uses this file during the * installation. You don't have to use the website, you can * copy this file to "wp-config.php" and fill in the values. * * This file contains the following configurations: * * * MySQL settings * * Secret keys * * Database table prefix * * ABSPATH * * @link https://codex.wordpress.org/Editing_wp-config.php * * @package WordPress */ /** MySQL settings - You can get this info from your web host **/ /** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'wordpress'); /** MySQL database password */ define('DB_PASSWORD', 'very-long-wordpress-password'); /** MySQL hostname */ define('DB_HOST', '126.96.36.199'); /** Database Charset to use in creating database tables. */ define('DB_CHARSET', 'utf8'); /** The Database Collate type. Don't change this if in doubt. */ define('DB_COLLATE', ''); ?>
After you have your configuration set (you should need to change only the database settings), move all those files out of your home directory and into somewhere that Apache can serve them. You also need to remove the Apache default page, index.html. The easiest way to do this is using rm and then rsync:
jj@wordpress:~/wordpress$ sudo rm /var/www/html/index.html jj@wordpress:~/wordpress$ sudo rsync -avP ~/wordpress/ /var/www/html/
Now navigate to the web server in your browser (for example, http://188.8.131.52 in this specific example), which should end up looking like figure 7.
From there, following the prompts should take about 5 minutes, and you’ll have a working WordPress installation!
Reviewing the system
So what did you do here? You set up quite a few different pieces:
- You turned on a Cloud SQL instance to store all of your data.
- You added a few users and changed the security rules.
- You turned on a Compute Engine virtual machine.
- You installed WordPress on that VM.
Did you forget anything? Do you remember when you set the security rules on the Cloud SQL instance to accept connections from anywhere (0.0.0.0/0)? Now that you know from where to accept requests (your VM), you should fix that. If you don’t, the database is vulnerable to attacks from the whole world. But if we lock down the database at the network level, even if someone discovers the password, it’s useful only if they are connecting from one of our known machines.
To do this, go to the Cloud Console, and navigate to your Cloud SQL instance. On the Access Control tab, edit the Authorized Network, changing 0.0.0.0/0 to the IP address followed by /32 (for example, 184.108.40.206/32), and rename the rule to read us-central1-c/wordpress so you don’t forget what this rule is for. When you’re done, the access control rules should look like figure 8.
Remember that the IP of your VM instance could change. To avoid that, you’ll need to reserve a static IP address, but we’ll dig into that later on when we explore Compute Engine in more depth
Q: What is Google Cloud?
A: Google Cloud is a cloud computing platform offered by Google that allows you to run your applications and store your data in a virtual environment.
Q: Why would I want to install WordPress on Google Cloud?
A: Installing WordPress on Google Cloud can provide you with a scalable, reliable, and cost-effective platform for hosting your website.
Q: What do I need to install WordPress on Google Cloud?
A: You will need a Google Cloud account, a virtual machine instance, and a domain name. You will also need to configure your virtual machine instance to meet the minimum system requirements for WordPress.
Q: How do I create a virtual machine instance on Google Cloud?
A: You can create a virtual machine instance on Google Cloud using the Compute Engine service. You can use the Google Cloud Console or the gcloud command-line tool to create a new virtual machine instance.
Q: How do I install WordPress on my virtual machine instance?
A: There are several ways to install WordPress on your virtual machine instance, but one common method is to use a web hosting control panel such as cPanel or Plesk to install WordPress. Alternatively, you can install WordPress manually by downloading the latest version from the WordPress website and uploading the files to your virtual machine instance
Q: How do I configure my virtual machine instance to meet the minimum system requirements for WordPress?
A: The minimum system requirements for WordPress include PHP 7.3 or higher, MySQL 5.6 or higher, and Apache or Nginx web server. You can use a package manager such as apt or yum to install these components on your virtual machine instance.
Q: How do I connect my domain name to my virtual machine instance?
A: You can connect your domain name to your virtual machine instance by creating a DNS record that points to the IP address of your virtual machine instance. You can do this through your domain registrar or DNS provider.
Q: How do I secure my WordPress installation on Google Cloud?
A: You can secure your WordPress installation on Google Cloud by enabling HTTPS using an SSL/TLS certificate, implementing security measures such as two-factor authentication and strong passwords, and keeping your WordPress installation and plugins up to date with the latest security patches.