How do I set up per-site php.ini files on a LAMP server using NameVirtualHosts?

22,621

Solution 1

Most settings that you would place in your php.ini can also be placed in a .htaccess or in the apache configuration for that site. For example one of my virtual hosts has this in the .htaccess

    php_value display_errors 1
    php_value register_globals 0
    php_value short_open_tag 0
    php_value magic_quotes_runtime 0
    php_value magic_quotes_gpc 0
    php_value session.name "PHPSESSID_SITEBLAH"
    php_value session.gc_maxlifetime 57600
    php_value session.gc_probability 1
    php_value session.gc_divisor 500

Solution 2

You can also specify configuration settings in the Apache configuration file (httpd.conf/apache.conf). In your case you can specify the configuration values within the context.

The following example should set upload_max_filesize to 10M for the virtual host example.com.

<VirtualHost *>
    ServerName example.com
    DocumentRoot /path_to_docroot
    php_value upload_max_filesize 10M
    php_admin_flag allow_url_fopen on
</VirtualHost>

If you do not want to set this value for the whole of the site, but to one particular directory where the application lives, then you can specify the values within a .

<VirtualHost *>
   ServerName example.com
   DocumentRoot /path_to_docroot
  <Location /testApp/>
     php_value upload_max_filesize 10M
     php_admin_flag allow_url_fopen on
  </Location>
</VirtualHost>

See http://php.net/manual/en/configuration.changes.php for more explanation.

Solution 3

Yet another option is suPHP which allows you to define a php.ini for each VirtualHost. It also allows you to specify a PHP version for each VirtualHost.

Solution 4

Another suggestion is to use mod-fastcgi or mod-fcgi, which run PHP as a separate standalone daemon. Then, for each website, you can define a separate user and php.ini config file.

Share:
22,621

Related videos on Youtube

phirschybar
Author by

phirschybar

Updated on September 17, 2022

Comments

  • phirschybar
    phirschybar over 1 year

    I have a LAMP server serving a number of different websites using NameVirtualHosts.

    Until recently having a global php.ini file has been just fine, but recently one of our developers has requested some settings in php.ini that I'd rather not set globally.

    I'm pretty sure I've heard that you can set up php.ini files on a per-site basis, but don't know how to go about doing this.

    Can somebody point me in the right direction as to how to accomplish this?

  • StackKrish
    StackKrish almost 15 years
    "AllowOverride Options" or "AllowOverride All" should be set in the APache configuration for this to work.
  • puk
    puk about 12 years
    Would the values in, for example, apache.conf overwrite those in your php.ini file, or the other way around?
  • boot13
    boot13 over 8 years
    +1 Weirdly, nothing else worked. Local htaccess, php ini files, none of that worked.
  • o0x258
    o0x258 about 7 years
    Thanks! Maybe someone comes across the issue that mod-security parameters need to be overwritten. This does only work on vhost level not in DIRECTORY or LOCATION level. The reason is that mod-sec is being configured before the directories or location blocks are read.