Use Docker Compose, Nginx and MariaDB to implement monitoring and log management of PHP applications
When developing and operating web applications, monitoring and log management are very important important part. Using Docker Compose, Nginx and MariaDB, we can implement a complete monitoring and log management solution.
First, we need to prepare a PHP application and write it as a Docker image. This PHP application can be a simple static page or a dynamic application containing PHP code. No matter which one it is, we need to package it into a Docker image.
Next, we need to write a Docker Compose file to define the entire environment of our application. In this file, we need to define three services: Nginx, PHP and MariaDB.
version: '3' services: web: image: nginx:latest ports: - 80:80 volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf - ./logs:/var/log/nginx php: image: your_php_image:latest volumes: - ./php:/var/www/html links: - db db: image: mariadb:latest environment: - MYSQL_ROOT_PASSWORD=your_password volumes: - ./data:/var/lib/mariadb
In the above configuration, we defined three services: web, php and db. The web service uses the Nginx image and binds port 80 of the host to port 80 in the container. We also specified the mounting path of the Nginx configuration file nginx.conf and the log file directory logs.
The php service uses our own packaged PHP image, and mounts the php directory in the host to the /var/www/html directory in the container, so that our PHP application can access this directory. files in .
The db service uses the MariaDB mirror and sets a ROOT user password. We also mounted a data directory for persistent storage of database data.
Next, we need to prepare an Nginx configuration file nginx.conf for configuring Nginx’s virtual host and log management. Here is a simple example:
server { listen 80; server_name localhost; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { root /var/www/html; index index.html index.php; } location ~ .php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; include fastcgi_params; } }
The above configuration file defines a virtual host named localhost, which listens on port 80. We configured the access log and error log to the /var/log/nginx directory respectively.
Next, we need to write a script to parse and store the logs. We can write a simple script using PHP to achieve this function. Here is an example:
<?php function save_log($log_file, $log_data) { // 解析日志数据 // 存储到数据库中 } $log_file = '/var/log/nginx/access.log'; $handle = fopen($log_file, "r"); if ($handle) { while (($line = fgets($handle)) !== false) { // 解析日志行,获取需要存储的数据 $log_data = parse_log($line); // 存储日志数据 save_log($log_file, $log_data); } fclose($handle); } ?>
In the above script, we use a simple loop to read the log file line by line and store the parsed log data into the database. The specific parsing and storage logic needs to be written according to the actual situation.
Finally, we need to add this script to our PHP application to perform log parsing and storage operations regularly. We can use crontab to implement scheduled execution. Here is an example:
# 每分钟执行一次日志解析脚本 * * * * * php /var/www/html/log_parser.php
Add the above configuration to crontab to execute the log_parser.php script every minute.
To sum up, using Docker Compose, Nginx and MariaDB, we can easily implement monitoring and log management of PHP applications. By configuring Nginx's virtual host and log management, we can store access logs and error logs in specified directories. Then, by writing a script that parses the logs and adding it to the PHP application, we can parse and store this log data on a regular basis. This way, we can easily monitor and troubleshoot the application.
The above is the detailed content of Monitoring and log management of PHP applications using Docker Compose, Nginx and MariaDB. For more information, please follow other related articles on the PHP Chinese website!