Alvar
Alvar

Reputation: 1

Can php-fpm service interpret environment variables inside www.conf to create unique log files?

I'm trying to set multiple cloud servers behind an ALB to write their log files to my common EFS. I have a single shared drive EFS, which I can access via a single development instance, while all my production instances are securely behind a VCN. I am trying to use system environment variables (specifically the HOSTNAME) so that each of the production instances, which use the same configuration file to start php-fpm (so that I can do auto-scaling), write a different log.

I've seen answers that say that the *nix way of using environment variables should work, but they are not working for me. Specifically ${HOSTNAME} returns blank (and I have checked it is set for the apache and root users; root starts the php-frm service). I also tried interpolations of that, and the bash flavor of things "%()". None of them work.

All of them return blank strings (but not errors).

I did create a single variable in my environment variable, and got that to work.

I also read that you can't assign a env value to a variable directly, but when I tried to create a separate variable, that actually returned an error when starting the service.

This is what I would expect to work:

slowlog = "/shared/logs/php-fpm/www-slow-${HOSTNAME}.log"

Tried all of the following:

slowlog = "/shared/logs/php-fpm/www-slow-".${HOSTNAME}.".log"

--> blank for hostname

slowlog = "/shared/logs/php-fpm/www-slow-${HOSTNAME}.log"

--> blank for hostname

slowlog = "/shared/logs/php-fpm/www-slow-%(HOSTNAME).log"

--> the file name includes %(HOSTNAME)

slowlog = "/shared/logs/php-fpm/www-slow-$HOSTNAME.log"

--> the file name includes $HOSTNAME

slowlog = "/shared/logs/php-fpm/www-slow-{$HOSTNAME}.log"

--> the file name includes {$HOSTNAME}

slowlog_name = "/shared/logs/php-fpm/www-slow-{$HOSTNAME}.log"
slowlog = slowlog_name

--> start error: unknown variable slowlog_name

Thank you for any tips/advice!

Upvotes: 0

Views: 647

Answers (1)

Jacob
Jacob

Reputation: 11

PHP 7.3 does support environment variable interpolation for log file names in config files - I just tested this. I suspect that where you're getting caught is that in bash, $HOSTNAME is a bash variable but not automatically an environment variable, and Linux systems vary in whether $HOSTNAME is set as an environment variable. RedHat distros do set it, Ubuntu does not:

jacob@nitrogen ~ $ cat /etc/redhat-release 
Fedora release 29 (Twenty Nine)
jacob@nitrogen ~ $ echo $HOSTNAME
nitrogen
jacob@nitrogen ~ $ env | grep HOSTNAME
HOSTNAME=nitrogen

jacob@iad11:~$ grep PRETTY_NAME /etc/os-release 
PRETTY_NAME="Ubuntu 18.04.2 LTS"
jacob@iad11:~$ echo $HOSTNAME
iad11
jacob@iad11:~$ env | grep HOSTNAME

You can test with a different environment variable, and then set $HOSTNAME in a custom startup script that then starts PHP.

Upvotes: 1

Related Questions