How to host WordPress with Google Cloud Free

Running a WordPress blog can be free with some free services, the easy way is using WordPress.comBut there are many limitation with the free plan, for example use your own domain or some theme you like.

The minimum for which you can get a dedicated server hosting is for at least $4 a month to use own domain, but still you can’t use your own themes!

Plans and features pricing on WordPress.com

So, you want to run a WordPress blog with own domain and themes but still free? 

Yes, there is a choice with Goolge Cloud Platform (GCP) Compute Engine and I’m going to help you setup a WordPress blog on it!


Google provided Always Free Tier of its cloud services. The free resources are not much but is more than enough to run a startup blog free for a month, every month. There is also a $300 trial credit across all their cloud services for a year.


We’re going to use this free Virtual Private Server (VPS) to run our WordPress site using the nginx high performance web server.

Pros of self hosted WordPress blog

  • Hassle free management, you can just login to the server and execute the necessary commands
  • Manual web server performance tweaks for your needs
  • No charges for adding your own domain
  • Use any themes and plugins as you like
  • No struggling with cPanel

Setting up the WordPress blog

There are 3 steps in getting the WordPress blog up and running.
  1. Signup on Google Cloud Platform and create the instance.
  2. Install Nginx and setup WordPress on the Google Compute Engine (GCE) instance.
  3. Map DNS (I’m using Cloudflare) to External IP of GCE instance

 

Step 1. Google Cloud Platform Account

  • Sign up on Google Cloud Platform and login to Cloud Console
  • Create first project and give a name to the project (You can use the default project name but it seem there is some issue to change the project name)
  • Enable billing for that project before you can create any machines. This step requires you to have an international debit/credit card. (If you don’t have one, you can provide bank account information instead)
     
  • On left sidebar Cloud Console Menu -> Compute Engine -> VM Instances -> Create
  • Use following configuration f1-micro (1 vCPU, 0.6 GB memory), 30 GB HDD, Region (lowa: us-central1), Zone (us-central1-a) for free tier
  • Select Linux Distribution -> Ubuntu 18.04 LTS Minimal or which you are comfortable on
  • Allow HTTP and HTTPS under the firewall section
  • After successful creation of the machine, VM instances -> SSH on the instance under Connect
  • Noted down the External IP, you gonna use it to setup DNS later

You can refer to GCP Free Tier Docs for more region and zone and the latest free resources quota

Step 2. Setting up WordPress with Nginx and mysql

This part is easy, just copy paste the commands (modified word in red to match your info if needed). Since we chose the minimal version of ubuntu, it does not preinstalled the tools we need. We have to manually install the things but it do save space!
  • Remove the existing apache2 server (nginx is better at performance and handling a lot of traffic)
sudo apt-get purge apache2
sudo apt update 
  • Install Nginx, mysql, php7.4(can change if there is newer version) and required extensions
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/nginx
  
It may take some time or stucked after “Fetched 35.2 kB in 1s (33.0 kB/s)”just wait or ctrl+c if it take too long like over 3-5mins
 
sudo apt install nginx mysql-server php7.4 php7.4-fpm php7.4-curl php7.4-gd php7.4-intl php7.4-mbstring php7.4-soap php7.4-xml php7.4-xmlrpc php7.4-zip php7.4-mysql php7.4-imagick php7.4-cli nano 
  • Select your area when you see this Please select the geographic area in which you live”
  • Allocate 1 GB of HDD space to swap to increase performance
 
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
  
  • You can check if swapfile is active with this command
sudo swapon --show 
  • Set a root password for mysql, answer some questions. Basically press Y for all except 1
sudo mysql_secure_installation 
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: Y
 
There are three levels of password validation policy:
LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
 
Password typed is hidden and no cursor indicator move, just press enter after done
 
Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
 
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
 
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y 
  • Login to mysql
sudo mysql -u root 
  • Create database for WordPress, change the “your_password”
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'your_password'; 
FLUSH PRIVILEGES;
EXIT; 
  • Download the latest version of WordPress and move to home directory for your domain. Change “yourdomain.com” without “www”
cd /var/www/
sudo curl -LO https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo mv wordpress/ yourdomain.com/ 
  • Get secret key for WordPress and copy to next step wp-config.php
curl -s https://api.wordpress.org/secret-key/1.1/salt/ 
  • Configuring WordPress and fixing permissions for the WordPress directory
cd /var/www/yourdomain.com/
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php 
Hold shift+up/down key to select lines
ctrl+k to cut the present line for faster delete
after done ctrl+x, Y for yes, press Enter
 
  • Change info in red to match yours 
  • Fixing permissions for the WordPress directory
