Serving Websites

This article discusses how to host a website on Google Cloud Platform (GCP). GCP provides a robust, flexible, reliable, and scalable platform for serving websites. Google built GCP by using the same infrastructure that Google uses to serve content from sites such as Google.com, YouTube, and Gmail. You can choose to serve your website's content by using the type and design of infrastructure that best suits your needs.

You might find this article useful if you are:

  • Knowledgeable about how to create a website and have deployed and run some web-serving infrastructure before.
  • Evaluating whether and how to migrate your site to GCP.

Choosing an option

If you're new to using GCP, it's a reasonable approach to start by using the kind of technology you're already familiar with. For example, if you currently use hardware servers or virtual machines (VMs) to host your site, perhaps with another cloud provider or on your own hardware, Google Compute Engine provides a familiar paradigm for you. If you already use a platform-as-a-service (PaaS) offering, such as Heroku or Engine Yard, Google App Engine might be the best place to start.

After you become more familiar with GCP, you can start to explore the richness of products and services that GCP provides. For example, if you started by using Compute Engine, you might choose to augment your site's capabilities by using Google Kubernetes Engine or migrate some or all of the functionality to App Engine.

The following table summarizes your hosting options on GCP:

Option Product Summary
Static website Google Cloud Storage
Firebase Hosting
Deliver static web pages and assets from a Cloud Storage bucket. This is the simplest option on GCP, and you get automatic scaling with no additional effort.
Firebase Hosting, which uses Cloud Storage, provides some additional features.
Virtual machines

(Linux and Windows)

Google Compute Engine Install, configure, and maintain your own web hosting stack. You have control of every component, but you also have all the responsibility to keep things running. You also must decide how to provide for load balancing and scalability, from a variety of options.

If you run a .NET-based website on Windows, choose this option.

Containers Google Kubernetes Engine Use container technology to package your dependencies with your code for easier deployment. Then, use Kubernetes Engine to manage clusters of your containers.
Managed platform Google App Engine Focus on your code, deploy to App Engine, and let Google manage the systems for you. You have a choice of the standard environment, which prescribes the languages and runtimes that you can use, and the flexible environment, that gives you additional options but requires some self-management.

Reading all of this article will help you to understand the main technologies that you can use for web serving on GCP and give you a glimpse of how the technologies work. The article provides links to complete documentation, tutorials, and solutions articles that can help you build deeper understanding, when you're ready.

Understanding costs

Because there are so many variables and each implementation can be so different, it's beyond the scope of this article to provide specific advice about costs. To understand Google's principles about how pricing works on GCP, see the Pricing page. To understand pricing for individual services, see the product pricing section. You can also take advantage of some tools to help you evaluate the costs of using GCP.

  • The pricing calculator provides a quick and easy way to estimate what your GCP usage will look like. You can provide details about the services you want to use and then see a pricing estimate.
  • The total cost of ownership (TCO) tool evaluates the relative costs for running your compute load in the cloud, and provides a financial estimate. The tool provides several inputs for cost modeling, which you can adjust, and then compares estimated costs on Cloud Platform and Amazon Web Services. This tool does not model all components of a typical application, such as storage and networking.

Setting up domain name services

Usually, you will want to register a domain name for your site. You can use a public domain name registrar, such as Google Domains, to register a unique name for your site. If you want complete control of your own domain name system (DNS), you can use Google Cloud DNS to serve as your DNS provider. The Cloud DNS documentation includes a quickstart to get you going.

If you have an existing DNS provider that you want to use, you generally need to create a couple of records with that provider. For a domain name such as example.com, you'd create an A record with your DNS provider. For the www.example.com sub-domain, you'd create a CNAME record for www to point it to the example.com domain. The A record maps a hostname to an IP address. The CNAME record creates an alias for the A record.

If your domain name registrar is also your DNS provider, that's probably all you need to do. If you use separate providers for registration and DNS, make sure that your domain name registrar has the correct name servers associated with your domain.

