lamp-stack.txt 14 KB

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