How to Host a Free Website on Google Cloud Platform

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to host a free website on Google Cloud Platform. This is possible on the Google Cloud Platform free tier.

By the end of this tutorial, you will have a fully-functioning WordPress website running on an Nginx web server. Let’s get started.

1. Create a Google Cloud Platform Account

First things first. Create yourself a Google Cloud Platform (GCP) account. This video will walk you through the process of setting up your GCP account if you don’t already have one.

2. Spin Up a Compute Engine VM on the Free Tier

From the GCP dashboard, click on Compute Engine. Create a VM instance.

In order to create your VM instance on the free tier, you must configure your VM with the following restrictions:

  • Non-preemptible f1-micro VM instance
  • US regions: Oregon (us-west1), Iowa (us-central1), or South Carolina (us-east1)
  • Up to 30 GB-months HDD

Google Cloud Platform Compute Engine VM free tier

Notice how it says “Your first 744 hours of f1-micro instance usage are free this month”. This number will vary depending on how many days are in the current month. For example, this screenshot was from October which has 31 days.

31 days x 24 hours = 744 hours

Feel free to choose any operating systems for the boot disk. In this tutorial, I chose Ubuntu 20.04 LTS.

3. Connect your Domain Name (optional)

You can optionally associate a domain name with your IP address. If you don’t have a domain name, feel free to skip ahead to the next step.

Otherwise, you can use create a DNS A record at your domain registrar with a value of the IP address of your Google Cloud Platform VM instance.

In Google Domains, for example, you can add the DNS A records for your domain name. The screenshot assumes the IP address of your VM instance is 35.222.110.120.

Google Domains A record for IP address

It can take up to 48 hours for your domain name to become associated with your IP address, but it usually happens within a few minutes.

4. Login to Your Server

You have a few different options for logging in to your VM instance. The easiest way is to select the “Open in browser window” which will log you in to your VM instance without the need to provide any credentials.

Open in browser window GCP VM instance

You can also use the gcloud command to log in via the command line or terminal.

5. Update Your VM

Once you’re logged in to your server, the first thing you want to do is update your system.

sudo apt update
sudo apt upgrade

6. Install the Web Server, Database, and PHP

Use the apt package manager to install the Nginx web server, Mariadb database, and PHP.

sudo apt-get install nginx mariadb-server php-fpm php-mysql

7. Setup the WordPress Database

First, secure your database installation. After executing the following command, answer Y for each security configuration option.

sudo mysql_secure_installation

Create a database and user with appropriate privileges for WordPress. Access the MySQL command prompt by simply typing mysql.

create database example_db default character set utf8 collate utf8_unicode_ci;
create user 'example_username'@'localhost' identified by 'example_password';
grant all privileges on example_db.* TO 'example_username'@'localhost';
flush privileges;
exit

8. Install WordPress

Next let’s download and install the latest version of WordPress from the official website.

cd /var/www
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -zxvf latest.tar.gz
sudo rm latest.tar.gz

Also, change the owner and group of the WordPress root directory to www-data.

sudo chown www-data:www-data -R wordpress/

9. Configure Nginx to Serve Your WordPress Website

Make a configuration file for your WordPress website at /etc/nginx/sites-available/example.conf with the following content adjusted accordingly for your website. Of course, feel free to name your configuration as you see fit.

upstream example-php-handler {
        server unix:/var/run/php/php7.4-fpm.sock;
}
server {
        listen 80; 
        server_name example.com www.example.com;
        root /var/www/wordpress;
        index index.php;
        location / { 
                try_files $uri $uri/ /index.php?$args;
        }   
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass example-php-handler;
        }   
}

You will need to change the server_name option to your domain name, or if you don’t have a domain name, simply change this line to server_name _;.

Also, depending on what version of PHP was installed, you may need to update line 2 to the actual version of PHP that’s installed on your server.

Finally, publish your website by making a symlink from your sites-available/example.conf file to the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/

Test your Nginx configuration changes and restart the web server.

nginx -t
systemctl restart nginx

10. Setup WordPress

Navigate to your IP address or domain name (in this case example.com) and you’ll see the famous five-minute WordPress installation process. In reality, it take about a minute to fill out this form.

Give your website a title, username, and secure password.

Famous five-minute WordPress installation process

After clicking on the Install WordPress button, you’ll have a brand-spanking new copy of WordPress on your web server. Feel free to pick out a theme (I recommend GeneratePress), write some blog posts, and make your website fast with caching plugins.

If you want to make another WordPress website, you can follow this tutorial that will teach you how to host multiple WordPress websites on a single server. This won’t incur any additional charges on Google Cloud Platform, but please be aware that you are limited to 1 GB of network egress per month. If you don’t know what this means, I explain it all in this video.

Any questions, let me know in the comments below.


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

7 thoughts on “How to Host a Free Website on Google Cloud Platform”

  1. Tony your work is amazing, your method to deliver knowledge is very simple. The best thing about you i like is the shortest videos. Straight to the point.

    Reply
  2. Great tutorial. And i did it too ^^
    My opinion, in this tutorial need to add “sudo mysql -u root -p “.
    Actually i stuck for a moment after step7. and watching Youtube tutorial and “sudo mysql -u root -p” was there.

    Reply
  3. And one more thing.
    It is not a good question. but i want to add subdomain thing.
    mydomain/sub (this index is index.html) how can i get this work?
    just add files to mydomain/sub and browser gets Forbidden error.

    Reply
    • Owh i did it.
      mydomain/sub folder was no access right.

      And i set access sudo chmod 777(full) mydomain/sub then get no error and it works.

      maybe 777(full) is not neccessary.

      Reply
  4. Hi Tony or anyone that can help…
    I’m almost at the end and just a little confused with sending the link from sites available to site directory sudo ln -s command..i get this message: missing file operand… Am i suppose copy and paste the whole link: sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/ or paste the link with my domain name in the place of example.conf or just type the command sudo ln -s and the link with my domain name will show up?
    Thank you,
    Claudia

    Reply
    • Hi Claudia. If you followed the tutorial step-by-step, you can copy the command as it is on this page. If not, just make sure to use the name of your configuration file and make sure these paths exist on your system.

      Reply

Leave a Comment