After making your DNS changes, the record updates will take some time to propagate depending on your time-to-live (TTL) values in your zone. If this is a new hostname, the changes should go into effect quickly because the DNS resolvers will not have cached previous values and will contact the DNS provider to get the necessary information to route requests.

Hosting a static website

The simplest way to serve website content over HTTP(S) is to host static web pages. Static web pages are served unchanged, as they were written, usually by using HTML. Using a static website is a good option if your site's pages rarely change after they have been published, such as blog posts or pages that are part of a small-business website. You can do a lot with static web pages, but if you need your site to have robust interactions with users through server-side code, you should consider the other options discussed in this article.

Hosting a static website with Cloud Storage

To host a static site in Cloud Storage, you need to create a Cloud Storage bucket, upload the content, and test your new site. You can serve your data directly from storage.googleapis.com, or you can verify that you own your domain and use your domain name. Either way, you'll get consistent, fast delivery from global edge caches.

You can create your static web pages however you choose. For example, you could hand-author pages by using HTML and CSS. You can use a static-site generator, such as Jekyll, Ghost, or Hugo, to create the content. Static-site generators make it easier for you to create a static website by letting you author in markdown, and providing templates and tools. Site generators generally provide a local web server that you can use to preview your content.

After your static site is working, you can update the static pages by using any process you like. That process could be as straightforward as hand-copying an updated page to the bucket. You might choose to use a more automated approach, such as storing your content on GitHub and then using a webhook to run a script that updates the bucket. An even more advanced system might use a continuous-integration /continuous-delivery (CI/CD) tool, such as Jenkins, to update the content in the bucket. Jenkins has a Cloud Storage plug-in that provides a Google Cloud Storage Uploader post-build step to publish build artifacts to Cloud Storage.

If you have a web application that needs to serve static content or user-uploaded static media, using Cloud Storage can be a cost-effective and efficient way to host and serve this content, while reducing the amount of dynamic requests to your web application.

Additionally, Cloud Storage can directly accept user-submitted content. This feature lets users upload large media files directly and in a secure manner, without proxying through your servers.

To get the best performance from your static website, see Best Practices for Google Cloud Storage.

For more information, see the following pages:

Hosting a static website with Firebase Hosting

Firebase Hosting provides fast and secure static hosting for your web app. With Firebase Hosting, you can quickly and easily deploy web apps and static content to a global content-delivery network (CDN) by using a single command.

Here are some benefits you get when you use Firebase Hosting:

  • Zero-configuration SSL is built into Firebase Hosting so content is always delivered securely. Provisions SSL certificates on custom domains for free.
  • All of your content is served over HTTPS.
  • Your content is delivered to your users quickly from CDN edges around the world.
  • Using the Firebase CLI, you can get your app up and running in seconds. Command line tools make it easy to add deployment targets into your build process.
  • You get release management features, such as atomic deployment of new assets, full versioning, and one-click rollbacks.
  • Hosting offers a configuration useful for single-page apps and other more-app-like sites.
  • Hosting is built to be used seamlessly with other Firebase features.

For more information, see the following pages:

Using virtual machines with Compute Engine

For infrastructure as a service (IaaS) use cases, GCP provides Google Compute Engine. Compute Engine provides a robust computing infrastructure, but you must choose and configure the platform components that you want to use. With Compute Engine, it's your responsibility to configure, administer, and monitor the systems. Google ensures that resources are available, reliable, and ready for you to use, but it's up to you to provision and manage them. The advantage, here, is that you have complete control of the systems and unlimited flexibility.

Use Compute Engine to design and deploy nearly any website-serving system you want. You can use VMs, called instances, to build your application, much like you would if you had your own hardware infrastructure. Compute Engine offers a variety of machine types to customize your configuration to meet your needs and your budget. You can choose which operating systems, development stacks, languages, frameworks, services, and other software technologies you prefer.

Setting up automatically with Cloud Launcher

The easiest way to deploy a complete web-serving stack is by using Google Cloud Launcher. With just a few clicks, you can deploy any of over 100 fully realized solutions with Google Click to Deploy or Bitnami.

