Have you encountered a problem where you want to host a simple web server but you don’t know how to go about it, here is a very simple solution to host a simple web server on Google cloud using the free tier.
In this tutorial, I will take you through the setting up of a server on Google cloud and later install Nginx. From here you can run a simple hello world.
Here I am using a free tier. Make sure you destroy your instance when you are done experimenting to avoid incurring extra charges.
What is Nginx?
Nginx pronounced as
engine x according to Wikipedia is a free and open-source web server that can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. Nginx is deployed to serve both static and dynamic content on the network.
Let’s dive in to spin up our server on the google cloud platform.
Set up Google Cloud platform.
I believe you already have your google cloud account with free tier capability. So to begin, we need to create project. Select create a project and give your project a name and give an organization name if you have one and click create.
On the notification click select project so that you will be in a position to create your VM instance from there.
Then the next thing is to enable the Compute Engine API so that you will be in a position to create and run virtual machines on GCP.
Inside the Compute Engine page, choose VM instance then create an instance.
Input the instance name, a region where you want to host your server, choose your desired machine configurations, boot disk such as Debian, Ubuntu etc, on the firewalls allow HTTP traffic so that you will not be blocked from accessing your server, and lastly click create.
After the VM has been created, connect to it by using ssh or the google console. To connect using ssh, click on the ssh and you will be taken to a terminal console.
Wait for it to validate ssh keys before letting you use the terminal. From here we have pinned up our server ready to do the update and installation of the Nginx server.
Update the repositories
First, let’s run an update and upgrade of our newly created instance in order to make the repositories up to date.
sudo apt update && apt upgrade -y
After the upgrade is complete we can now install the Nginx server with the following command.
sudo apt install nginx -y
From here you can check if the Nginx is up and running by using the following command.
$ ps auwx | grep nginx root 1861 0.0 0.3 58420 11984 ? S 18:35 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 1864 0.0 0.0 58792 3264 ? S 18:35 0:00 nginx: worker process sangkip+ 1891 0.0 0.0 5136 708 pts/0 S+ 18:38 0:00 grep nginx
Everything is up and running. Or you can use
sudo systemctl status nginx if it supports systems or
sudo service status nginx for sysV.
To know that the server is working we can do a verification from the browser. Use the IP address of the server
There you have it, we have installed Nginx successfully, next we need to use an HTML file to display static content on our page.
First, create an index.html page using vim or any code editor. I will be using the vscode.
Create an HTML file with the following content
Welcome to my website
Go back to the VMs terminal and upload the file you have created. It will go to the home directory, so we need to move it to
sudo mv index.html /var/www/html
Now go back to the browser and refresh your page, this time you will be in a position to see your message.
Lastly, we need to destroy our server to avoid extra charges.
Go to where you can see the tilda and click on it, click stop and then delete to remove the server from your project’s workspace.
And that is all for Nginx on Google Cloud Platform, go ahead and implement the SSL certificate option to remove not secure on the browser.