I'm working on a PHP web app which ALSO has some command line tools. I need the command line tools to detect the environment so that they connect with the correct DB credentials etc. The web app does this easily by checking $_SERVER['SERVER_NAME'] but that doesn't work for a shell script.

I'd like to create my own $_SERVER variable that the shell script can check. Ex: $_SERVER['MYAPP_ENVIRONMENT']. How do I do this?

I found this solution, but I don't see the same files in /etc/apache2/. I also found this, but they're using .htaccess and I'm not sure if I have mod_env and also my app uses it's own .htaccess file, so it would have to be edited every time I deploy.

I'm on a Dreamhost VPS, which runs Ubuntu 12.04 LTS

  • 1
    $_SERVER will be defined either way but environment variables are there because of Apache, when you use CL to execute your script, Apache won't be involved in the process, so there will be no environment variables!
    – undone
    Jan 22, 2015 at 17:30
  • I don't think what you want is possible. A shell or cron session is neutral and has no information that could help a script determine an environment in the same way as Apache does. In the Apache case, the requesting user supplies the environment with the HTTP host header - how do you think this could work for cron or a shell session?
    – Sven
    Jan 22, 2015 at 17:36
  • @Sven I was hoping the shell script could look at... something... which would differentiate the remote environment from my local. For example, the app locations differ, so I considered checking against $_SERVER['PHP_SELF'], which seems to be available in the shell. But it felt really hacky... but maybe that's the best option? Jan 22, 2015 at 18:12
  • Can you run them separately? each with its own CL parameter?
    – undone
    Jan 22, 2015 at 18:14

4 Answers 4


How about using php_sapi_name() function ( http://php.net/manual/en/function.php-sapi-name.php ) to detect the server API in use?

  • Thanks but that doesn't help me know WHICH server I'm on. The challenge isn't distinguishing CLI vs Apache. It's distinguishing which server either are running on. Jan 22, 2015 at 22:12

You can use command line arguments to make different and use $argv array to access them:

if (PHP_SAPI == "cli") {
    echo 'WebServer';

And its output:

C:\Users\user>E:\xampp\php\php.exe  -f E:\Apache\test\s.php 1st 2nd
array(3) {
  string(20) "E:\Apache\test\s.php"
  string(3) "1st"
  string(3) "2nd"

Few things about $argv:

  1. This is a super global array.
  2. First item is script name (or full path if you passed full path).
  3. There is another super global variable $argc which holds the number of $argv elements.
  4. register_argc_argv should be enabled otherwise $argv will be NULL.
  5. This variable is not defined when you access the script from browser(only for CL).
  • Thanks but remember the challenge isn't distinguishing CLI vs Apache. It's distinguishing which server either are running on. Jan 22, 2015 at 22:13
  • I know that, but you can execute your scripts separately. For example php -f script.php host_number1 or php -f script.php host_number2
    – undone
    Jan 22, 2015 at 22:58
  • When you say server, you mean domain? right?
    – undone
    Jan 22, 2015 at 22:59

As you lucky said, Apache allow this feature like writing SetEnv in vhost config - it really works!

<VirtualHost *:80>
    SetEnv APPLICATION_ENV "production"

So in PHP you will have $_SERVER['APPLICATION_ENV'] with value production


The proper variable to check environment variables is $ENV. This works both in CLI and in scripts run by Apache.


Or you set whatever environment variable you want in your system.

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.