PHP-FPM environment variables are limited to 1024 chars

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, June 30, 2014

Follow me on Twitter as @mattiasgeniar

Here’s something I didn’t know: environment variables passed via PHP-FPM to PHP code, are hard-limited to 1024 characters. Not a very big problem per se, but if your environment variables pass the length of 1024 characters, you’ll find yourself scratching your head why the PHP-FPM pool won’t start.

This problem has now been filed as an enhancement request for PHP-FPM, #67846: Environment variables passed via php-fpm are hardlimited to 1024 chars.

If you have an environment variable longer than 1024 chars, you will find it silently truncated to the first 1024 characters. If you happen to have a string value, surrounded by quotes, that’ll end up causing parsing errors as the trailing quote is stripped.

$ cat /etc/php-fpm.d/pool.conf
...
env[TEST_VAR] = 'abc ... 1024 chars later ... xyz'

The above, given that TEST_VAR exceeds 1024 characters, will be truncated, causing the actual environment variable that is being read to be:

$ cat /etc/php-fpm.d/pool.conf
...
env[TEST_VAR] = 'abc ... 1024 char

The characters greater than 1024 are cut off, and you suddenly have an invalid string value, as it is missing its ending quote.

If you start your PHP-FPM daemon this way, you’ll see errors like:

$ /etc/init.d/php-fpm start
Starting php-fpm: PHP:  syntax error, unexpected $end in Unknown on line 1
[30-Jun-2014 14:01:22] ERROR: failed to load configuration file '/etc/php-fpm.d/site.conf'
[30-Jun-2014 14:01:22] ERROR: FPM initialization failed              [FAILED]

The only solution, until feature #67546 gets approved, is to limit your configs to 1024 characters per environment variable. The offending piece of code can be found in /sapi/fpm/fpm/fpm_conf.c of the PHP source.

int fpm_conf_load_ini_file(char *filename TSRMLS_DC) /* {{{ */
{
	int error = 0;
	char buf[1024+1];
....
		memset(buf, 0, sizeof(char) * (1024 + 1));
		for (n = 0; n < 1024 && (nb_read = read(fd, &c, sizeof(char))) == sizeof(char) && c != '\n'; n++) {
			buf[n] = c;
		}
....
}

Hopefully someone that knows more about C coding can pick this up. :-)



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.