dracoblue.net

Use Environment Variables for php.ini Settings in Docker

If you use the official docker image for php and want to modify e.g. max_post_body_size, you might come to the conclusion that adding a custom php.ini might be the only solution.

In terms of the 12factor app manifest, this is kind of disappointing, since they promote usage of environment variables for deployment specific configuration.

When trying to enable my php application to be configurable like this, I found multiple ways how to make this possible and want to share those with you.

php.ini/fpm.conf supports environment variables

It was news to me, that you can actually use environment variables in php‘s php.ini or fpm.conf. This is a feature of php's parseinifile.

So something like this in e.g. an /etc/php.d/environment.ini:

memory_limit=${PHP_MEMORY_LIMIT}

is possible. If you set PHP_MEMORY_LIMIT to 2048M in your docker-compose's environment or any of those env_files, it will work.

fastcgi_param in nginx.conf

Even if you might not want to modify your php image, you will usually have a custom nginx.conf in your nginx image.

So whenever you pass your php requests to a fastcgi handler/upstream, you can use:

fastcgi_param PHP_VALUE "memory_limit=2G\nsession.save_handler=memcache";
fastcgi_param PHP_ADMIN_VALUE "open_basedir=off";

in nginx‘s conf-files. Remember that setting this once for a php fastcgi process will keep the config also for the next requests. Thus use it never or always for the same php fpm pool.

Using environment variables is not built-in nginx. But if you have lua enabled in nginx, you can retrieve a environment variable like this somewhere before nginx‘s http:

env PHP_MEMORY_LIMIT;

and the following in a location/server:

set_by_lua $PHP_MEMORY_LIMIT 'return os.getenv("PHP_MEMORY_LIMIT")';
fastcgi_param php_value "memory_limit=$PHP_MEMORY_LIMIT“;

.htaccess in apache

You can also override the php value in a .htaccess if you are using apache as webserver:

php_value memory_limit 256M

If you use environment variables here:

php_value memory_limit %{ENV:PHP_MEMORY_LIMIT}

you can use dockers environment variables again to configure the deployment on demand.

.user.ini in fastcgi

The fastcgi version of php supports .user.ini files in any of the parent directories. So a file like:

memory_limit=2G

will work, too. You might even use environment variables here again.

Use envplate

If you use envplate, you have to modify how your Dockerfiles entrypoint/cmd is:

CMD [ "/usr/local/bin/ep", "-v", "/etc/nginx/nginx.conf", "--", "/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf" ]

This will replace all ${PHP_MEMORY_LIMIT} or even ${PHP_MEMORY_LIMIT:-2G} with a default value of 2G if the environment variable is not set.

Conclusion

There are several different ways how you can use deployment specific configuration for your php.ini settings.

If you put a:

memory_limit=${PHP_MEMORY_LIMIT}

at the end of your php.ini or create a custom /etc/php.d/environment.ini with this content, you can configure your php fpm/cli with:

  environment:
    - PHP_MEMORY_LIMIT=2G

Use envplate if you are able to introduce new tools for full conf files. If you just want to set those values for a specific fastcgi pool, you can use the webservers facilities for this purpose.

In 12factor, docker, docker-compose, open source, php, symfony by @ 09 Oct 2017

comments powered by Disqus

Recent Files

Advertisement

Recent Dev-Articles

Read recently

About

Blogroll