Deploying Your First Website on Google Cloud


Google Cloud Platform (GCP) provides various services to host and deploy websites. In this guide, we'll walk you through deploying your first website using Google Cloud Storage, a simple and cost-effective way to host static websites.


Step 1: Create a Google Cloud Storage Bucket

Follow these steps to create a storage bucket for your website:

  1. Go to the Google Cloud Console.
  2. Click "Storage" in the left sidebar and then "Browser."
  3. Click the "Create Bucket" button.
  4. Provide a unique name for your bucket and click "Create."

Step 2: Upload Your Website Files

Upload your website's HTML, CSS, and other static files to the bucket you created. You can use the Google Cloud Console or the Google Cloud SDK for this.


Step 3: Set Bucket Permissions

Make sure your bucket's objects are publicly accessible. Follow these steps:

  1. Select the bucket you created in the Google Cloud Console.
  2. Click "Permissions" and add a new entity with the email address "allUsers" and the role "Storage Object Viewer."

Step 4: Enable Website Hosting

Enable website hosting for your bucket:

  1. Select your bucket in the Google Cloud Console.
  2. Click "Edit Bucket" and navigate to the "Website configuration" tab.
  3. Enable the "Website configuration" and set the main page and error page (e.g., "index.html" and "404.html").

Step 5: Access Your Website

Your website is now live! You can access it using the provided URL. It follows the format:

https://storage.googleapis.com/your-bucket-name
.


Sample Code: Deploying a Simple Website using Google Cloud SDK

Here's a sample Python script to create a bucket, upload files, and set up website hosting using the Google Cloud SDK:


# Import the Google Cloud Storage client library
from google.cloud import storage
# Set your project ID
project_id = 'your-project-id'
# Set your bucket name
bucket_name = 'your-bucket-name'
# Set the local directory containing your website files
local_website_dir = 'path/to/website/files'
# Create a client
client = storage.Client(project=project_id)
# Create a bucket
bucket = client.bucket(bucket_name)
bucket.create()
# Upload website files
blob = bucket.blob('index.html')
blob.upload_from_filename(f'{local_website_dir}/index.html')
# Enable website hosting
bucket.configure_website(index_page_suffix='index.html', not_found_page='404.html')
print(f'Website hosted at: https://storage.googleapis.com/{bucket_name}')

Conclusion

Hosting a website on Google Cloud Storage is a straightforward and cost-effective way to deploy your first website. You can expand your website's functionality by integrating with other GCP services as needed.