The Fun Way to Deploy a Website on Google Cloud
Tags: googlecloud, linux, terraform, devops
Published: 2023-11-11 08:07:33

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 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ra2144ymq6diivfaple.png)
- 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. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sr54dd5fviim8u4eh01v.gif)

**Connecting with SSH:**

To connect to your Linux instance using SSH, Open an SSH client on your local machine.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2v32ydobvpplx4afs8p.png)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/545g94bh36lx15j5o6jx.png)

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.