Custom php.ini per-directory (for testing sites)

13,092

Solution 1

If you are using php_flag then you are probably using mod_php5. Custom 'php.ini' files aren't supported with this. You would need to use suPHP or equiv as the php.ini is only loaded at PHP startup, and this isn't per-request for mod_php5 and FCGI.

However, since you are using PHP 5.3 you can also use .user.ini files which are parsed on a per-request basis.

Solution 2

Many times you might feel that you require a different settings for different sites (running on the same server) as specified in php.ini . So you start making changes in your php files using ini_set() or .htaccess to override those settings. In this howto, I will tell you how to solve this problem of having different php settings by having separate php.ini files for each website.

So, lets assume that I have a website with document root as /var/www/html/example/ with domain as www.example.com. For testing purpose you can create a small file in /var/www/html/example/phpinfo.php

<?php
phpinfo();
?>

Now run http://example.com/phpinfo.php it will display current php settings.

Now copy the php.ini file from /etc/php.ini file to your /var/www/html/example.

[root]# cp /etc/php.ini /var/www/html/example #For fedora users

[chia]$ sudo cp /etc/php5/apache2/php.ini /var/www/example # for Debian and Ubuntu

And now, we just need a simple trick to tell apache where to find the php settings for this website. You need to make this small change in the configuration file.

<VirtualHost 10.24.11.2:80>

ServerName example.com

ServerAlias www.example.com

PHPINIDir /var/www/html/example

</VirtualHost>

PHPINIDir should be the directory where the file php.ini resides.

Restart the apache after this

[root]# service httpd restart # for Fedora/Redhat/CenOS based systems

[chia]$ sudo apache2 restart # for Debian/Ubuntu based systems

Share:
13,092

Related videos on Youtube

avenas8808
Author by

avenas8808

Learning about PHP, graphic design etc.

Updated on June 04, 2022

Comments

  • avenas8808
    avenas8808 almost 2 years

    I'm running Apache 2.26, on Windows 7 with PHP 5.39 mod_ssl/2.2.6 OpenSSL/0.9.8g .

    PHP works fine, so does Apache.

    However, I want to try and create custom php.ini files per directory, for my test sites.

    I could - and did - use php_flag but would like to try to create a custom php.ini file that works. I tried to Google this, but couldn't find anything relevant.

    This is my current .htaccess for C:/www/vhosts/localhost/testsite1:

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    
    #RewriteCond %{REQUEST_fileNAME} !\.php -f
    #RewriteRule .* index.php
    
    AddType text/html .asp
    AddHandler application/x-httpd-php .page
    
    php_value include_path "./php:/php/"
    

    Yet, I made a change in the custom php.ini within the php to have short tags off [for testing only] but it didn't pick it up, the php code showed instead.

    Any help is appreciated with this; it'll be extremely useful!

    (bear in mind, this Apache install is a development/testing one)

  • hakre
    hakre about 12 years
    .user.ini : "These files are processed only by the CGI/FastCGI SAPI."
  • TerryE
    TerryE about 12 years
    @hakre, sorry, yup you're right. The hit a sweep-spot for FCGI. If the OP wants to use mod_php5 on a WAMP config then the easiest way to debug this is in a separate VM
  • Pacerier
    Pacerier over 9 years
    @hakre, For apache we can use .htaccess per directory to specify php variables/config.
  • Pacerier
    Pacerier about 9 years
    @TerryE, Also, please elaborate on the "suPHP" option.
  • TerryE
    TerryE about 9 years
    suPHP isn't an option for the end developer. It's a choice for the sysadmin when configuring the webserver, and one that is mostly deprecated these days.