8

Let's said that I have installed PHP 5.5.x in Ubuntu and it comes with a default configuration. I would like to overwrite a few configurations but I do not want to (this is how I know it's possible):

  • edit the default php.ini file
  • use a PHP block in each .php file to overwrite them
  • use .htaccess files and directives

I would like to create a file named custom-php.ini with the following lines (as an example):

; Basic configuration override
expose_php = Off
memory_limit = 512M
post_max_size = 128M
upload_max_filesize = 128M
date.timezone = UTC
max_execution_time = 120

; Error reporting
display_errors = stderr
display_startup_errors = Off
error_reporting = E_ALL

; A bit of performance tuning
realpath_cache_size = 128k

; OpCache tuning
opcache.max_accelerated_files = 32000
; Temporarily disable using HUGE PAGES by OpCache.
; This should improve performance, but requires appropriate OS configuration
; and for now it often results with some weird PHP warning:
; PHP Warning:  Zend OPcache huge_code_pages: madvise(HUGEPAGE) failed: Invalid argument (22) in Unknown on line 0
opcache.huge_code_pages=0

; Xdebug
[Xdebug]
xdebug.remote_enable = true
xdebug.remote_host   = "192.168.3.1" // this IP should be the host IP
xdebug.remote_port   = "9001"
xdebug.idekey        = "XDEBUG_PHPSTORM"

Is there any place where I can write this file and default PHP values gets overwrited by the ones on custom-php.ini?

authentictech
  • 422
  • 6
  • 23
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • 1
    I don't have an Ubuntu machine nearby to check, but isn't there something like `/etc/php/conf.d`? You should be able to put a partial config in there which will override the main configuration file. – ChrisGPT was on strike Sep 23 '16 at 20:45
  • No want **ini_set()**, ya? – The One and Only ChemistryBlob Sep 23 '16 at 20:45
  • @TheOneandOnlyChemistryBlob yes, I don't want to add such line to each file ;) – ReynierPM Sep 23 '16 at 20:45
  • @Chris I could try that but I am not sure is the way to go - maybe I am wrong – ReynierPM Sep 23 '16 at 20:46
  • Why is that not the way to go? That's pretty much what that directory is for… – ChrisGPT was on strike Sep 23 '16 at 20:47
  • @Chris I believe you're right but is used for other config and not for overwrite current ones, I am not sure about it, I haven't a Ubuntu installed right now but I will try later at home – ReynierPM Sep 23 '16 at 20:48
  • you could look at creating a Virtual Host within Apache then for the VH for this site you can amend all/some of those paameters – RiggsFolly Sep 23 '16 at 20:48
  • 1
    This is answered here: http://stackoverflow.com/questions/1391808/how-do-i-include-a-php-ini-file-in-another-php-ini-file – Gabiriele Lalasava Sep 23 '16 at 20:48
  • @GabirieleLalasava If he does not want to amend the `php.ini` presumably because he does not want to effect all sites in the same way. HOW would recompiling PHP itself be a better solution – RiggsFolly Sep 23 '16 at 20:50
  • @ReynierPM, again, I don't have an Ubuntu machine handy to check… but I'm pretty sure the stuff in `conf.d/` gets loaded at the end of the main config file. It should override your existing settings. Have you tried it? – ChrisGPT was on strike Sep 23 '16 at 20:50
  • Why not use `ini_set()` like suggested. Better for portability of the site. – Daniel Sep 23 '16 at 20:50
  • @Chris I will try at home where I have Ubuntu, I will update the post with results – ReynierPM Sep 23 '16 at 20:51
  • 1
    @Daniel as I said on the OP **I don't want to** use a PHP block in each .php file to overwrite them, this mean I don't want to use `ini_set()` – ReynierPM Sep 23 '16 at 20:52
  • 1
    @Daniel, I would argue that `ini_set()` is a code smell. It should almost never be used. PHP allows configuration in way too many places… – ChrisGPT was on strike Sep 23 '16 at 21:02
  • @Chris - I agree teh use of ini_set should be avoided, but if needed I do think it is better to keep things in one file and include it in the header. Unless it is a design with all loose pages... – Daniel Sep 23 '16 at 21:12

3 Answers3

7

Yeah should be possible. First, create a PHP file somewhere in your document root called info.php and put <?php phpinfo() ?> in it. Then call it from the browser and look for Scan this dir for additional .ini files. On Ubuntu it is probably something like /etc/php5/apache2/conf.d.

In that directory, you can put your own custom ini file (call it something like 99.overrides.php.ini) so it gets parsed last.

In that file, put whatever additional config you want, restart Apache or the web server and the changes will take effect.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • Note that the `Scan this dir for additional .ini files` value will only be set if PHP was compiled with the `--with-config-file-scan-dir` argument. If it says something like `(none)`, then you'd have to recompile PHP with the correction options in order to use this feature. – Henry A. Sep 26 '16 at 17:54
  • This is driving me crazy. I added a file in conf.d directory and according to phpinfo() it's parsed but still my memory_limit value is the master value. I used a [PATH=/var/www/mysite] directive inside my custom ini-file. Any idea on why this has no effect? – Hokascha Mar 01 '21 at 13:49
0

If you're not using Apache and you're using CGI/FastCGI SAPI you can use .user.ini file as bellow:

upload_max_filesize     = "300M" 
post_max_size           = "100M"
CIRCLE
  • 4,501
  • 5
  • 37
  • 56
  • 1
    From the OP, in the original question: "I do not want to … use `.htaccess` files and directives". – ChrisGPT was on strike Sep 23 '16 at 21:16
  • He said he knows about that method and not that he didn't want to use that method. I've set up my answer to list from top to bottom the common methods and the best ones from my experience. – CIRCLE Sep 23 '16 at 21:22
  • 1
    My comment included a _direct quote_ from the OP: "I do not want to … use `.htaccess` files and directives". I'm pretty sure the OP doesn't want that solution. – ChrisGPT was on strike Sep 23 '16 at 21:24
0

Also interesting is: https://www.howtoforge.com/how-to-specify-a-custom-php.ini-for-a-website-apache2-with-mod_php

Apparently you can set a custom php.ini file through the PHPINIDir directive. You can specify that per virtual host as well.

<VirtualHost 1.2.3.4:80> [...] PHPINIDir /var/www/web1 [...] </VirtualHost>

Only for Apache though.