Hosting a simple web server with Google Cloud

Note: When creating a new project in Google Cloud Platform, you need to link billing account information to it. You may also need to enable the Compute Engine API when you get to the corresponding step.

Hosting a simple web server is a great way to get familiar with cloud hosting technologies; this step helps you to understand how websites and web applications actually run. In this article, you will utilize the Google Cloud Platform to create a vm and host a basic web server. Start by creating/logging in to your Google Cloud Platform account.

Click the select a project tab from the top nav bar

Click new project

Input a project name while avoiding spaces in it.

Give google a few minutes to set up your new project. Your dashboard should look like this when ready.

Scroll down the blade on the left side of the screen until you get to Compute Engine and then select VM instances. Compute Engine may take a few minutes to get ready. You may also need to enable the Compute Engine API here as well.

Select create new virtual machine.

Title the VM and make sure to keep the region set as the default option, which for me was us-west1 Oregon. Keeping the default region enables the option to create a free VM. For the free VM, set Machine family to General-purpose, Series to N-1, and machine type to f-1 micro. This VM is a relatively weak machine, but it’s a great way to try things out without breaking the bank. If you need/want more power, adjust machine configuration accordingly.

Under the firewall section, ensure to select Allow HTTP Traffic. This will let HTTP traffic come to your VM and allow viewers to see your web page. Leave everything else as default and create the VM.

Once the VM has been created, go to the VM Instances page and select SSH connection to get access to your VM. Your internal and external IP addresses will be here. The external IP is important to note down as that is how others will access your site.

Once the SSH connection is complete, you’ll have access to the linux VM terminal. There’s a few commands to run. The commands below will update the system and install Apache, which is a web server software package.

sudo apt update

sudo apt - y install apache2

When Apache is installed, it creates an index.html file located at /var/www/html/index.html. There is an Apache default page there, however, you can upload a new html file there and when the external IP is accessed the index file will appear. To do a quick test to verify the VM is up and running, copy and paste the external IP into a web browser and a default Apache web page should come up.

The final step involves updating the index.html file to be what you want it to be. There are multiple ways to accomplish this but the simplest is the command below. It will replace the html currently there with what’s after the echo command.

echo '<!doctype html><html><body><h1>Happy Snail Google VM!</h1></body></html>' | sudo tee /var/www/html/index.html

Now try revisiting your external IP and you should see the updated page. This is a super simplistic look at a web server, but it shows the basics of setting up a VM hosted on the cloud, installing Apache web server, and having it be accessible through the cloud. The one issue is that an IP address is extremely difficult to remember and ugly to type into a web browser. So, how do we get easy-to-remember links such as google.com? These are done through Domain Name Services (DNS) which will be discussed in a future article. Thanks for reading!

Leave a comment