Running a basic Apache web server

This tutorial shows you how to use Compute Engine to deploy an Apache webserver on a virtual machine (VM) instance. To learn more about the technologiesthat you can use for web serving onGoogle Cloud, seeChoosing a hosting option onGoogle Cloud.

A VM on Compute Engine can be controlled likeany standard Linux server. Deploy an Apache web server to learn thebasics of running a server on a VM instance.

Prerequisites

Create a Linux VM that allows HTTP traffic by doing the following:

  1. Create a new Linux VM. For more information, seeQuickstart using a LinuxVM.

    1. Optional: While creating a Linux VM, you can select theAllow HTTPtraffic checkbox to open porttcp:80 for traffic andAllow HTTPStraffic checkbox to open porttcp:443. However, if youwant to open a different port,configure the firewallafter creating the VM, and thenconfigure Apache to use the firewall.
  2. Connect to the Linux VM. For more information, seeConnect to theVM instance.

Install Apache

  1. In the Google Cloud console, go to theVM Instances page.

    Go to VM instances

  2. To connect to the Linux VM you just created, clickSSH in the row of theVM.
  3. To update the available packages andinstall theapache2 package, use the system package manager for that operating system.If you followed the Quickstart, this creates an Ubuntu VM. To update anUbuntu VM, run the following command:

    sudo apt update && sudo apt -y install apache2

    After installing Apache, the operating system automatically starts theApache server.

  4. Verify that Apache is running:

    sudo systemctl status apache2
  5. Overwrite the Apache web server default web page:

    echo'<!doctype html><html><body><h1>Hello World!</h1></body></html>'|sudotee/var/www/html/index.html

Test your server

Test that your VM is serving traffic on its external IP.

  1. In the Google Cloud console, go to theVM Instances page.

    Go to VM instances

  2. Copy the external IP for your VM under theExternal IP column.
  3. In a browser, navigate tohttp://[EXTERNAL_IP]. Don't connect usinghttps because this causes the server to return aConnection Refusederror.

You should now see the "Hello World!" page.

Clean up

To avoid incurring charges for the VM after you're done experimenting, deletethe VM. For more information, seeClean up.

Troubleshooting

Receiving aConnection Refused error

If you are seeing aConnection Refused error, it is possible that:

  • Your VM instance is not publicly accessible because your firewall rules ortags are misconfigured in one of the following ways:

    • The VM instance does not have the proper tag that allows Compute Engineto apply the appropriate firewall rules to your instance.
    • Your project does not have a firewall rule that allows traffic to theexternal IP address for your instance.
  • You are trying to access the VM using anhttps address. Check that yourURL ishttp://[EXTERNAL_IP] rather thanhttps://[EXTERNAL_IP].

To ensure that your VM instance has the correct tags:

  1. In the Google Cloud console, go to theVM instances page.

    Go to VM instances

  2. Click the name of the instance that you are trying to connect to.
  3. ClickEdit at the top of the page.
  4. Scroll down toFirewalls, and make sure theAllow HTTP traffic boxis checked. If it is not checked, check it.
  5. Save your changes. This ensures that the correct tags are added to theVM instance.

To ensure that the correct firewall rule exists:

  1. In the Google Cloud console, go to theFirewall rules page.

    Go to Firewall rules

  2. Look for a firewall rule that allows all IP ranges through tcp:80. Usually,this rule is named thedefault-allow-http rule.
  3. If a rule does not exist, create one.
    1. ClickCreate firewall rule.
    2. Enter a name for the rule, such asdefault-allow-http.
    3. UnderSource IP ranges, enter0.0.0.0/0 to allow traffic fromall sources.
    4. UnderProtocols and ports, checkSpecified protocols and portsand entertcp:80.
    5. Create your firewall rule.

Test your server again by going to the external IP address of the instance:

http://[EXTERNAL_IP]

What's next

Learn how tohost a website on Compute Engine.

Learn how toset up LAMP on Compute Engine.

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how Compute Engine performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Try Compute Engine free

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-09 UTC.