Build and Host a Website on GCP
Overview:
Google Cloud Platform (GCP), a suite of cloud computing services offered by Google, has experienced a surge in demand, particularly for the same infrastructure that powers Google’s popular products like Google Search, Gmail, file storage, and YouTube.
Cloud computing is a shared pool of adaptable resources, including networks, servers, storage, applications, and services, that customers can utilize to build and deploy their products and services.
In this article, I will explore how to host and build your web application or website on the Google Cloud Platform. Choosing GCP to host your website offers numerous advantages such as cost-effective services, high speed, reliability, and the ability to scale cloud storage as needed.
What is Google Website Hosting?
Google Cloud Platform offers a range of web hosting solutions, including Compute Engine, a powerful and flexible virtual server option often used by developers and businesses. Compute Engine is particularly well-suited for website hosting, providing the infrastructure and tools to build and manage websites efficiently.
Objectives:
1. Create a VM to host your website
2. Install Apache Server to host the virtual machine
3. Build a first web page for your website on GCP
4. Configure Cloud DNS for your Website Domain name
5. Update the Nameservers in your Domain Provider Panel
Preconditions:
1. GCP Free Tier Account
2. Access to IAM and Admin
Step 1: Create a Project
Before we start, let’s create a new project. This way, you can easily delete the project and all its associated resources later if you don’t need to keep them. In the Google Cloud console, on the project selector page, select or create a new Google Cloud project.
Step 2: Create a Virtual Machine (VM) to host your website
Google Cloud makes hosting a secure and reliable website simple, allowing you to accomplish more while saving money. While invisible to the naked eye, virtual machines function similarly to physical computers and can be accessed over the internet via the cloud.
Follow the below steps to create a VM for your website.
1. Go to Google Cloud Platform Console
2. In the navigation menu click on Compute Engine
3. Click on VM Instance
4. Now, click on Create to create a new instance
1. Enter the name of an instance under the ‘Name’ heading
2. Select Region and Zone nearest to you
3. Select Machine Configuration as per your traffic and requirements. See the below image for better understanding.
1. Under the Boot Disk option, select the boot disk you want to use. In this case, we will use Ubuntu. If you need to change the boot disk, click on Change
2. To choose your boot disk, please click on “Select”
3. In the Firewall section, ensure both “Allow HTTP traffic” and “Allow HTTPS traffic” are checked
4. Click on Create to create the instance
Step 3: Install Apache Server to host the virtual machine
Deploying an Apache web server on your GCP VM is straightforward. Execute a few commands, and your VM transforms into a versatile platform for hosting websites, files, and more.
1. In the Navigation Menu, Click on Compute Engine>>VM Instances
2. Click on the VM instances you’ve created
3. Click on SSH to connect to your VM
4. Wait for the instance to connect
Run the below commands to install the Apache server on your machine
sudo su
apt-get install apache2
(enter Y if prompted)
service apache2 status
2. You can check whether your server is running by going to the External IP address of the web-hosting-instance VM
3. You will see the below page if your server is running
Step 4: Build a first web page for your website on GCP
Kickstart your website development on the Google Cloud Platform (GCP). Harness its robust infrastructure to host your inaugural web page, ensuring a smooth user experience and global accessibility. GCP’s user-friendly tools empower you to effortlessly deploy your content and lay the groundwork for future growth as your online presence expands.
After installing the Apache server on your host machine. Let’s go ahead and modify the index.html web page stored in the /var/www/html folder.
Follow the below steps to create your first default home web page.
1. Enter the commands into SSH of your web-hosting-instance VM
2. $ ls /var/www/html (You will see index.html file)
3. $ rm /var/www/html/index.html (To delete the default home web page)
4. $ vi /var/www/html/index.html (To create a new default home web page)
5. Enter i on the keyboard to edit the document in vi editor
6. Create your web page by using HTML
7. See below commands and image for reference.
Commands:
$ ls /var/www/html
$ rm /var/www/html/index.html
$ vi /var/www/html/index.html
------------------------------------------------------------------------------
Code:
<html>
<head></head
<body>
<h1>Welcome to My Web Page</h1>
<marquee>This is a simple website</marquee>
</body>
</html>
1. Press Esc, Shift+Colon then type wq to save the file
2. Now, Enter command service apache2 restart to restart the server
3. Again, Go to the External IP address of the mywebsite VM. Congo, You will see the below page.
Step 5: Configure Cloud DNS for your Website Domain name
Configuring Cloud DNS for your website domain is essential for making it accessible online. It involves pointing your domain name to the IP address where your website is hosted, ensuring visitors can find your site. Cloud DNS offers benefits like high availability, scalability, and easy management, making it a popular choice.
Follow these steps to Configure Cloud DNS for your Website Domain name
1. Go back to Navigation Menu search for Network Services
2. Click on Network Services
3. Click on Cloud DNS
1. Click on Create Zone
2. Enter the Zone name under ‘Zone name’ field
3. Under DNS name, enter your website domain name that you want to host and build
4. Provide description if applicable (Optional)
1. Click on Create button
2. Now, click on the Add Record Set button
3. Select Record Reference Type as A and under IPV4 enter the enternal address of the web-hosting-instance VM instance
1. Once again click on the Add Record Set button
2. Select Record Reference Type as CNAME
3. Enter www into DNS Name and [your domain website name] into Canonical name
4. Click on Create
Now that we have configured our Cloud DNS for the website domain name let’s go ahead and register it. On the top right side, you will see Registrar Setup click on it. But make sure to note down the nameservers on the notepad for future use. You will use these nameservers to point your domain to Google Cloud for hosting.
Step 6: Update the Nameservers in your Domain Provider Panel
To make your new website live, update your domain’s nameservers. These tell your domain where your website files are hosted. Get the new nameservers from your hosting provider, log into your domain registrar’s account, and replace the old nameservers with the new ones. Changes can take time to update, so be patient.
Login to any domain provider website like domain.com, Wix, etc. Then navigate to the DNS Management page of your domain. Update your nameservers into the fields that you have noted down before. Your website is now live on the Google Cloud Platform! The domain name may take 24–48 hours to update fully. In the meantime, you can set up your database and any other services your site needs.
Thank you for reading my post! Be sure to clean up your environment after.