Google Cloud Launcher

For example, it's easy to set up a LAMP stack or WordPress with Cloud Launcher. The system deploys a complete, working software stack in just a few minutes on a single instance. Before you deploy, Cloud Launcher shows you cost estimates for running the site, gives you clear information about which versions of the software components it installs for you, and lets you customize your configuration by changing component instance names, choosing the machine type, and choosing a disk size, for example. After you deploy, you have complete control over the Compute Engine instances, their configurations, and the software.

Setting up manually

You can also choose to create your infrastructure on Compute Engine manually, either building your configuration from scratch or building on a Cloud Launcher deployment. For example, you might want to use a specific version of a software component not offered by Cloud Launcher, or perhaps you just prefer to install and configure everything on your own.

Providing a complete framework and best practices for setting up a website is beyond the scope of this article. But from a high-level view, the technical side of setting up a web-serving infrastructure on Compute Engine requires that you:

  • Understand the requirements. If you're building a new website, make sure you understand the components you'll need, such as instances, storage needs, and networking infrastructure. If you're migrating your application from an existing solution, you probably already understand these requirements, but you'll need think through how your existing setup maps to GCP services.
  • Plan the design. Think through your architecture and write down your design. Be as explicit as you can.
  • Create the components. The components that you might usually think of as physical assets, such as computers and network switches, are provided through services in Compute Engine. For example, if you want a computer, you have to create a Compute Engine instance. If you want a persistent hard disk drive, you create that, too. Google Cloud Deployment Manager makes this an easy and repeatable process.
  • Configure and customize. After you have the components you want, you'll need to configure them, install and configure software, and write and deploy any customization code that you require. You can replicate the configuration by running shell scripts, which helps to speed future deployments. Cloud Deployment Manager helps here, too, by providing declarative, flexible configuration templates for automatic deployment of resources. You can also take advantage of IT automation tools such as Puppet and Chef.
  • Deploy the assets. Presumably, you'll have web pages, images, and so on.
  • Test. Verify that everything works as you expect.
  • Deploy to production. Open up your site for the world to see and use.

To help you to get started and understand what it's like to set up Compute Engine instances manually, try one or more of the following tutorials:

Storing data with Compute Engine

Most websites need some kind of storage. You might need storage for a variety of reasons, such as saving files that your users upload, and of course the assets that your site uses.

GCP provides a variety of managed storage services, including:

  • A SQL database in Cloud SQL, which is based on MySQL.
  • Two options for NoSQL data storage: Cloud Datastore and Cloud Bigtable.
  • Consistent, scalable, large-capacity object storage in Cloud Storage. Cloud Storage comes in several flavors:
    • Multi-Regional provides maximum availability and geo-redundancy.
    • Regional provides maximum availability and a localized storage location.
    • Nearline provides a low-cost choice ideal for data accessed less than once a month.
    • Coldline provides the lowest-cost choice for archiving, backup, and disaster recovery.
  • Persistent disks on Compute Engine for use as primary storage for your instances. Compute Engine offers both hard-disk-based persistent disks, called standard persistent disks, and solid-state persistent disks (SSD). You can also choose to set up your preferred storage technology on Compute Engine by using persistent disks. For example, you can set up PostgreSQL as your SQL database or MongoDB as your NoSQL storage. To understand the full range and benefits of storage services on GCP, see Choosing a Storage Option.

Load balancing with Compute Engine

