This tutorial shows you how to use Compute Engine to deploy an Apache web server on a virtual machine (VM) instance. To learn more about the technologies that you can use for web serving on Google Cloud, see Choosing a hosting option on Google Cloud.
A VM on Compute Engine can be controlled like any standard Linux server. Deploy an Apache web server to learn the basics of running a server on a VM instance.
Create a Linux VM that allows HTTP traffic by doing the following:
Create a new Linux VM. For more information, see Quickstart using a Linux VM.
- Optional: While creating a Linux VM, you can select the Allow HTTP
traffic checkbox to open port
tcp:80for traffic and Allow HTTPS traffic checkbox to open port
tcp:443. However, if you want to open a different port, configure the firewall after creating the VM, and then configure Apache to use the firewall.
- Optional: While creating a Linux VM, you can select the Allow HTTP traffic checkbox to open port
Connect to the Linux VM. For more information, see Connect to the VM instance.
Use the system package manager to update the available packages and install the
apache2package. If you followed the Quickstart, this will be an Ubuntu VM:
sudo apt update && sudo apt -y install apache2
After installing Apache, the operating system automatically starts the Apache server.
Verify that Apache is running:
sudo systemctl status apache2
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 VM is serving traffic on its external IP.
- In the Google Cloud Console, go to the VM Instances page.
- Copy the external IP for your VM under the External IP column.
- In a browser, navigate to
http://[EXTERNAL_IP]. Don't connect using
httpsbecause this causes the server to return a
You should now see the "Hello World!" page.
To avoid incurring charges for the VM after you're done experimenting, delete the VM. For more information, see Clean up.
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
httpsaddress. Check that your URL is
To ensure that your VM instance has the correct tags:
- In the Google Cloud Console, go to the VM instances page.
- Click the name of the instance that you are trying to connect to.
- Click Edit at the top of the page.
- Scroll down to Firewalls, and make sure the Allow HTTP traffic box is checked. If it is not checked, check it.
- Save your changes. This ensures that the correct tags are added to the VM instance.
To ensure that the correct firewall rule exists:
- In the Google Cloud Console, go to the Firewall rules page.
- Look for a firewall rule that allows all IP ranges through tcp:80. Usually,
this rule is named the
- If a rule does not exist, create one.
- Click Create firewall rule.
- Enter a name for the rule, such as
- Under Source IP ranges, enter
0.0.0.0/0to allow traffic from all sources.
- Under Protocols and ports, check Specified protocols and ports
- Create your firewall rule.
Test your server again by going to the external IP address of the instance:
Learn how to host a website on Compute Engine.
Learn how to set 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