Learn how to collect and monitor metrics from an Apache web server installed on a Compute Engine virtual machine (VM) instance by using the Ops Agent:
To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:
Security constraints defined by your organization might prevent you from completing the following steps. For troubleshooting information, see Develop applications in a constrained Google Cloud environment.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, Cloud Monitoring, Cloud Logging, and OS Config APIs.
Roles required to enable APIs
To enable APIs, you need the serviceusage.services.enable permission. If you
created the project, then you likely already have this permission through the
Owner role (roles/owner). Otherwise, you can get this permission through the
Service Usage Admin role (roles/serviceusage.serviceUsageAdmin).
Learn how to grant roles.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, Cloud Monitoring, Cloud Logging, and OS Config APIs.
Roles required to enable APIs
To enable APIs, you need the serviceusage.services.enable permission. If you
created the project, then you likely already have this permission through the
Owner role (roles/owner). Otherwise, you can get this permission through the
Service Usage Admin role (roles/serviceusage.serviceUsageAdmin).
Learn how to grant roles.
To get the permissions that you need to create a Compute Engine instance, install Apache, and to view metric data, ask your administrator to grant you the following IAM roles on your project:
roles/compute.instanceAdmin.v1)roles/monitoring.editor)roles/osconfig.osPolicyAssignmentAdmin)roles/iam.serviceAccountUser)roles/serviceusage.serviceUsageAdmin)For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
In the Google Cloud console, go to the VM instances page:
If you use the search bar to find this page, then select the result whose subheading is Compute Engine.
To install an Apache web server on your Compute Engine VM instance, do the following:
On the VM instances page, locate your new VM, go to the Connect column, and then click SSH.
Having trouble connecting? Refer to Troubleshooting SSH.
To update the package lists, copy the following command to your clipboard, paste the command into the SSH terminal, and then press enter:
sudo apt-get update
After you see the message "Reading package lists... Done", in the SSH terminal, run the following command to install an Apache2 web server:
sudo apt-get install apache2 php7.0
When asked to continue the installation, enter Y. If the
install command fails, then use sudo apt-get install apache2 php.
When your command prompt returns, go to the VM instances page and copy the VM's external IP address into the following URL:
http://EXTERNAL_IP
To connect to your Apache web server, open a new browser tab, and then enter the URL from the previous step.
When the web server is successfully installed, the browser tab displays the Apache2 Debian default page.
Go to the SSH terminal for your VM instance. If you don't have a terminal open, then do the following:
In the Google Cloud console, go to the VM instances page:
If you use the search bar to find this page, then select the result whose subheading is Compute Engine.
Copy the following command, then paste it into the terminal for your instance, and then press enter:
The previous command creates the configuration to collect and ingest logs and metrics from the Apache web server. For more information, see Configure the Ops Agent for Apache web server.
sudo systemctl restart google-cloud-ops-agent
sudo systemctl status "google-cloud-ops-agent*"
To generate traffic to your Apache web server, do the following:
Go to the SSH terminal for your VM instance. If you don't have a terminal open, then do the following:
In the Google Cloud console, go to the VM instances page:
If you use the search bar to find this page, then select the result whose subheading is Compute Engine.
In the SSH terminal, run the following command, which generates requests to your Apache web server:
timeout 120 bash -c -- 'while true; do curl localhost; sleep $((RANDOM % 4)) ; done'To view the Apache Overview dashboard, which is automatically created, do the following:
In the Google Cloud console, go to the Dashboards page:
If you use the search bar to find this page, then select the result whose subheading is Monitoring.
In the My Dashboards pane, select the Apache Overview dashboard from the list.
You've configured the Ops Agent to collect logs and metrics from your Apache web server, and you've viewed the metrics. The next step is to create an alerting policy so that you're notified when load on your Apache web server exceeds a threshold.
In the Google Cloud console, go to the notifications Alerting page:
If you use the search bar to find this page, then select the result whose subheading is Monitoring.
My email, and then
click Save.In this section, you create an alerting policy so that you are notified when the traffic to your Apache web server exceeds a defined threshold:
In the Google Cloud console, go to the notifications Alerting page:
If you use the search bar to find this page, then select the result whose subheading is Monitoring.
Select the time series to be monitored:
The chart for Apache traffic is shown.
Advance to the Configure trigger fields, and then
set the Threshold value field to 1500.
The chart displays the threshold as a dashed line. Ensure that the dashed line is much less than the peak traffic level.
Advance to the Notifications and name fields, and then use the Notification channels menu to select your email address.
For the policy name, enter Apache traffic above threshold.
Advance to the Review alert fields, review the alerting policy, and then click Create policy.
To test the alerting policy, generate traffic that exceeds the threshold:
Go to the SSH terminal for your VM instance. If you don't have a terminal open, then do the following:
In the Google Cloud console, go to the VM instances page:
If you use the search bar to find this page, then select the result whose subheading is Compute Engine.
In the SSH terminal, run the following command, which generates requests to your Apache web server:
timeout 120 bash -c -- 'while true; do curl localhost; sleep $((RANDOM % 4)) ; done'When the command prompt returns, check your email for a message whose
subject line begins with [ALERT].
If you don't see an email, then examine the chart on the alerting policy to verify that the traffic level exceeded the threshold. You might need to run the previous command again. Otherwise, wait a minute or two for the email to arrive.
The notification provides a summary of the alerting policy and a link to an incident. Each incident contains a record of the failure, and these are typically helpful when troubleshooting.
You've configured the Ops Agent to collect logs and metrics from your Apache web server, but you've only viewed metrics. For information about how to view Apache web server logs, see the quickstart View Apache web server logs.
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
If you created a new project and you no longer need the project, then delete the project.
If you used an existing project, then do the following:
If you created a VM, then delete it:
Delete the alerting policy that you created:
In the Google Cloud console, go to the notifications Alerting page:
If you use the search bar to find this page, then select the result whose subheading is Monitoring.
To learn about the Ops Agent and supported integrations, see:
To learn how to test whether a URL is responding to requests and to simulate a failure, see the quickstart Get notified if your application stops responding.
To view logs from your Apache web server, see the quickstart Cloud Logging for Compute Engine VMs.
To learn about charting and viewing performance data, see Dashboards overview.
For a list of metrics supported by Google Cloud, see Metrics list.
To create your own Monitoring metrics, see Custom metrics.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-07-17 UTC.