For any website that operates at scale, using load-balancing technologies to distribute the workload among servers is often a requirement. You have a variety of options when architecting your load-balanced web servers on Compute Engine, including:

  • HTTP(S) load balancing. Explains the fundamentals of using the Compute Engine load balancer.
  • Network load balancing. Shows a basic scenario that sets up a layer 3 load balancing configuration to distribute HTTP traffic across healthy instances.
  • Content-based load balancing. Demonstrates how to distribute traffic to different instances based on the incoming URL.
  • Cross-region load balancing. Demonstrates configuring Compute Engine instances in different regions and using HTTP and HTTPS load balancing to distribute traffic across the regions.
  • HTTP(S) load balancing using NGINX. Walks you through one possible solution that you could use in place of the Compute Engine load balancer.
  • Cross-region load balancing using Microsoft IIS backends. Shows how to use the Compute Engine load balancer to distribute traffic to Microsoft Internet Information Services (IIS) servers.
  • Setting Up Internal Load Balancing You can set up a load balancer that distributes network traffic on a private network that is not exposed to the Internet. Internal load balancing is useful not only for intranet applications where all traffic remains on a private network, but also for complex web applications where a frontend sends requests to backend servers by using a private network.
  • Autoscaled internal load balancing using HAProxy and Consul. Extends the previous configuration to support autoscaling both the HAProxy load balancing tier and the backend server tier.

Autoscaling with Compute Engine

You can set up your architecture to enable it to add and remove servers as demand varies. This approach can help to ensure that your site performs well under peak load, while keeping costs under control during more-typical demand periods. Compute Engine provides an autoscaler that you can use for this purpose.

Autoscaling is a feature of managed instance groups. A managed instance group is a pool of homogeneous virtual machine instances, created from a common instance template. An autoscaler adds or remove instances in a managed instance group. Although Compute Engine has both managed and unmanaged instance groups, only managed instance groups can be used with an autoscaler. The Compute Engine documentation contains a complete guide to autoscaling on Compute Engine.

For an in-depth look at what it takes to build a scalable and resilient web-app solution, see Building Scalable and Resilient Web Apps.

Logging and monitoring with Compute Engine

GCP includes features that you can use to keep tabs on what's happening with your website.

Google Stackdriver Logging collects and stores logs from applications and services on GCP. You can view or export logs and integrate third-party logs by using a logging agent.

Stackdriver logging

Google Stackdriver Monitoring provides dashboards and alerts for your site. You configure Stackdriver Monitoring by using the Stackdriver Monitoring Console. You can review performance metrics for cloud services, virtual machines, and common open source servers such as MongoDB, Apache, Nginx, and Elasticsearch. You can use the Stackdriver Monitoring API to retrieve monitoring data and create custom metrics.

Stackdriver monitoring dashboard

Managing DevOps with Compute Engine

For information about managing DevOps with Compute Engine, see the following articles:

Using containers with Kubernetes Engine

You might already be using containers, such as Docker containers. For web serving, containers offer several advantages, including:

  • Componentization. You can use containers to separate the various components of your web application. For example, suppose your site runs a web server and a database. You can run these components in separate containers, modifying and updating one component without affecting the other. As your application's design becomes more complex, containers are a good fit for a service-oriented architecture, including microservices. This kind of design supports scalability, among other goals.
  • Portability. A container has everything it needs to run—your application and its dependencies are bundled together. You can run your containers on a variety of platforms, without worrying about the underlying system details.
  • Rapid deployment. When it's time to deploy, your system is built from a set of definitions and images, so the parts can be deployed quickly, reliably, and automatically. Containers are typically small and deploy much more quickly compared to, for example, virtual machines.

Container computing on GCP offers even more advantages for web serving, including:

  • Orchestration. Kubernetes Engine is a managed service built on Kubernetes, the open source container-orchestration system introduced by Google. With Kubernetes Engine, your code runs in containers that are part of a cluster that is composed of Compute Engine instances. Instead of administering individual containers or creating and shutting down each container manually, you can automatically manage the cluster through Kubernetes Engine, which uses the configuration you define.
  • Image registration. Google Container Registry provides private storage for Docker images on GCP. You can access Container Registry through an HTTPS endpoint, so you can pull images from any machine, whether it's a Compute Engine instance or your own hardware. The registry service hosts your custom images in Cloud Storage under your Cloud Platform Console project. This approach ensures by default that your custom images can only be accessed by members of your project.
  • Mobility. This means that you have the flexibility to move and combine workloads with other cloud providers, or mix cloud computing workloads with on-premises implementations to create a hybrid solution.

Storing data with Kubernetes Engine

