| 1 | import { OneLineInstallWget } from '@site/src/components/OneLineInstall/' |
| 2 | |
| 3 | # LAMP stack monitoring with Netdata |
| 4 | |
| 5 | Set up robust LAMP stack monitoring (Linux, Apache, MySQL, PHP) in a few minutes using Netdata. |
| 6 | |
| 7 | The LAMP stack is the "hello world" for deploying dynamic web applications. It's fast, flexible, and reliable, which |
| 8 | means a developer or sysadmin won't go far in their career without interacting with the stack and its services. |
| 9 | |
| 10 | _LAMP_ is an acronym of the core services that make up the web application: **L**inux, **A**pache, **M**ySQL, and |
| 11 | **P**HP. |
| 12 | |
| 13 | - [Linux](https://en.wikipedia.org/wiki/Linux) is the operating system running the whole stack. |
| 14 | - [Apache](https://httpd.apache.org/) is a web server that responds to HTTP requests from users and returns web pages. |
| 15 | - [MySQL](https://www.mysql.com/) is a database that stores and returns information based on queries from the web |
| 16 | application. |
| 17 | - [PHP](https://www.php.net/) is a scripting language used to query the MySQL database and build new pages. |
| 18 | |
| 19 | LAMP stacks are the foundation for tons of end-user applications, with [Wordpress](https://wordpress.org/) being the |
| 20 | most popular. |
| 21 | |
| 22 | ## Challenge |
| 23 | |
| 24 | You've already deployed a LAMP stack, either in testing or production. You want to monitor every service's performance |
| 25 | and availability to ensure the best possible experience for your end-users. You might also be particularly interested in |
| 26 | using a free, open-source monitoring tool. |
| 27 | |
| 28 | Depending on your monitoring experience, you may not even know what metrics you're looking for, much less how to build |
| 29 | dashboards using a query language. You need a robust monitoring experience that has the metrics you need without a ton |
| 30 | of required setup. |
| 31 | |
| 32 | ## Solution |
| 33 | |
| 34 | In this tutorial, you'll set up robust LAMP stack monitoring with Netdata in just a few minutes. When you're done, |
| 35 | you'll have one dashboard to monitor every part of your web application, including each essential LAMP stack service. |
| 36 | |
| 37 | This dashboard updates every second with new metrics, and pairs those metrics up with preconfigured alerts to keep you |
| 38 | informed of any errors or odd behavior. |
| 39 | |
| 40 | ## What you need to get started |
| 41 | |
| 42 | To follow this tutorial, you need: |
| 43 | |
| 44 | - A physical or virtual Linux system, which we'll call a _node_. |
| 45 | - A functional LAMP stack. There's plenty of tutorials for installing a LAMP stack, like [this |
| 46 | one](https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04) |
| 47 | from Digital Ocean. |
| 48 | - Optionally, a [Netdata Cloud](https://app.netdata.cloud/sign-up?cloudRoute=/spaces) account, which you can use to view |
| 49 | metrics from multiple nodes in one dashboard, and a whole lot more, for free. |
| 50 | |
| 51 | ## Install the Netdata Agent |
| 52 | |
| 53 | If you don't have the free, open-source Netdata monitoring agent installed on your node yet, get started with a [single |
| 54 | kickstart command](/packaging/installer/README.md): |
| 55 | |
| 56 | <OneLineInstallWget/> |
| 57 | |
| 58 | The Netdata Agent is now collecting metrics from your node every second. You don't need to jump into the dashboard yet, |
| 59 | but if you're curious, open your favorite browser and navigate to `http://localhost:19999` or `http://NODE:19999`, |
| 60 | replacing `NODE` with the hostname or IP address of your system. |
| 61 | |
| 62 | ## Enable hardware and Linux system monitoring |
| 63 | |
| 64 | There's nothing you need to do to enable system monitoring and Linux monitoring with |
| 65 | the Netdata Agent, which autodetects metrics from CPUs, memory, disks, networking devices, and Linux processes like |
| 66 | systemd without any configuration. If you're using containers, Netdata automatically collects resource utilization |
| 67 | metrics from each using the [cgroups data collector](/src/collectors/cgroups.plugin/README.md). |
| 68 | |
| 69 | ## Enable Apache monitoring |
| 70 | |
| 71 | Let's begin by configuring Apache to work with Netdata's [Apache data |
| 72 | collector](/src/go/plugin/go.d/collector/apache/README.md). |
| 73 | |
| 74 | Actually, there's nothing for you to do to enable Apache monitoring with Netdata. |
| 75 | |
| 76 | Apache comes with `mod_status` enabled by default these days, and Netdata is smart enough to look for metrics at that |
| 77 | endpoint without you configuring it. Netdata is already collecting [`mod_status` |
| 78 | metrics](https://httpd.apache.org/docs/2.4/mod/mod_status.html), which is just _part_ of your web server monitoring. |
| 79 | |
| 80 | ## Enable web log monitoring |
| 81 | |
| 82 | The Netdata Agent also comes with a [web log |
| 83 | collector](/src/go/plugin/go.d/collector/weblog/README.md), which reads Apache's access |
| 84 | log file, processes each line, and converts them into per-second metrics. On Debian systems, it reads the file at |
| 85 | `/var/log/apache2/access.log`. |
| 86 | |
| 87 | At installation, the Netdata Agent adds itself to the [`adm` |
| 88 | group](https://wiki.debian.org/SystemGroups#Groups_without_an_associated_user), which gives the `netdata` process the |
| 89 | right privileges to read Apache's log files. In other words, you don't need to do anything to enable Apache web log |
| 90 | monitoring. |
| 91 | |
| 92 | ## Enable MySQL monitoring |
| 93 | |
| 94 | Because your MySQL database is password-protected, you do need to tell MySQL to allow the `netdata` user to connect to |
| 95 | without a password. Netdata's [MySQL data |
| 96 | collector](/src/go/plugin/go.d/collector/mysql/README.md) collects metrics in _read-only_ |
| 97 | mode, without being able to alter or affect operations in any way. |
| 98 | |
| 99 | First, log into the MySQL shell. Then, run the following three commands, one at a time: |
| 100 | |
| 101 | ```mysql |
| 102 | CREATE USER 'netdata'@'localhost'; |
| 103 | GRANT USAGE, REPLICATION CLIENT, PROCESS ON *.* TO 'netdata'@'localhost'; |
| 104 | FLUSH PRIVILEGES; |
| 105 | ``` |
| 106 | |
| 107 | Run `sudo systemctl restart netdata`, or the [appropriate alternative for your system](/docs/netdata-agent/start-stop-restart.md), to collect dozens of metrics every second for robust MySQL monitoring. |
| 108 | |
| 109 | ## Enable PHP monitoring |
| 110 | |
| 111 | Unlike Apache or MySQL, PHP isn't a service that you can monitor directly, unless you instrument a PHP-based application |
| 112 | with [StatsD](/src/collectors/statsd.plugin/README.md). |
| 113 | |
| 114 | However, if you use [PHP-FPM](https://php-fpm.org/) in your LAMP stack, you can monitor that process with our [PHP-FPM |
| 115 | data collector](/src/go/plugin/go.d/collector/phpfpm/README.md). |
| 116 | |
| 117 | Open your PHP-FPM configuration for editing, replacing `7.4` with your version of PHP: |
| 118 | |
| 119 | ```bash |
| 120 | sudo nano /etc/php/7.4/fpm/pool.d/www.conf |
| 121 | ``` |
| 122 | |
| 123 | > Not sure what version of PHP you're using? Run `php -v`. |
| 124 | |
| 125 | Find the line that reads `;pm.status_path = /status` and remove the `;` so it looks like this: |
| 126 | |
| 127 | ```text |
| 128 | pm.status_path = /status |
| 129 | ``` |
| 130 | |
| 131 | Next, add a new `/status` endpoint to Apache. Open the Apache configuration file you're using for your LAMP stack. |
| 132 | |
| 133 | ```bash |
| 134 | sudo nano /etc/apache2/sites-available/your_lamp_stack.conf |
| 135 | ``` |
| 136 | |
| 137 | Add the following to the end of the file, again replacing `7.4` with your version of PHP: |
| 138 | |
| 139 | ```apache |
| 140 | ProxyPass "/status" "unix:/run/php/php7.4-fpm.sock|fcgi://localhost" |
| 141 | ``` |
| 142 | |
| 143 | Save and close the file. Finally, restart the PHP-FPM, Apache, and Netdata processes. |
| 144 | |
| 145 | ```bash |
| 146 | sudo systemctl restart php7.4-fpm.service |
| 147 | sudo systemctl restart apache2 |
| 148 | sudo systemctl restart netdata |
| 149 | ``` |
| 150 | |
| 151 | As the Netdata Agent starts up again, it automatically connects to the new `127.0.0.1/status` page and collects |
| 152 | per-second PHP-FPM metrics to get you started with PHP monitoring. |
| 153 | |
| 154 | ## View LAMP stack metrics |
| 155 | |
| 156 | If the Netdata Agent isn't already open in your browser, open a new tab and navigate to `http://localhost:19999` or |
| 157 | `http://NODE:19999`, replacing `NODE` with the hostname or IP address of your system. |
| 158 | |
| 159 | > If you [signed up](https://app.netdata.cloud/sign-up?cloudRoute=/spaces) for Netdata Cloud earlier, you can also view |
| 160 | > the exact same LAMP stack metrics there, plus additional features, like drag-and-drop custom dashboards. Be sure to |
| 161 | > [connecting your node](/src/claim/README.md) to start streaming metrics to your browser through Netdata Cloud. |
| 162 | |
| 163 | Netdata automatically organizes all metrics and charts onto a single page for easy navigation. Peek at gauges to see |
| 164 | overall system performance, then scroll down to see more. Click-and-drag with your mouse to pan _all_ charts back and |
| 165 | forth through different time intervals, or hold `SHIFT` and use the scrollwheel (or two-finger scroll) to zoom in and |
| 166 | out. Check out our doc on [interacting with charts](/docs/dashboards-and-charts/netdata-charts.md) for all the details. |
| 167 | |
| 168 |  |
| 169 | |
| 170 | The **System Overview** section, which you can also see in the right-hand menu, contains key hardware monitoring charts, |
| 171 | including CPU utilization, memory page faults, network monitoring, and much more. The **Applications** section shows you |
| 172 | exactly which Linux processes are using the most system resources. |
| 173 | |
| 174 | Next, let's check out LAMP-specific metrics. You should see four relevant sections: **Apache local**, **MySQL local**, |
| 175 | **PHP-FPM local**, and **web log apache**. Click on any of these to see metrics from each service in your LAMP stack. |
| 176 | |
| 177 |  |
| 179 | |
| 180 | ### Key LAMP stack monitoring charts |
| 181 | |
| 182 | Here's a quick reference for what charts you might want to focus on after setting up Netdata. |
| 183 | |
| 184 | | Chart name / context | Type | Why? | |
| 185 | |-------------------------------------------------------|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 186 | | System Load Average (`system.load`) | Hardware monitoring | A good baseline load average is `0.7`, while `1` (on a 1-core system, `2` on a 2-core system, and so on) means resources are "perfectly" utilized. Higher load indicates a bottleneck somewhere in your system. | |
| 187 | | System RAM (`system.ram`) | Hardware monitoring | Look at the `free` dimension. If that drops to `0`, your system will use swap memory and slow down. | |
| 188 | | Uptime (`apache_local.uptime`) | Apache monitoring | This chart should always be "climbing," indicating a continuous uptime. Investigate any drops back to `0`. | |
| 189 | | Requests By Type (`web_log_apache.requests_by_type`) | Apache monitoring | Check for increases in the `error` or `bad` dimensions, which could indicate users arriving at broken pages or PHP returning errors. | |
| 190 | | Queries (`mysql_local.queries`) | MySQL monitoring | Queries is the total number of queries (queries per second, QPS). Check this chart for sudden spikes or drops, which indicate either increases in traffic/demand or bottlenecks in hardware performance. | |
| 191 | | Active Connections (`mysql_local.connections_active`) | MySQL monitoring | If the `active` dimension nears the `limit`, your MySQL database will bottleneck responses. | |
| 192 | | Performance (phpfpm_local.performance) | PHP monitoring | The `slow requests` dimension lets you know if any requests exceed the configured `request_slowlog_timeout`. If so, users might be having a less-than-ideal experience. | |
| 193 | |
| 194 | ## Get alerts for LAMP stack errors |
| 195 | |
| 196 | The Netdata Agent comes with hundreds of pre-configured alerts to help you keep tabs on your system, including 19 alerts |
| 197 | designed for smarter LAMP stack monitoring. |
| 198 | |
| 199 | Click the 🔔 icon in the top navigation to [see active alerts](/docs/dashboards-and-charts/alerts-tab.md). The **Active** tabs |
| 200 | shows any alerts currently triggered, while the **All** tab displays a list of _every_ pre-configured alert. The |
| 201 | |
| 202 |  |
| 204 | |
| 205 | [Tweak alerts](/src/health/REFERENCE.md) based on your infrastructure monitoring needs, and to see these alerts |
| 206 | in other places, like your inbox or a Slack channel, [enable a notification |
| 207 | method](/docs/alerts-and-notifications/notifications/README.md). |
| 208 | |
| 209 | ## What's next? |
| 210 | |
| 211 | You've now set up robust monitoring for your entire LAMP stack: Linux, Apache, MySQL, and PHP (-FPM, to be exact). These |
| 212 | metrics will help you keep tabs on the performance and availability of your web application and all its essential |
| 213 | services. The per-second metrics granularity means you have the most accurate information possible for troubleshooting |
| 214 | any LAMP-related issues. |
| 215 | |
| 216 | Another powerful way to monitor the availability of a LAMP stack is the [`httpcheck` |
| 217 | collector](/src/go/plugin/go.d/collector/httpcheck/README.md), which pings a web server at |
| 218 | a regular interval and tells you whether if and how quickly it's responding. The `response_match` option also lets you |
| 219 | monitor when the web server's response isn't what you expect it to be, which might happen if PHP-FPM crashes, for |
| 220 | example. |
| 221 | |
| 222 | The best way to use the `httpcheck` collector is from a separate node from the one running your LAMP stack, which is why |
| 223 | we're not covering it here, but it _does_ work in a single-node setup. Just don't expect it to tell you if your whole |
| 224 | node crashed. |
| 225 | |
| 226 | If you're planning on managing more than one node, or want to take advantage of advanced features, like finding the |
| 227 | source of issues faster with [Metric Correlations](/docs/metric-correlations.md), |
| 228 | [sign up](https://app.netdata.cloud/sign-up?cloudRoute=/spaces) for a free Netdata Cloud account. |
| 229 | |
| 230 | ### Related reference documentation |
| 231 | |
| 232 | - [Netdata Agent · Get started](/packaging/installer/README.md) |
| 233 | - [Netdata Agent · Apache data collector](/src/go/plugin/go.d/collector/apache/README.md) |
| 234 | - [Netdata Agent · Web log collector](/src/go/plugin/go.d/collector/weblog/README.md) |
| 235 | - [Netdata Agent · MySQL data collector](/src/go/plugin/go.d/collector/mysql/README.md) |
| 236 | - [Netdata Agent · PHP-FPM data collector](/src/go/plugin/go.d/collector/phpfpm/README.md) |
| 237 |