Blog

Hosting a free website in Google Cloud

Jose Chapa • May 25 2023

Intro

There are plenty of simple all-in-one web hosting solutions that abstract the whole process and make it so anyone could make a website. But what's the fun in that? A solution where we can learn a lot more in the process is to run the web server ourselves, configure an engine such as WordPress, and for security and even more learning we can toss it in the cloud. So that is what we will achieve here.


Precautions

First thing's first. Although the hosting can be done 100% free on Google Cloud right now, this does not cover a domain. There are many ways to get free or almost free subdomains through services like Google sites, GitHub Pages and Netlify. But if you want a good, custom domain that doesn't require subdomains and has a reputable Top Level Domain, your best best is to purchase a domain from a reputable source such as Google Domains or GoDaddy. I was able to obtain a domain JChapa.com for only $1 per month.

Google Cloud has $300 credits for the first 90 days you use it. But most importantly, they have a free tier where you can set up a server and operate it perpetually at no cost, under certain parameters. We will be using the compute engine which has parameters found here. At the moment that includes 1 non-preemptible e2-micro VM instance per month in us-west1, us-central1, or us-east1. It also includes 30GB of standard persistent disk and 1GB of network egress per month. You will also be granted an IPv4 address.

These parameters are plenty for most people. Though if your site becomes a runaway success, your compute power will be a limiting factor and if you exceed the network egress, you will be billed for it. But don't worry, we will set up a budget cap, so you can have comfort knowing you won't get any surprise bills. At the end of this guide, you will have a website hosted using the WordPress engine on the Google Cloud Compute Engine, and you will only need to pay for your domain if you want your own proper domain. Without further ado, let's get started.

Configuring the Virtual Machine in Google's Cloud Compute Engine

Head to the Google Cloud Platform and sign up for the service. You will be granted $300 in credits for the first 90 days but our project should not require it. 

Head to the navigation pane on the left and select Compute Engine.

Create a virtual machine instance. Name it whatever you like. I will name mine web-host. And then we have to keep in mind the parameters for the free tier. Select an appropriate region such as us-east1. Select a General Purpose, E2 series processor. Specifically, select the Shared core e2-micro. This is the only processor available in the free tier. 

Now scroll down and select the button to change the Boot disk configuration. You can select any Linux operating system that you prefer, but my guide will be using Ubuntu 22.04 and the commands will not work across all distros. We will also select Standard persistent disk and 30GB to maximize our VM under this free tier.

Notice: the pricing summary does not say $0. Unfortunately google does not take into account your usage of free tier hardware and it shows the bill as if you had exceeded your resources. Rest assured, if you followed the guide correctly it will be free and we will create the budget caps in order to enforce that


Under the firewall settings make sure you allow http and https traffic. Then select create and we should be ready for the next step.

Associating your web server with your domain name

Although the web server is not up and running yet, we can go ahead and take the external IP allocated to your VM and associate it with your domain name. So whichever domain provider you used, you will need to head to your domain settings and find the DNS configurations. You will need to create an A record with the value of the external IP address allocated to your VM. You should also make an A record for the www. subdomain. Exact instructions will vary depending on the domain vendor. It will take time for the DNS records to propagate, so it is good to take care of this step now so it runs while we configure the web server itself.


Server initial configuration

Now back to the Google Cloud Compute Engine. Find your VM instance and SSH into it. It will take some time to create the SSH keys the first time. The first thing we'll do, as always, is update our system.

sudo apt update

sudo apt upgrade

Then we will use Ubuntu's package manager to install the Nginx web server, Mariadb database, and PHP.  You have options for other services but these are the ones we will be demonstrating to get WordPress running.

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

Database configuration

Secure your mySQL installation. Run the following command and select Y on each prompt to set it up with the defaults.

sudo mysql_secure_installation

You will create a database and user for WordPress to have the proper privileges. First log into the mySQL server

sudo mysql -u root -p

Enter the password you had setup for the root user. 

For the following, replace any sequence of 3 capital instances of a letter with a name of your choosing.

create database AAA default character set utf8 collate utf8_unicode_ci;

create user 'BBB'@'localhost' identified by 'CCC';

grant all privileges on AAA.* TO 'BBB'@'localhost';

flush privileges;

exit

The AAA designates the name of the database. The BBB is the username that WordPress will use to log into the database. And CCC designates the password that WordPress will use to log in. Replace each of those with your own values.

Setup WordPress

Head to the www directory and install the latest WordPress image. This directory is where web server files are traditionally stored.

cd /var/www

sudo wget https://wordpress.org/latest.tar.gz

sudo tar -zxvf latest.tar.gz

sudo rm latest.tar.gz

You will want www-data to the be owner of the wordpress directory.

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


Next we will need to know what version of php we have.

php --version

Take note of this. Mine says 8.1.2 but we only need to know the numbers before the second period. 8.1

We will use that information to create a conf file for the WordPress website. My preferred text editor is nano but you can use what you like.

nano /etc/nginx/sites-available/website.conf

Paste the following after you make some modifications:

upstream example-php-handler {

        server unix:/var/run/php/php8.1-fpm.sock;

}

server {

        listen 80; 

        server_name JChapa.com www.JChapa.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;

        }   

}

In the second line, change the php version to match yours. In the sixth line, modify the server name to match your domain name.

Next, create a symlink from your sites-available/website.conf file to the sites-enabled directory.

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

Make sure you change any file or directory names to make sure they match up with any modifications you've made along the way.

You will also need to remove the default Nginx config file.

sudo rm /etc/nginx/sites-enabled/default

Run the following to test your Nginx configuration. Address any errors found.

nginx -t

Once everything looks good you can restart the web server and you'll be ready to access it via web GUI.

systemctl restart nginx

Setup WordPress

From this point you should be able to access your website through he browser. Head to your domain and get access. You'll see the initial WordPress setup steps with a Site Title, username, and password that you must select. Then you can Install WordPress and you're off to the races.

At this stage you already have a functioning web server built on an engine with immense community support. You can select a theme, create some posts, and learn your way around WordPress. There are plenty of online resources that detail how to proceed from this point forward.

Budgeting

But before we leave we have to take care of some housekeeping. We can't let this server run wild. If we want to ensure we have no bills at the end of the month, or that we have reasonable bills, we must enforce a budget. 

On the Google Cloud Console, find the budgeting section. 


Here you can create a budget. Whether you want to set aside $100 per month, or $0 per month, you have the control here.

Conclusion

Most people could benefit in some way by having their own website. And although this process was more difficult than using Wix or Squarespace, you learned a lot about Linux administration, DNS records, and Google Cloud by doing it this way. On top of all that, you have way more customization and control over your website. 

Stick around for more guides on server administration!