Because Kubernetes Engine runs on GCP and uses Compute Engine instances as nodes, your storage options have a lot in common with storage on Compute Engine. You can access Cloud SQL, Cloud Storage, Cloud Datastore, and Cloud Bigtable through their APIs, or you can use another external storage provider if you choose. However, Kubernetes Engine does interact with Compute Engine persistent disks in a different way than a normal Compute Engine instance would.

A Compute Engine instance includes an attached disk. When you use Compute Engine, as long as the instance exists, the disk volume remains with the instance. You can even detach the disk and use it with a different instance. But in a container, on-disk files are ephemeral. When a container restarts, such as after a crash, the on-disk files are lost. Kubernetes solves this issue by using a volume abstraction, and one type of volume is gcePersistentDisk. This means that you can use Compute Engine persistent disks with containers to keep your data files from being deleted when you use Kubernetes Engine.

To understand the features and benefits of a volume, you should first understand a bit about pods. You can think of a pod as an app-specific logical host for one or more containers. A pod runs on a node instance. When containers are members of a pod, they can share several resources, including a set of shared storage volumes. These volumes enable data to survive container restarts and to be shared among the containers within the pod. Of course, you can use a single container and volume in a pod, too, but the pod is a required abstraction to logically connect these resources to each other.

For an example, see the tutorial Using Persistent Disks with WordPress and MySQL.

Load balancing with Kubernetes Engine

Many large web serving architectures need to have multiple servers running that can share the traffic demands. Because Kubernetes Engine enables you to create and manage multiple containers, nodes, and pods so easily, it's a natural fit for a load-balanced web serving system.

Using network load balancing

The easiest way to create a load balancer in Kubernetes Engine is to use Compute Engine's network load balancing. Network load balancing can balance the load of your systems based on incoming Internet Protocol data, such as the address, port, and protocol type. Network load balancing uses forwarding rules. These rules point to target pools that list which instances are available to be used for load balancing.

With network load balancing, you can load balance additional TCP/UDP-based protocols such as SMTP traffic, and your app can directly inspect the packets.

You can deploy network load balancing simply by adding the type: LoadBalancer field to your service configuration file.

Using HTTP(S) load balancing

If you need more advanced load-balancing features, such as HTTPS load balancing, content-based load balancing, or cross-region load balancing, you can integrate your Kubernetes Engine service with Compute Engine's HTTP/HTTPS load balancing feature. Kubernetes provides the Ingress resource that encapsulates a collection of rules for routing external traffic to Kubernetes endpoints. In Kubernetes Engine, an Ingress resource handles provisioning and configuring the Compute Engine HTTP/HTTPS load balancer.

For more information about using HTTP/HTTPS load balancing in Kubernetes Engine, see the documentation.

Scaling with Kubernetes Engine

For automatic resizing of clusters, you can use the Cluster Autoscaler. This feature periodically checks whether there are any pods that are waiting for a node with free resources but are not being scheduled. If such pods exist, then the autoscaler resizes the node pool if resizing would allow the waiting pods to be scheduled.

Cluster Autoscaler also monitors the usage of all nodes. If a node is not needed for an extended period of time, and all of its pods can be easily scheduled elsewhere, then the node is deleted.

For more information about the Cluster Autoscaler, its limitations, and best practices, see the Cluster Autoscaler documentation.

Logging and monitoring with Kubernetes Engine

Like on Compute Engine, Stackdriver Logging and Stackdriver Monitoring provide your logging and monitoring services. Stackdriver Logging collects and stores logs from applications and services. You can view or export logs and integrate third-party logs by using a logging agent.

Stackdriver Monitoring provides dashboards and alerts for your site. You configure Stackdriver Monitoring by using the Stackdriver Monitoring Console. You can review performance metrics for cloud services, virtual machines, and common open source servers such as MongoDB, Apache, Nginx, and Elasticsearch. You can use the Stackdriver Monitoring API to retrieve monitoring data and create custom metrics.

Managing DevOps with Kubernetes Engine

