7

Is it possible to have just a single php.ini file, for example in the webroot (or even outside of it to prevent people accessing it via GET), and tell PHP quickly and easily where it is?

I know you can set php.ini directives in .htaccess, but is it possible to define a specific php.ini file to be used?

alex
  • 479,566
  • 201
  • 878
  • 984
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592

3 Answers3

13

Add this to your server configuration...

<VirtualHost 1.2.3.4:80>
PHPINIDir /path/to/new/php_ini
</VirtualHost>

Make sure to just include the path to the directory, not the entire path to the file.

Then restart Apache.

Check it worked with phpinfo().

alex
  • 479,566
  • 201
  • 878
  • 984
  • Hmm... That'd work, but I don't have access to that server configuration. – Niet the Dark Absol Feb 13 '12 at 22:41
  • @Kolink If you don't have access to that, consult your host. They may have already set something up. – alex Feb 13 '12 at 22:43
  • Worked in the way: Configuration File (php.ini) Path /etc/php/7.0/apache2 Loaded Configuration File /etc/php/7.2/apache2/php.ini Scan this dir for additional .ini files /etc/php/7.0/apache2/conf.d where still loads 7.0..... So it didn't work – Greg Wozniak Aug 15 '19 at 15:28
3

Have a look at .user.ini section at the php docs.

Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on a per-directory basis.

But beside the .unser.ini solution you can place an additional ini file in the "additional .ini files parsed" directory. There you can use one single ini file to overwrite all other settings. Name it with zzz at the beginning and it will be parsed at last. This is also easy for your hoster to deploy without destroying his settings.

powtac
  • 40,542
  • 28
  • 115
  • 170
  • 3
    You missed to quote a very important part: _Since PHP 5.3.0, PHP includes support for configuration INI files on a per-directory basis. **These files are processed only by the CGI/FastCGI SAPI**_ – lucaferrario Aug 31 '17 at 09:28
1

Kolink, I suspect that you are on a shared hosting service, in which case your host may be using something called suPHP. In this case -- as you describe -- the PHPINIDir directive doesn't work, in which case there is a suPHP_ConfigPath directive.

In terms of access, I have a standard mod_rewrite in my DOCROOT/.htaccess:

RewriteEngine   On
RewriteBase     /
# if a forbidden directory or file name (starting with a . or /) then raise 404 Fatal
RewriteRule (^|/)[_.] -         [F]

What this does is forbid any request for any filename or directory prefixed by . or _. I have a DOCROOT/_private where I keep this stuff for me:

suPHP_ConfigPath DOCROOT/_private

where you will need to replace DOCROOT by your local setting on your service. Look for DOCUMENT_ROOT in a phpinfo() listing.

TerryE
  • 10,724
  • 5
  • 26
  • 48
  • Actually, I'm on a VDS in this case. Anyway, turns out suPHP_ConfigPath was the answer (I asked the host about it). – Niet the Dark Absol Feb 14 '12 at 01:15
  • ?VDS? not an acronym that I've come across. If you mean VPS then why on earth have you configured it for suPHP?. The only logical reason for using suPHP is to enable UID based access control to maintain separation between cohosted accounts. – TerryE Feb 14 '12 at 01:45
  • Virtual Dedicated Server. To be honest, I don't care how it works as long as it works and does what I tell it to do. – Niet the Dark Absol Feb 14 '12 at 02:21
  • As long as you understand that your users will see a (slight) performance hit, and your VM will max out at ~5 URI script requests per sec. – TerryE Feb 14 '12 at 10:31
  • 1
    It's only for dev purposes. Before going live it'll get moved to a properly dedicated system ;) – Niet the Dark Absol Feb 14 '12 at 15:58
  • K that's fine, but note the "forbidden" rule. I have a bunch of directories `_private` for backups, releases and config; `_include` for all of my PHP class libraries, etc.; `_template` for all of my templates. It's a good trick: I don't need any other rule to prevent them being browsed. Also a +1 vote would be nice -- I did get the right answer. ;-P – TerryE Feb 14 '12 at 17:41