The Fun Way to Deploy a Website on Google Cloud
Mohamed
Posted on November 11, 2023
Google Cloud is one of the easiest cloud platforms to use, making it an excellent choice for beginners looking to get their feet wet in cloud computing. One of the first projects you can start with is deploying a web server. To make this project even more fun, we're going to deploy a website to Batman portfolio .
Prerequisites:
- A Google Cloud account
- Basic familiarity with Linux commands
- (Optional) Terraform knowledge
Steps:
Create a Linux instance: You can do this using the Google Cloud console (lazy mode) or Terraform (hacker mode).
Lazy Mode:
- Go to the Compute Engine > VM Instances page in the Google Cloud console.
- Click the "Create Instance" button.
- Choose a Linux operating system, such as Ubuntu or CentOS.
- Select the desired machine type and region.
- Enable HTTP and HTTPS traffic by checking the appropriate boxes.
- Click the "Create" button to provision the instance.
Hacker Mode:
- open the code shell editor
- create main.tf file
nano main.tf
- copy the Terraform configuration defines the instance resources.
- Initialize Terraform in the project directory.
terraform init
- Run
terraform plan
to review the planned changes. - Run
terraform apply
to apply the changes and provision the instance.
`provider "google" {
project = "qwiklabs-gcp-03-1b02059e2c47"
region = "us-central1"
zone = "us-central1-a" # Add the desired zone here
}
resource "google_compute_instance" "web_server" {
name = "web-server-instance"
machine_type = "f1-micro"
boot_disk {
initialize_params {
image = "ubuntu-os-cloud/ubuntu-2004-lts"
}
}
network_interface {
network = "default"
access_config {
// Automatically allocate an external IP
}
}
metadata_startup_script = <<-SCRIPT
#!/bin/bash
apt-get update
apt-get install -y apache2
SCRIPT
}
resource "google_compute_firewall" "allow-http" {
name = "allow-http"
network = "default"
allow {
protocol = "tcp"
ports = ["80"]
}
source_ranges = ["0.0.0.0/0"]
}
resource "google_compute_firewall" "allow-https" {
name = "allow-https"
network = "default"
allow {
protocol = "tcp"
ports = ["443"]
}
source_ranges = ["0.0.0.0/0"]
}
`
With Terraform, Apache installation is automated: When using Terraform, the Apache web server is automatically installed during the instance creation process.
Connecting with SSH:
To connect to your Linux instance using SSH, Open an SSH client on your local machine.
Navigate to the directory where you want to store the project files . remove the default index.html files
cd /var/www/html
sudo rm index.html
Clone the repository using the following command:
git clone https://github.com/Mouhamed-dridi/thebatman.git
Restart the Apache service to ensure the newly cloned project files are properly served:
sudo service apache2 restart
Test and Enjoy:
Open a web browser and navigate to your instance's external IP address.
You should now see the Batman portfolio website up and running.
Congratulations! You've successfully deployed a website on Google Cloud, whether you chose the manual or Terraform-automated approach.
Posted on November 11, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.