Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to send php errors to std #811

Closed
bentcoder opened this issue Apr 6, 2019 · 2 comments
Closed

Unable to send php errors to std #811

bentcoder opened this issue Apr 6, 2019 · 2 comments
Labels
question Usability question, not directly related to an error with the image

Comments

@bentcoder
Copy link

bentcoder commented Apr 6, 2019

Hi,

I cannot get the php-fpm error logs appear in docker std. So far I've tried many suggestions but for some reason no progress at all. I am posting my whole setup so that someone could kindly help me to right direction. All I see is app_php_1 | 172.21.0.7 - 06/Apr/2019:12:26:37 +0000 "GET /index.php" 500

Thanks

config

PHP-FPM
Dockerfile

    FROM php:7.2.13-fpm-alpine3.8
    
    RUN apk update \
     && apk add --no-cache $PHPIZE_DEPS \
        bash git zip unzip
    
    RUN docker-php-ext-install opcache
    RUN docker-php-ext-enable opcache
    
    RUN rm -rf /var/cache/apk/*
    
    COPY php.ini /usr/local/etc/php/conf.d/php.override.ini
    COPY www.conf /usr/local/etc/php-fpm.d/www.conf
    
    WORKDIR /app
    
    CMD ["php-fpm", "--nodaemonize"]

php.ini

    [php]
    date.timezone=UTC
    log_errors=On
    error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT
    display_errors=Off
    max_execution_time=60
    memory_limit=256M
    
    [opcache]
    opcache.enable_cli=1
    opcache.memory_consumption=256
    opcache.max_accelerated_files=20000
    realpath_cache_size=4096K
    realpath_cache_ttl=600

www.conf

    [global]
    daemonize=no
    
    [www]
    user=www-data
    group=www-data
    
    listen=app_nginx:9000
    
    pm=dynamic
    pm.max_children=30
    pm.start_servers=2
    pm.min_spare_servers=2
    pm.max_requests=1000

NGINX
Dockerfile

    FROM nginx:1.15.8-alpine
    
    RUN apk add --no-cache bash
    
    RUN rm -rf /var/cache/apk/*
    
    COPY app.conf /etc/nginx/conf.d/default.conf
    COPY nginx.conf /etc/nginx/nginx.conf
    COPY app_ssl.crt /etc/ssl/certs/app_ssl.crt
    COPY app_ssl.key /etc/ssl/private/app_ssl.key

app.conf

    server {
        listen 80;
    
        server_name localhost;
    
        root /app/public;
    
        listen 443 default_server ssl;
        ssl_certificate /etc/ssl/certs/app_ssl.crt;
        ssl_certificate_key /etc/ssl/private/app_ssl.key;
    
        location / {
            try_files $uri /index.php$is_args$args;
        }
    
        location ~ ^/index\.php(/|$) {
            fastcgi_pass app_php:9000;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_hide_header X-Powered-By;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            internal;
        }
    
        location ~ \.php$ {
            return 404;
        }
    }

nginx.conf

    user nginx;
    
    worker_processes 2;
    
    error_log /var/log/nginx/error.log warn;
    pid /var/run/nginx.pid;
    
    events {
        worker_connections 1024;
    
        use epoll;
    }
    
    http {
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
    
        log_format json_combined escape=json
          '{'
            '"time_local":"$time_local",'
            '"remote_addr":"$remote_addr",'
            '"remote_user":"$remote_user",'
            '"request":"$request",'
            '"status": "$status",'
            '"body_bytes_sent":"$body_bytes_sent",'
            '"request_time":"$request_time",'
            '"http_referrer":"$http_referer",'
            '"http_user_agent":"$http_user_agent"'
          '}';
    
        access_log /var/log/nginx/access.log json_combined;
    
        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
    
        keepalive_timeout 65;
    
        server_tokens off;
    
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection '1; mode=block';
        add_header X-Frame-Options DENY;
        add_header Strict-Transport-Security 'max-age=31536000; includeSubdomains; preload';
        add_header 'Referrer-Policy' 'no-referrer-when-downgrade';
    
        types_hash_max_size 2048;
    
        gzip on;
    
        include /etc/nginx/conf.d/*.conf;
    }


docker-compose.yml

    version: "3"
    
    services:
      app_php:
        build:
          context: "./php"
        volumes:
          - "../..:/app:consistent"
      app_nginx:
        build:
          context: "./nginx"
        ports:
          - "5080:80"
          - "5443:443"
        volumes:
          - "../..:/app:consistent"
        depends_on:
          - "aapp_php"

WHAT I TRIED

I tried these independently from each other/file, not all in one go.

  • Changed /proc/self/fd/2 to /dev/stdout//dev/stderr for error_log and access.log properties in /usr/local/etc/php-fpm.d/docker.conf file.

  • Added code below at the bottom of www.conf file.

catch_workers_output = yes
php_flag[display_errors] = off
php_admin_flag[log_errors] = on
php_admin_value[error_log] = /var/log/php-fpm-error.log
php_admin_value[error_log] = /dev/stdout
php_admin_value[error_log] = /dev/stderr
  • Some more but I lost track of it.
@wglambert wglambert added the question Usability question, not directly related to an error with the image label Apr 8, 2019
@wglambert
Copy link

wglambert commented Apr 8, 2019

TL;DR use /proc/1/fd/1 (stdout of PID 1), and /proc/1/fd/2 (stderr of PID 1)
(note; php has some sort of oddity with outputting to stdout so just use stderr)


So docker logs reads from /proc/1/fd/ for file descriptors 1 and 2 (stdout and stderr)
(Which correspond to /dev/pts/0, IE: the pseudo-terminal for the process that has PID 1)

The CMD is PID 1 and any argument given after the image (IE: sh) will replace the CMD and become PID 1 in its place. If you then use -i and -t then you are "attached" to the CMD and docker logs will get all stdout/stderr you produce as your current shell is also PID 1.
(docker run -it [image] sh in one terminal and then docker logs -f in another)

When detached /dev/std{out,err} are redirected to /proc/self/fd/{1,2} which redirects to /dev/pts/1, the pseudo-terminal of your current terminal (whose stdout/stderr docker logs won't read as it's not pts 0)

The redirect chain:

$ docker run -dit --rm --name php php:7.2-fpm-alpine
fc3a3751c6e37a1518ce1b692e25cf81869ed1d9e98fd6a7635007306d93f75f

$ docker exec -it php sh
# ls -al /dev/ | grep std
lrwxrwxrwx    1 root     root            15 Apr  8 21:42 stderr -> /proc/self/fd/2
lrwxrwxrwx    1 root     root            15 Apr  8 21:42 stdin -> /proc/self/fd/0
lrwxrwxrwx    1 root     root            15 Apr  8 21:42 stdout -> /proc/self/fd/1

# ls -al /proc/self/fd
lrwx------    1 root     root            64 Apr  8 21:44 0 -> /dev/pts/1
lrwx------    1 root     root            64 Apr  8 21:44 1 -> /dev/pts/1
lrwx------    1 root     root            64 Apr  8 21:44 2 -> /dev/pts/1

Showing the PID's and doing an echo to different stdout's:

$ docker run -d --rm --name php php:7.2-fpm-alpine
72b83fb1167d740951669fab6086bbd3d2084e4088d4ad5a1c8fe96042edbf33

$ docker exec php ps
PID   USER     TIME  COMMAND
    1 root      0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
    6 www-data  0:00 php-fpm: pool www
    7 www-data  0:00 php-fpm: pool www
    8 root      0:00 ps

# echo '/proc/1/fd/2' >/proc/1/fd/2
# echo '/proc/self/fd/1' >/proc/self/fd/1
# exit

$ docker logs php
[08-Apr-2019 21:42:52] NOTICE: fpm is running, pid 1
[08-Apr-2019 21:42:52] NOTICE: ready to handle connections
/proc/1/fd/2

Note that php-fpm: master process is PID 1, so docker logs is only capturing output and error from that specific process.

And then when in "attached" state the shell (or process) that you give after the image name is PID 1:

$ docker run -it --rm --name php php:7.2-fpm-alpine ash
# ps
PID   USER     TIME  COMMAND
    1 root      0:00 ash
    6 root      0:00 ps

# ls -al /proc/self/fd
lrwx------    1 root     root            64 Apr  8 22:03 0 -> /dev/pts/0
lrwx------    1 root     root            64 Apr  8 22:03 1 -> /dev/pts/0
lrwx------    1 root     root            64 Apr  8 22:03 2 -> /dev/pts/0

@drexlma
Copy link

drexlma commented Dec 10, 2022

php-fpm 8.2 has problems with /dev/stderr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usability question, not directly related to an error with the image
Projects
None yet
Development

No branches or pull requests

3 participants