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

Developing Multiple Domains on One Machine?

Embedding php.ini settings in the httpd-vhost.conf, typically found in your server root under conf/extra/, is a great way to solve this common problem. If you never knew you could do this, see the PHP.net Manual under How To Change Configuration Settings. This will solve the pesky include_path problem, without adding configuration code to your bootstrapping code or anything else.

Of course, to use this effectively as localhost, you would need to make copies of a <VirualHost> block and configure each accordingly. Then, comment out all virtual host blocks except the one that you want to use!

Alternatively, one could start Apache with the -f option to point the server daemon to a different httpd.conf upon starting. Each httpd.conf would require an "if module block," such as and <IfModule phpx_module> block. Be sure to remember to account for Apache logging!

httpd -f /usr/local/apache2/conf/domains/fooDomain.conf

httpd -f /usr/local/apache2/conf/domains/barDomain.conf

Virtual Host Block With php.ini statements.

<VirtualHost 127.0.0.1:80>
    UseCanonicalName On
    ServerName localhost:80
    ServerAdmin you@localhost        
    CustomLog "/var/www/someDomain.com/data/logs/httpd_access_log" common
    ErrorLog "/var/www/someDomain.com/data/logs/httpd_error_log"
    LogLevel warn
    DocumentRoot "/var/www/someDomain.com/public"

    <Directory "/var/www/someDomain.com/public">
        # disable directory listing
        Options -Indexes +FollowSymLinks
        AllowOverride FileInfo
        Require all granted
    </Directory>

    <IfModule alias_module>
        # Alias /webpath /full/filesystem/path
        ScriptAlias /cgi-bin/ "/var/www/someDomain.com/scripts/cgi-bin/"
    </IfModule>

    <IfModule php7_module>
        # Use php7_module in opening statement above if on PHP 7+

        # Domain specific PHP configuration options.
        # The Apache process user must own any of the following directories.

        php_admin_value include_path "/var/www/someDomain.com/application/controllers:/var/www/someDomain.com/application/models:/var/www/someDomain.com/application/views"

        # Errors and Logging
        php_admin_flag display_startup_errors off
        php_admin_flag display_errors off
        php_admin_flag html_errors off
        php_admin_flag log_errors on
        php_admin_value error_log "/var/www/someDomain.com/data/logs/php_error_log"

        # File Related
        php_admin_flag file_uploads on
        php_admin_value upload_tmp_dir "/var/www/someDomain.com/data/uploads"
        php_admin_value upload_max_filesize 10M
        php_admin_value max_file_uploads 5

        # Sessions
        php_value session.save_handler "files"
        php_value session.save_path "/var/www/someDomain.com/data/sessions"

        # Caching
        php_value soap.wsdl_cache_dir "/var/www/someDomain.com/data/cache/sopa.wsdl"
    </IfModule>
</VirtualHost>

If you could use the %{SERVER_NAME} variable in conjunction with the <IfModule phpx_module> blocks to form a compound conditional, you could have just one httpd.conf`, or include a extra/php.conf with all the domain specific PHP.ini settings (in blocks, also). However, as long as "localhost" is the domain target, it will not do what you want. Thus, my answer in the virtual host block above.


Simple way to use custom php.ini file for vhost using Fast CGI is to copy the php.ini into a folder in the host like "customini".

After that to your vhost directive and add this simple line :

FcgidInitialEnv PHPRC "/path_to_your_custom_ini_dir_for_this_vhost/"

BE SURE TO HAVE / to your path no \, (it won't work with \)

Restart Apache.

That's all!

Full sample (on Windows Server here) :

 <VirtualHost *:80>
    DocumentRoot "C:/APACHE/htdocs/vhost/myvhost"
    ServerName www.vhost.com
    FcgidInitialEnv PHPRC "C:/APACHE/customini/myvhost/"
 </VirtualHost>

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)