php.ini: which one? - php

I moved from my old apache to nginx and php 5.3.10. And when I tried to modify php.ini to suit my needs, I found there are 3 of them:
$ locate php.ini
/etc/php5/cgi/php.ini
/etc/php5/cli/php.ini
/etc/php5/fpm/php.ini
Which one should I edit?

Generally speaking, the cli/php.ini file is used when the PHP binary is called from the command-line.
You can check that running php --ini from the command-line.
fpm/php.ini will be used when PHP is run as FPM -- which is the case with an nginx installation.
And you can check that calling phpinfo() from a php page served by your webserver.
cgi/php.ini, in your situation, will most likely not be used.
Using two distinct php.ini files (one for CLI, and the other one to serve pages from your webserver) is done quite often, and has one main advantages : it allows you to have different configuration values in each case.
Typically, in the php.ini file that's used by the web-server, you'll specify a rather short max_execution_time : web pages should be served fast, and if a page needs more than a few dozen seconds (30 seconds, by default), it's probably because of a bug -- and the page's generation should be stopped.
On the other hand, you can have pretty long scripts launched from your crontab (or by hand), which means the php.ini file that will be used is the one in cli/. For those scripts, you'll specify a much longer max_execution_time in cli/php.ini than you did in fpm/php.ini.
max_execution_time is a common example ; you could do the same with several other configuration directives, of course.

Although Pascal's answer was detailed and informative it failed to mention some key information in the assumption that everyone knows how to use phpinfo()
For those that don't:
Navigate to your webservers root folder such as /var/www/
Within this folder create a text file called info.php
Edit the file and type phpinfo()
Navigate to the file such as: http://www.example.com/info.php
Here you will see the php.ini path under Loaded Configuration File:
Make sure you delete info.php when you are done.

It really depends on the situation, for me its in fpm as I'm using PHP5-FPM. A solution to your problem could be a universal php.ini and then using a symbolic link created like:
ln -s /etc/php5/php.ini php.ini
Then any modifications you make will be in one general .ini file.
This is probably not really the best solution though, you might want to look into modifying some configuration so that you literally use one file, on one location. Not multiple locations hacked together.

