Search code examples
gogoogle-cloud-platformweb-deploymenthugo

How to deploy a Hugo website from a Google Cloud VM?


I've started up a Google Cloud VM with the external IP address 35.225.45.169:

enter image description here

Just to check that I can serve a website from there, I've cloned a Hugo started project and run hugo server --bind=0.0.0.0 --baseURL=http://0.0.0.0:1313:

kurt_peek@mdm:~/synamdm$ hugo server --bind=0.0.0.0 --baseURL=http://0.0.0.0:1313
Building sites … WARN 2020/01/02 04:36:44 .File.Dir on zero object. Wrap it in if or with: {{ with .File }}{{ .Dir }
}{{ end }}
                   | EN  
+------------------+----+
  Pages            | 16  
  Paginator pages  |  0  
  Non-page files   |  0  
  Static files     | 20  
  Processed images |  0  
  Aliases          |  0  
  Sitemaps         |  1  
  Cleaned          |  0  
Built in 112 ms
Watching for changes in /home/kurt_peek/synamdm/{content,layouts,static,themes}
Watching for config changes in /home/kurt_peek/synamdm/config.toml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://0.0.0.0:1313/ (bind address 0.0.0.0)
Press Ctrl+C to stop

Now I would expect to be able to go to http://35.225.45.169:1313/ in my browser and for the website to be visible, but I find that it is not; instead the operation times out (as shown below with a curl command):

> curl http://35.225.45.169:1313
curl: (7) Failed to connect to 35.225.45.169 port 1313: Operation timed out

Am I missing something here? How should I deploy this static website from the Google Cloud Compute instance to the internet?

Update

Following Ahmet's comment, I edited the VM to allow HTTP and HTTPS traffic. This appears to have created several Firewall Rules in the VPC Network tab (see below).

enter image description here

enter image description here

However, I'm still not able to access http://35.225.45.169:1313/ after this; are there specific rules that I must define?


Solution

  • How to deploy a Hugo website from a Google Cloud VM?

    As pradeep mentioned, you will need to create a new Firewall rule that allows the port tcp:1313 to receive and egress traffic.

    Here you will find more details on how to create Firewalls rules in Google Cloud Platform.

    Nonetheless, I think there are better approaches depending on the website that you would like to serve. Here you will find the different options available for serving websites in Google Cloud Platform, but mainly there are three:

    1. Using Google Cloud Storage.
    2. Using Google App Engine.
    3. Firebase Hosting.

    Google Cloud Storage

    If you are serving a static website, I highly recommend you to go with Google Cloud Storage or Firebase Hosting. It is true that they do not have either Load Balancing capabilities or Logging, but they are an easy way if you are new to Google Cloud Platform.

    As shown here if you would like to host a static site, you could do it within Cloud Storage, but you will need to create a Cloud Storage Bucket, and upload the content to it.

    Here you will find more information and a tutorial on how host static websites within Google Cloud Platform using Google Cloud Storage.

    Google App Engine

    Another option would be to use App Engine, not only is fully managed by Google's infrastructure but also is more simpler than spinning up a VM and making sure that X ports are open or not, Google does it for you.

    I attached you a tutorial on how to host Hugo on Google App Engine.

    Firebase Hosting

    Finally, you could also use Firebase Hosting in order to serve your Hugo website. I attached some documentation regarding more detail information about Firebase Hosting here.

    I hope it helps.