Disabling PHP-FPM Access Logs in Kubernetes while Preserving Error Logs


9 views

When running PHP-FPM in Kubernetes with the official php:7.1-fpm Docker image, you'll notice request logs appearing twice:

[php] - - 05/Jul/2020:20:15:55 +0000 "POST /api/bookings" 200

These duplicate logs occur because both PHP-FPM and your reverse proxy (typically Nginx) are logging the same requests. While error logging remains essential for debugging, these access logs become redundant noise in your cluster.

To disable only the request logging while keeping error logs intact, modify your PHP-FPM pool configuration. Here's how to implement this in your Kubernetes deployment:

# Custom php-fpm.conf
[global]
; Error log remains active
error_log = /proc/self/fd/2

; Disable access logging
access.log = /dev/null

; Still capture slow logs if needed
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 5s

For Docker-based deployments using the official PHP-FPM image, create a custom config file and mount it:

# Dockerfile snippet
FROM php:7.1-fpm
COPY custom-php-fpm.conf /usr/local/etc/php-fpm.d/zz-custom.conf

Or for a Kubernetes ConfigMap approach:

# kubectl create configmap php-fpm-config --from-file=custom-php-fpm.conf
apiVersion: v1
kind: ConfigMap
metadata:
  name: php-fpm-config
data:
  custom-php-fpm.conf: |
    [global]
    error_log = /proc/self/fd/2
    access.log = /dev/null

After implementing these changes:

# Check running PHP-FPM processes
kubectl exec -it [php-pod] -- ps aux | grep php-fpm

# Verify logging behavior
kubectl logs [php-pod] | grep "POST /api" | wc -l

You should see error messages when they occur, but the access logs should no longer appear. If you need to temporarily re-enable logging for debugging:

# Quick debug command
kubectl exec -it [php-pod] -- sed -i 's/access.log = .*/access.log = \/proc\/self\/fd\/2/' /usr/local/etc/php-fpm.d/zz-custom.conf
kubectl exec -it [php-pod] -- kill -USR2 1

When running PHP-FPM in containerized environments like Kubernetes, you'll often see duplicate access logs - once from Nginx and again from PHP-FPM. The log format you're seeing:

[php] - - 05/Jul/2020:20:15:55 +0000 "POST /api/bookings" 200

is PHP-FPM's default access logging, which becomes redundant when you already have Nginx logging requests.

There are two primary methods to disable these access logs while maintaining error logging:

1. Using php-fpm.conf

Modify your php-fpm configuration file (typically located at /usr/local/etc/php-fpm.conf or /etc/php/7.1/fpm/php-fpm.conf):

[global]
; Error log should remain enabled
error_log = /proc/self/fd/2

; Disable access log
access.log = /dev/null

2. Docker-specific Solution

For the official php-fpm Docker image, you can override the default configuration:

FROM php:7.1-fpm

# Disable access logging
RUN echo "access.log = /dev/null" >> /usr/local/etc/php-fpm.d/docker.conf \
    && echo "catch_workers_output = yes" >> /usr/local/etc/php-fpm.d/docker.conf

When deploying to Kubernetes, ensure your ConfigMap or deployment includes these changes:

apiVersion: v1
kind: ConfigMap
metadata:
  name: php-fpm-config
data:
  docker.conf: |
    [global]
    error_log = /proc/self/fd/2
    access.log = /proc/self/fd/1
    catch_workers_output = yes

After making changes, verify with:

kubectl exec -it [php-pod] -- /bin/sh -c "ps aux | grep php-fpm"

You should no longer see access logs in your cluster logs, while error messages will still appear.

If you want to keep some logging but reduce verbosity:

; In php-fpm.conf
log_level = error