How do I create custom php.ini files for each virtual host?

Joao picture Joao · Mar 10, 2014 · Viewed 28.1k times · Source

I've installed EasyPHP WAMP for local development only (I'm not hosting any websites).

Is there a way to set custom php settings for separate virtual hosts?

Currently and out-of-the-box, the php.ini file is loaded from: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion\php.ini It would be nice if, say, I could drop in a custom php.ini file into the virtual host directory to override settings in the original php.ini This way, I could better emulate a production server's environment on a per-site basis.

I've seen this work with online hosting accounts. But I can't figure out how to make this work on my machine.

Answer

Álvaro González picture Álvaro González · Mar 11, 2014

Using custom php.ini files is pretty straighforward for CGI/FastCGI based PHP installations but it isn't feasible when running PHP as Apache module (mod_php) because the whole server runs a single instance of the PHP interpreter.

My advice:

  • Set from PHP itself as many settings as you can:

    ini_set('memory_limit', '16M');
    date_default_timezone_set('Europe/Madrid')
    ...
    

    In other words, directives that can be changed at runtime.

  • Set the rest of stuff from per-directory Apache setting files (aka .htaccess):

    php_flag short_open_tag off
    php_value post_max_size 50M
    php_value upload_max_filesize 50M
    

    i.e., settings that need to be defined before the script starts running

Please have a look at the Runtime Configuration for further details.

Sometimes, you'll actually need different settings in development and production. There're endless ways to solve that with PHP code (from creating a boolean constant from the $_SERVER['HTTP_HOST'] variable to just having a config.php file with different values) but it's trickier with .htaccess. I normally use the <IfDefine> directive:

<IfDefine DEV-BOX>
    #
    # Local server directives
    #
    SetEnv DEVELOPMENT "1"

    php_flag display_startup_errors on
    php_flag display_errors on
    php_flag log_errors off
    #php_value error_log ...
</IfDefine>
<IfDefine !DEV-BOX>
    #
    # Internet server directives
    #
    php_flag display_startup_errors off
    php_flag display_errors off
    php_flag log_errors on
    php_value error_log "/home/foo/log/php-error.log"
</IfDefine>

... where DEV-BOX is a string I pass to the local Apache command-line:

C:\Apache24\bin\httpd.exe -D DEV-BOX

If you run Apache as service, the -D DEV-BOX bit can be added in the Windows registry, e.g.:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Apache2.4\Parameters\ConfigArgs

Related: Find out how PHP is running on server (CGI OR fastCGI OR mod_php)