This article will explain how to host a website with a custom domain on Google Cloud Storage. Cloudflare will be used to get the site working with https, though you could alternatively use a load balancer like Google Cloud Load Balancer.
- Verify your domain name in Google Search Console. Instructions for that are here, and will involve adding a TXT record to your domain DNS (note: do NOT add any Google-related CNAME records yet, this caused verification to fail for me)
- Create Google Cloud Storage bucket with same name as your domain name.
- In your Google Cloud Storage bucket Permissions, click “Grant Access”, in the “New principles” box type “allUsers” and select the role Cloud Storage → Storage Object Viewer. Adding this role will open your bucket up to the public.
- Go back to your list of buckets. Next to your bucket, click on the three-dot menu on the rightmost column, and click “Edit website configuration”. In the “Index (main) page suffix” box type “index.html”, and in the 404 box type 404.html.
- Upload your website to the bucket. At a minimum you will need a index.html file and a CNAME file containing your domain name (eg. jbernier.com). You can upload to your bucket with one of the following commands:
Sync local files to bucket:
gsutil rsync -r out gs://jbernier.com
Copy local files to bucket:
gsutil cp -r out gs://jbernier.app
Ok now your site will work with your custom domain over http, but not https. Google Cloud Storage does not support https out of the box, so we will need to handle that via a load balancer or a service like Cloudflare. Cloudflare has a free plan with generous limits of 100,000 reads per day, so in this tutorial we will go with Cloudflare.
Setting up Cloudflare
- Sign up for the Cloudflare free plan
- Update your DNS nameservers in your domain hosting settings as instructed
- In Cloudflare DNS settings, add a CNAME record pointing to “c.storage.googleapis.com”
That’s it! If certain files aren’t loading (eg. images), you can purge the cache in Cloudflare’s dashboard.
In another article I’ll discuss how to set up Google Cloud Run with a custom domain name.
Alternative articles: