Running a basic Apache web server

A virtual machine instance on Google Compute Engine can be controlled like any standard Linux server. Deploy a simple Apache web server to learn the basics of running a server on a virtual machine instance.


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

  1. Follow the Quickstart instructions to create a new Linux instance.

  2. Optionally, when you follow the Quickstart to create a new Linux instance, you Allow HTTP traffic, which opens tcp:80 for traffic. However, if you want to open a different port, configure the firewall, and then configure Apache to use the firewall.

  3. Connect to the new Linux instance. For more information, see Connect to your instance.

Install Apache

  1. Use the Debian package manager to install the apache2 package:

    sudo apt update && sudo apt -y install apache2

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

  2. Overwrite the Apache web server default web page with the following command:

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

Test your server

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

  1. In the Google Cloud Console, go to the VM Instances page.

    Go to VM instances

  2. Copy the external IP for your instance under the External IP column.
  3. In a browser, navigate to http://[EXTERNAL_IP]. Do not connect using https, as the server will return a Connection Refused error.

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

Clean up

When you're done experimenting, follow the clean up instructions in the Quickstart to avoid incurring charges for the virtual machine instance.


Receiving a Connection Refused error

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

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

    • The VM instance does not have the proper tag that allows Compute Engine to apply the appropriate firewall rules to your instance.
    • Your project does not have a firewall rule that allows traffic to the external IP address for your instance.
  • You are trying to access the VM using an https address. Check that your URL is http://[EXTERNAL_IP] rather than https://[EXTERNAL_IP].

To ensure that your VM instance has the correct tags:

  1. In the Google Cloud Console, go to the VM instances page.

    Go to VM instances

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

To ensure that the correct firewall rule exists:

  1. In the Google Cloud Console, go to the Firewall 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 the default-allow-http rule.
  3. If a rule does not exist, create one.
    1. Click Create firewall rule.
    2. Enter a name for the rule, such as default-allow-http.
    3. Under Source IP ranges, enter to allow traffic from all sources.
    4. Under Protocols and ports, check Specified protocols and ports and enter tcp:80.
    5. Create your firewall rule.

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


What's next

Learn how to host a website on Google Compute Engine.

Learn how to set up LAMP on Google 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