sudo chown -R www-data:www-data . 
  • Configuring nginx and enabling your website in nginx. You can also run multiple websites on a single server using this way.
cd /etc/nginx/sites-available
sudo nano default 
  • Remove “default_server” values in the file
listen 80 default_server; -> listen 80;
listen [::]:80 default_server; -> listen [::]:80; 
  • After done ctrl+x, Y for yes, press Enter
  • Create a site configuration file for nginx
cd /etc/nginx/sites-available
sudo nano yourdomain.com.conf 
  • Copy the following server configuration and replace “yourdomain.com” with yours
server {
         listen 80;
         listen [::]:80;
         root /var/www/yourdomain.com;
         index index.php index.html index.htm index.nginx-debian.html;
         server_name yourdomain.com www.yourdomain.com;
         
         client_max_body_size 256M;
         location / {
                 # First attempt to serve request as file, then
                 # as directory, then fall back to displaying a 404.
                 try_files $uri /index.php?$args;
         }
         location /wp-admin/ {
                 index index.php;
                 try_files $uri $uri/ /index.php$args;
         }
         location ~ .php$ {
                 include snippets/fastcgi-php.conf;
                 fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
                 fastcgi_connect_timeout 300s;
                 fastcgi_read_timeout 300s;
                 fastcgi_send_timeout 300s;
         }
         location ~* \.(txt|xml|js)$ {
                 expires 365d;
         }
         location ~* \.(css)$ {
                 expires 365d;
         }
          location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac|eot|ttf|otf|woff|svg)$ {
                 expires 365d;
         }
         location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
                 expires 365d;
         }
} 
  • Enable the site configuration in nginx for your domain.
#Change to root path
cd 
sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/  
  • Skip this unless you have any error happen on the command, switch to this path and remove the symlink.
cd /etc/nginx/sites-enabled
ls -l 
total 4 
lrwxrwxrwx 1 root root default -> /etc/nginx/sites-available/default 
lrwxrwxrwx 1 root root CorrectDomain.com.conf -> /etc/nginx/sites-available/CorrectDomain.com.conf 
lrwxrwxrwx 1 root root sites-enabled -> /etc/nginx/sites-available/WrongDomain.com.conf
#Remove the error symlink with name 
sudo rm sites-enabled 
 
  • Configure nginx upload size and hash by adding types_hash_max_size 2048; and client_max_body_size 256M;
sudo nano /etc/nginx/nginx.conf
 
http {
        ##
        # Basic Settings
        ##
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        client_max_body_size 256M; 
  • Configure PHP by changing some values in php.ini
sudo nano /etc/php/7.4/fpm/php.ini

upload_max_filesize = 256M
post_max_size = 256M
max_execution_time = 600
max_input_time = 400
max_input_vars = 10000
memory_limit = 256M 

ctrl+w to search keyword, remember to remove “;” in front max_input_vars

  • Test Nginx is working with this command
 
sudo nginx -t
#output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful 
  • Restart the nginx web server and php-fpm
sudo service php7.4-fpm restart
sudo service nginx restart 

Step 3. Map DNS to GCE External IP

I’m using Cloudflare as my DNS. If you don’t, you can just map the GCE External IP obtained above to your domain with your current DNS provider. But you can switch your DNS to Cloudflare for extra benefit!
 
Cloudflare acts as a shield between the internet and your site. It can prevent the incoming DDOS attacks, improve site loading speed, and the best part is you can get it for free too. 
The following steps will show you how to switch your DNS to Cloudflare.
  • Create Account on Cloudflare
  • Cloudflare Dashboard -> add a new site (Cloudflare will guide you through the initial setup)
  • Once updated the name servers on the Domain Name registrar (Namecheap, GoDaddy), we’ll proceed to map our domain to the GCE External IP
  • Under the DNS records section of your site on Cloudflare, delete all the existing A records and CNAME records 
  • Create a new type A Record name = “yourdomain.com” value = GCE External IP
  • Create a new type CNAME name = “www” value = “yourdomain.com”
  • Once done, give it a few minutes to update new DNS records
  • Go to www.yourdomain.com and you will see this WordPress installation. Continue the setup and enjoy your self hosted WordPress Blog!

That’s all for the 3 steps and you just got your WordPress blog running on GCP!

If you have any issues in any of the steps above, feel free to drop a comment!

2 thoughts on “How to host WordPress with Google Cloud Free”

  1. Pingback: How To Install / Upgrade To PHP 7.4 On Ubuntu 18.04 LTS - Jimmy Beh

  2. Pingback: Optimizing PHP-FPM Performance For High Load - Jimmy Beh

Leave a Comment

Scroll to Top