2

I've used Apache in Ubuntu is the past. There is a envvars file contains environment variables used by the Apache Service. I'm unable to find such a file for Windows.

There are two approach that I've found to deal with it. I found Apache read Windows Environment variables. But I don't like this, since they are global and not application specific.

Other option I found is creating a .bat file, set env variables and then start httpd.exe. But the problem with this is that it doesn't works with Apache Service.

Another option I found is to use nssm. It allows to create custom service with service specific environment variables. But I am getting AH00141: Could not initialize random number generator error if I use it.

Is there any other alternative option that I can use?

2

You can use the SetEnv directive in Apache to setup your own environment variables specific to your application.

More details here https://httpd.apache.org/docs/2.4/mod/mod_env.html#setenv You can put these values inside your Virtual host or httpd.conf

  • It doesn't work in a lot of cases. I am using PHP module in Apache. These variable are not passed to PHP as environment variable. – VarunAgw Mar 9 '16 at 12:16
  • Also setenv doesn't work in this case too CustomLog ${APACHE_LOG_DIR}/access.log vcommon – VarunAgw Mar 9 '16 at 12:18
  • If you want to pass environment variable to PHP, then try this us2.php.net/configuration.changes#configuration.changes.apache & also you can simply do the environment variable initialization within your PHP code. – serverliving.com Mar 9 '16 at 12:22
  • Doesn't work php_value PHP_INI_SCAN_DIR "c:/". Only env variable can change this – VarunAgw Mar 9 '16 at 15:00
0

I ended up writing a wrapper that replaced httpd.exe. I renamed httpd.exe to httpd2.exe and used this wrapper httpd.sh to execute it. It basically temporarily set system environment variables before starting apache service. Once the service is started, it delete them.

#!/bin/bash

PHP_INI_SCAN_DIR="C:\Server\PHP\7.0\conf;C:\Server\Config\PHP"

if [ "stop" == "$*" ]; then
    exec /c/Server/Apache/bin/httpd2.exe -k stop
elif [ "" == "$*" ]; then
    export PHP_INI_SCAN_DIR
    echo "Starting Apache in console mode"
    /c/Server/Apache/bin/httpd2.exe
elif [ "start" == "$*" ]; then
    /bin/regtool set "/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Environment/PHP_INI_SCAN_DIR" "$PHP_INI_SCAN_DIR" -s
    /c/Server/Apache/bin/httpd2.exe -k start
    /bin/regtool unset "/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Environment/PHP_INI_SCAN_DIR"  
elif [ "restart" == "$*" ]; then
    #/bin/regtool set "/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Environment/PHP_INI_SCAN_DIR" "$PHP_INI_SCAN_DIR" -s
    #/c/Server/Apache/bin/httpd2.exe -k stop
    #/c/Server/Apache/bin/httpd2.exe -k start
    #/bin/regtool unset "/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Environment/PHP_INI_SCAN_DIR"
    /c/Server/Apache/bin/httpd2.exe -k restart # ENV don't reload this way :(
else
    /c/Server/Apache/bin/httpd2.exe "$*"
fi 

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.