I know I can add a custom value to the $_SERVER variable through Nginx with

fastcgi_param MY_VARIABLE "myValue";

Is it possible to add the same variable to PHP CLI $_SERVER? This would be used specifically for cron jobs. I'm using an Ubuntu server.

One option I've found is to call the PHP script like

$ MY_VARIABLE="myValue" php /path/to/php/script

But this seems less than ideal.

By default environmental variables are not sent to PHP and as I understand it, doing so will slow things down slightly. I'd like to avoid this if possible.


The best solution I've found is to put the variable into /etc/environment. The settings in this are loaded by cron jobs and CLI and show in PHP $_SERVER.

By default /etc/environment has one line for PATH. Simply add a new line at the end of the file.

PATH="/usr/local/sbin: ... "

As a side note, the PATH setting is this file seems to be overwritten by PHP as shown in $_SERVER.

PHP web service does not show the variable but adding it to the Nginx configuration is easy enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.