php/apache: Custom php.ini file for apache and cli

Created on 21 Jan 2016  路  14Comments  路  Source: docker-library/php

It would be nice to have a configuration like this, as happens by default on debian/ubuntu/centos.
Anyone knows/tried to implement that ?

Most helpful comment

Just a quick update, i ended up by implementing this solution: https://github.com/sparkfabrik/docker-php-base-image/blob/master/7.0.6/Dockerfile

The specifica part:

RUN mkdir -p /usr/local/etc/php/apache2 && \
    cp -R /usr/local/etc/php/conf.d /usr/local/etc/php/apache2 && \
    echo 'export PHP_INI_SCAN_DIR=/usr/local/etc/php/apache2/conf.d' >> /etc/apache2/envvars && \

It is quite simple, we just copy the entire php configurations in a specific apache2 directory, then using an apache env var to make php read configurations from it, as simple as that.

As you can see xdebug is just installed but not activated, if you need it just create a new xdebug.ini file in /usr/local/etc/php/apache2/conf.d' to enable it just for apache.

All 14 comments

Like this?

Not really.

@paolomainardi can you explain a bit more what your use case is and what you're trying to accomplish so we can understand better why what @mbrevda suggested isn't sufficient?

I'm also looking for this. The use case is so that you can have xdebug enabled for fpm for nginx / apache and then disabled for the cli so that composer can be run without xdebug

Just a quick update, i ended up by implementing this solution: https://github.com/sparkfabrik/docker-php-base-image/blob/master/7.0.6/Dockerfile

The specifica part:

RUN mkdir -p /usr/local/etc/php/apache2 && \
    cp -R /usr/local/etc/php/conf.d /usr/local/etc/php/apache2 && \
    echo 'export PHP_INI_SCAN_DIR=/usr/local/etc/php/apache2/conf.d' >> /etc/apache2/envvars && \

It is quite simple, we just copy the entire php configurations in a specific apache2 directory, then using an apache env var to make php read configurations from it, as simple as that.

As you can see xdebug is just installed but not activated, if you need it just create a new xdebug.ini file in /usr/local/etc/php/apache2/conf.d' to enable it just for apache.

Great, thanks for the pointer - I'll look into the equivalent for nginx

@metadan let me know if you need some help :)

Can someone add this sample for readme and docker hub?

php.ini for apache2

  /etc/php5/apache2/conf.d/php.ini

php.ini for command-line php

 /usr/local/etc/php/php.ini

It's very annoying that the exist sample only works for cli.

@kujiy even the Apache variants should be using /usr/local/etc/php -- I created a simple phpinfo(); file to verify, and this is what it's got:

image

@tianon Umm. My case may be happened only on php5 image?

@kujiy hmm, I can't seem to reproduce there, either: :disappointed:

$ docker run -it --rm --name test php:5-apache bash
root@8b1e2d2613b1:/var/www/html# echo '<?php phpinfo();' > index.php
root@8b1e2d2613b1:/var/www/html# apache2-foreground 
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.12. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.12. Set the 'ServerName' directive globally to suppress this message
[Tue Jan 24 17:21:23.364620 2017] [mpm_prefork:notice] [pid 7] AH00163: Apache/2.4.10 (Debian) PHP/5.6.29 configured -- resuming normal operations
[Tue Jan 24 17:21:23.364665 2017] [core:notice] [pid 7] AH00094: Command line: 'apache2 -D FOREGROUND'
$ docker run -it --rm --link test buildpack-deps:jessie-curl sh -c 'curl -fsSL http://test | grep php.ini'
<tr><td class="e">Configuration File (php.ini) Path </td><td class="v">/usr/local/etc/php </td></tr>

@tianon Wow thanks! Perhaps somehow someone changed apache command in our image since we've been using it with a bit customized entrypoint. I'll check it later. Thanks!

Given that this appears to be working as designed (and there are workarounds described above for achieving the desired behavior in a fairly simple way), I'm going to close. :+1:

When I had this problem, google gave me this page first, so I'll leave a small manual here

https://www.php.net/manual/en/configuration.file.php says:

If php-SAPI.ini exists (where SAPI is the SAPI in use, so, for example, php-cli.ini or php-apache.ini), it is used instead of php.ini. The SAPI name can be determined with php_sapi_name()

Dockerfile:

FROM php:7.4-fpm
RUN cp --verbose "${PHP_INI_DIR}/php.ini-development" "${PHP_INI_DIR}/php-fpm-fcgi.ini" && \
    cp --verbose "${PHP_INI_DIR}/php.ini-development" "${PHP_INI_DIR}/php-cli.ini"
# Just for testing line above
RUN apt-get update && apt-get install -y libfcgi0ldbl && \
    echo '<?php phpinfo();' >> /tmp/index.php

Test inside container:

# CLI
root@9c78c813e01a:/var/www/html# php -i | grep -i '\.ini'
Configuration File (php.ini) Path => /usr/local/etc/php
Loaded Configuration File => /usr/local/etc/php/php-cli.ini
Scan this dir for additional .ini files => /usr/local/etc/php/conf.d
Additional .ini files parsed => /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
user_ini.filename => .user.ini => .user.ini
# FPM
root@9c78c813e01a:/var/www/html# SCRIPT_NAME=/index.php SCRIPT_FILENAME=/tmp/index.php REQUEST_METHOD=GET cgi-fcgi -bind -connect 127.0.0.1:9000 | grep -i '\.ini'
<tr><td class="e">Configuration File (php.ini) Path </td><td class="v">/usr/local/etc/php </td></tr>
<tr><td class="e">Loaded Configuration File </td><td class="v">/usr/local/etc/php/php-fpm-fcgi.ini </td></tr>
<tr><td class="e">Scan this dir for additional .ini files </td><td class="v">/usr/local/etc/php/conf.d </td></tr>
<tr><td class="e">Additional .ini files parsed </td><td class="v">/usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
<tr><td class="e">user_ini.filename</td><td class="v">.user.ini</td><td class="v">.user.ini</td></tr>
Was this page helpful?
0 / 5 - 0 ratings