When you use Kubernetes Engine, you're already getting many of the benefits most people think of when they think of DevOps. This is especially true when it comes to ease of packaging, deployment, and management. For your CI/CD workflow needs, you can take advantage of popular tools such as Jenkins. See the following articles:

Building on a managed platform with App Engine

On GCP, the managed platform as a service (PaaS) is called Google App Engine. When you build your website on App Engine, you get to focus on coding up your features and let Google worry about managing the supporting infrastructure. App Engine provides a wide range of features that make scalability, load balancing, logging, monitoring, and security much easier than if you had to build and manage them yourself. App Engine lets you code in a variety of programming languages, and it can easily make use of a variety of other GCP services.

App Engine provides the standard environment, which lets you run applications in a secure, sandboxed environment. The App Engine standard environment distributes requests across multiple servers, and scales servers to meet traffic demands. Your application runs in its own secure, reliable environment that is independent of the hardware, operating system, or physical location of the server. For a sample architecture for a web application running in the standard environment, see Architecture: Web Application on Google App Engine.

Web application uses App Engine and other components

To give you more options, App Engine offers the flexible environment. When you use the flexible environment, your application runs on configurable Compute Engine instances, but App Engine manages the hosting environment for you. This means that you can use additional runtimes, including custom runtimes, for more programming language choices. You can also take advantage of some of the flexibility that Compute Engine offers, such as choosing from a variety of CPU and memory options.

Programming languages

The App Engine standard environment provides default runtimes for Python 2.7, Java 7 / Servlet 2.5, PHP 5.5.34, and Go. The flexible environment includes native support for Java 8 / Servlet 3.1, Java 7 / Jetty 9, Python 2.7 and Python 3.4, Node.js 4.2.3, and Go. With the flexible environment, you can customize these runtimes or provide your own runtime with a custom Docker image or Dockerfile.

If the programming language you use is a primary concern, you need to decide whether the runtimes provided by App Engine standard meet your requirements. If they don't, you should consider using flexible environment.

Getting started tutorials by language

The following tutorials can help you get started using the App Engine standard environment:

The following tutorials can help you get started using the flexible environment:

Storing data with App Engine

App Engine gives you options for storing your data:

Name Structure Consistency
Cloud Datastore Schemaless Strongly consistent except when performing global queries.
Cloud SQL Relational Strongly consistent.
Cloud Storage Files and their associated metadata Strongly consistent except when performing list operations that get a list of buckets or objects.

You can also use several third-party databases with the standard environment.

For more details about storage in App Engine, see the documentation. After opening the documentation page, you can select your preferred programming language by clicking the language name in the top-right part of the page.

When you use the flexible environment, you can use all of the same storage options as you can with the standard environment, and a wider range of third-party databases as well. For more information about third-party databases in the flexible environment, see Using Third-party Databases.

Load balancing and autoscaling with App Engine

When you build on App Engine, load balancing and autoscaling are automatically managed for you.

Logging and Monitoring with App Engine

In App Engine, requests are logged automatically, and you can view these logs in the Cloud Platform Console. App Engine also works with standard, language-specific libraries that provide logging functionality and forwards the log entries to the logs in the console. For example, in Python you can use standard Python logging module and in Java you can use the java.util.logging.Logger API.

Stackdriver Monitoring provides features for monitoring your App Engine apps. Through the Cloud Platform Console, you can monitor incidents, uptime checks, and other details.

Building content management systems

Serving a website means managing your website assets. Cloud Storage provides a global repository for these assets. One common architecture deploys static content to Cloud Storage and then syncs to Compute Engine to render dynamic pages. Cloud Storage works with many third-party content management systems, such as WordPress, Drupal, and Joomla. Cloud Storage also offers an S3 compatible API, so any system that works with S3 can work with Cloud Storage.

For a look at a sample architecture for a content management system, see Content Management.

Content management system on Google Cloud Platform

What's next

  • Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.

Overvåg dine ressourcer på farten

Få Google Cloud Console-appen for bedre at kunne administrere dine projekter.