You can find what is the php.ini file used:
By add phpinfo() in a php page and display the page (like the picture under)
From the shell, enter: php -i
Next, you can find the information in the Loaded Configuration file (so here it's /user/local/etc/php/php.ini)
Sometimes, you have indicated (none), in this case you just have to put your custom php.ini that you can find here: http://git.php.net/?p=php-src.git;a=blob;f=php.ini-production;hb=HEAD
I hope this answer will help.

Related

PHP 7.2.14 "short_open_tag = On" ignored

On a fresh install of Fedora 28 running PHP 7.2.14 I am experiencing a strange issue where short_open_tag = On in the main php.ini is being ignored. I have verified that there is only one instance of the flag in only the main php.ini (/etc/php.ini). I have tried setting the flag on in .htaccess with php_value short_open_tag 1. I have restarted Apache after each change. But when I verify with phpinfo() the flag is always set to Off. Has this flag been finally deprecated and the change is simply not reflected in the PHP change log (http://php.net/ChangeLog-7.php)? Looking through the PHP source (which I am no expert at) does not suggest an override so I am at a loss for explanation. Any insight would be appreciated.
I will first note that this question looks like a duplicate of this one. On the off chance it's not, here's my best answer.
The PHP documentation has a page on the opening tags which says:
PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).
Check phpinfo() and you may see a section entitled Configure Command which contains compilation options. See if there's an --enable-short-tags option in there anywhere. If not, listen to #phil and look for a section titled Additional .ini files parsed which may list other ini files that have been parsed.
If your search is as thorough as you suggest in your original post and you still cannot get short tags, it may be turned off in an apache configuration file or the short_open_tag directive may exist in more than one spot. A PHP.ini directive will override any prior values that might have been set.
A grep search might help. In my PHP info output, I see these values:
Loaded Configuration File - /etc/php5/apache2/php.ini
Scan this dir for additional .ini files - /etc/php5/apache2/conf.d
I can easily search all files in that location with this grep command:
grep -ir 'short_open_tag' /etc/php5/apache2
If you double/triple check all these ini files and restart apache and still can't get short_open_tag setting to work, this value may be set as an apache option. I suggest searching the apache configuration files for any reference to short_open_tag. The exact directory location may be different on your machine, but this grep command works for me
grep -ir 'short_open_tag' /etc/apache2
You should also keep in mind that your apache configuration may not be set up to even bother with .htaccess files so your attempt to override using .htaccess may be for naught unless you configure apache to actually use the .htaccess file. Assuming your apache configuration does bother parsing your htaccess file, this appears to be covered in another question here on SO.
And finally, if your server is configured to use PHP-FPM, then it uses a pool of PHP processes to handle PHP requests from the web server. If that's the case, you would need to restart php-fpm with this command:
sudo service php7.0-fpm restart
NOTE: this command may vary on different machines.

Installing PHP on Linux and php.ini file

I have installed PHP with Apache 2.2 on my Linux machine and all works fine following guide here
During the installation I had to copy the php.ini-development file into /usr/local/lib/php.ini, the question is why? Why exactly in this directory?
Default locations of the PHP configurations vary greatly by distribution. In any case, phpinfo() will allways tell you where is looks for your configurations.
So, create file with extension ".php" in your webroot and past this into it:
<?php
phpinfo();
Also, the default php.ini files contain plenty od usefull comments. Just read them and change what you need. Differences between dev and prod environments are usually only the verbosity. They show and log different ammounts of errors. Production usually shows no error at all.
(Debian and many other distributions put the PHP config into /etc/php5. This is actually the most reasonable place for configurations, but stick with what your distribution uses.)
The document you link to says this
You may edit your .ini file to set PHP options. [...]
If you instead choose php.ini-production, be certain to read the list of changes within, as they affect how PHP behaves.`
Have you tried something like
vi /usr/local/lib/php.ini
to see what's in it?
Maybe you need to read http://www.php.net/manual/en/configuration.file.php
You copied a default PHP.ini file, php.ini-development, to the standard 'php.ini' file in the location '/usr/local/lib/' the guide http://www.php.net/manual/it/install.unix.apache2.php
expected the php.ini file to be.
The php.ini file is a configuration file used by PHP when it is launched (runtime)

How to configure htaccess-like files for configuring PHP when on shared hosting situations or FCGI? [duplicate]

My development environment is shared with other developers of my startup and is setup on Rackspace. The php.ini file is located in /etc/ folder, and I believe this is a centralized location from where every other developer's dev environment setting is being configured from. I want to customize this php.ini file specifically for myself rather than having to do it in the /etc/ location.
Specifically I am setting up XDEBUG in my environment, some other developers don't want it, so I don't want to bug em :)
To do so, I scanned the Internet on how to override the php.ini file specifically for a directory, and found this page on stackoverflow
And following that, I simply copy pasted the php.ini file within my htdocs folder and then simply echoed out phpinfo() (I echoed this in one of my Controllers, (using Zend)). The index.php file is within the htdocs folder.
When I look # "Loaded Configuration File", it still reads
/etc/ instead of ../htdocs/
Anybody know what's up?
In general, it isn't possible to load php.ini on a per directory basis, but in some special cases (CGI/FastCGI), it is: see documentation for Per-user configuration
Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.
In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.
If you are hosting several independent sites on one server, you should consider FastCGI anyway, to keep them separated. With php5-fpm it's very easy to setup many pools of workers.
Note that only set a limited subset of the ini-options in the user-ini-file.
As you said you don't have control on the server, the possible work-arounds would be to:
Use ini_set() to override the changes inside your script. Not all of the configuration directives can be changed using ini_set() though.
Use an .htaccess file in your directory to override the configurations in php.ini file.
(certain parts adapted from #1438393)
Hope this helps!
I'm not sure you understood the post. The post means if you run the server and want a per domain php.ini you can run the module as a per domain so each user controls there domain php.ini however it looks like your server does not offer this so you will need to us htaccess file to overwrite the php.ini settings.
By over write this doesn't mean you can change the directory this means maybe add a module or add error reporting ect...
You can do it by using this post: How can I use xdebug to debug only one virtual host?

Overriding PHP.ini in a shared development environment

My development environment is shared with other developers of my startup and is setup on Rackspace. The php.ini file is located in /etc/ folder, and I believe this is a centralized location from where every other developer's dev environment setting is being configured from. I want to customize this php.ini file specifically for myself rather than having to do it in the /etc/ location.
Specifically I am setting up XDEBUG in my environment, some other developers don't want it, so I don't want to bug em :)
To do so, I scanned the Internet on how to override the php.ini file specifically for a directory, and found this page on stackoverflow
And following that, I simply copy pasted the php.ini file within my htdocs folder and then simply echoed out phpinfo() (I echoed this in one of my Controllers, (using Zend)). The index.php file is within the htdocs folder.
When I look # "Loaded Configuration File", it still reads
/etc/ instead of ../htdocs/
Anybody know what's up?
In general, it isn't possible to load php.ini on a per directory basis, but in some special cases (CGI/FastCGI), it is: see documentation for Per-user configuration
Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.
In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.
If you are hosting several independent sites on one server, you should consider FastCGI anyway, to keep them separated. With php5-fpm it's very easy to setup many pools of workers.
Note that only set a limited subset of the ini-options in the user-ini-file.
As you said you don't have control on the server, the possible work-arounds would be to:
Use ini_set() to override the changes inside your script. Not all of the configuration directives can be changed using ini_set() though.
Use an .htaccess file in your directory to override the configurations in php.ini file.
(certain parts adapted from #1438393)
Hope this helps!
I'm not sure you understood the post. The post means if you run the server and want a per domain php.ini you can run the module as a per domain so each user controls there domain php.ini however it looks like your server does not offer this so you will need to us htaccess file to overwrite the php.ini settings.
By over write this doesn't mean you can change the directory this means maybe add a module or add error reporting ect...
You can do it by using this post: How can I use xdebug to debug only one virtual host?

How do I include a php.ini file in another php.ini file?

How do I include a php.ini file in another php.ini file?
I don't think you can "include" .ini files from the main php.ini file.
One possible solution, though, might be to use this option on the configure line, when compiling PHP:
--with-config-file-scan-dir=PATH
Set the path where to scan for configuration files
If this option is used at compile-time, PHP will look for every .ini file in this directory, in addition to the "normal" php.ini file.
I suppose this is what is used by Ubuntu, for instance, which uses a different .ini file for each downloaded extension, instead of modifying php.ini.
The path to the php.ini file is being defined with this option, on the configure line:
--with-config-file-path=PATH
Set the path in which to look for php.ini [PREFIX/lib]
Still, it probably means you'll have to re-compile PHP -- which is not that hard, btw -- the hardest part being to get the dependencies you need.
And, here is a post on the internals# mailling-list that says the same thing as I do: config files and PHP_CONFIG_FILE_SCAN_DIR
One can also define the path in ~/.bashrc
export PHP_INI_SCAN_DIR=/usr/local/lib/php.d
I installed Memcached for php and wasn't sure how to make sure that its ini was included in my php.ini file, but as it turns out, it automatically is. You can validate what is being loaded by running php --ini.
php --ini
Configuration File (php.ini) Path: /opt/local/etc/php5
Loaded Configuration File: (none)
Scan for additional .ini files in: /opt/local/var/db/php5
Additional .ini files parsed: /opt/local/var/db/php5/memcached.ini
EDIT: My answer was mistaken. This only works in .conf files, which is not the question asked. Better testing showed that it won't work in php.ini files, where include statement is ignored.
I just tested it on DebianĀ 9 (Stretch) with PHP-FPM. From some .conf file, use this syntax:
include=/path/to/special-php.ini
or even
include=/path/to/special-dir-full-of-conf-files/*.conf
as it is used in
/etc/php/7.0/fpm/php-fpm.conf
include=/etc/php/7.0/fpm/pool.d/*.conf
By the way, this will be most useful if you split your settings by topic, and or if you want a set for development and another one for production. Then you could do it the Debian/Apache style like
/etc/php/conf-available/
/etc/php/conf-enabled/
with symliks from the second to the other and an include to that one.
It seems you cannot include one ini file into another so it gets referenced and loaded. But you can set php up to load several files by telling it which folders to look into.
When using a FastCGI setup (possibly in FPM, too, though I don't know that for sure) you can export environment variables from within the PHP wrapper.
There you could do:
export PHP_INI_SCAN_DIR=/etc/php5/cgi/conf.d:/var/www/mydomain.net/etc
/var/www/mydomain.net/etc is just an example. It's the folder where you put your additional ini files into. It seems this can be a : separated list.
Use a phpinfo.php (file called arbitrarily containing only <?php phpinfo();), open the corresponding URL in your browser and check the list of directories that are parsed and the list of files that get loaded in the top area of it.
/etc/php5/cgi/conf.d should always be included (I guess because it was compiled into the PHP executable) and possibly not really be needed.
You can't. Read online pages:
The configuration file
SUMMARY: The configuration file
(php.ini) is read when PHP starts up.
For the server module versions of PHP,
this happens only once when the web
server is started. For the CGI and CLI
version, it happens on every
invocation.
.user.ini files
SUMMARY: In addition to the main
php.ini file, PHP scans for INI files
in each directory, starting with the
directory of the requested PHP file,
and working its way up to the current
document root (as set in
$_SERVER['DOCUMENT_ROOT']). Only INI
settings with the modes PHP_INI_PERDIR
and PHP_INI_USER will be recognized in
.user.ini-style INI files.
You could try to simulate it making use of the ini_set function. But as the "man page" indicates, not all ini options can be changed using ini_set. It's definitely a useful function, though.

Categories