PHP · LINUX
PHP configuration from the terminal, without phpinfo.php
In 2012 I swapped the file with phpinfo() for php -i over SSH. The command is still useful, but it shows the command-line configuration, which is not always the same as the website's.
For a long time, when I needed to know how PHP was configured on a server, I created a file with
<?php phpinfo(); ?>, uploaded it over FTP and opened it in the browser. It worked, but it was a chore:
find or create the file, upload it, open it and then remember to delete it, because that page shows
versions, paths and modules to anyone who finds the address.
In October 2012 I wrote here about the alternative I had found: asking PHP itself, from the terminal. This post is a revision of that one, with what I would change today.
The commands
With SSH access to the server, the version:
php -v
Everything phpinfo() would show:
php -i
The output is long and in plain text. Most of the time I only want one directive, so I filter it:
php -i | grep memory_limit
Or I ask for the value directly:
php -r 'echo ini_get("memory_limit"), PHP_EOL;'
The loaded modules:
php -m
And which php.ini is in use, which tends to be the real question when a setting “doesn’t stick”:
php --ini
What I wrote in 2012 and would not repeat
The original post suggested saving everything to a file:
php -i > phpinfo.txt
The problem is where the file ends up. If the command runs inside the site’s public folder, such as
public_html, phpinfo.txt is reachable from the browser, just as exposed as the phpinfo.php I wanted to
avoid. Today I read it straight in the terminal, with grep or less, or save it outside the public folder
(php -i > ~/phpinfo.txt) and delete it afterwards.
What the terminal doesn’t show
php -i shows the configuration of command-line PHP. The site may run on another one: Apache with
mod_php or PHP-FPM, each with its own php.ini. On Debian and Ubuntu, for example, they are different
files, such as /etc/php/8.2/cli/php.ini and /etc/php/8.2/fpm/php.ini. A common case: memory_limit
shows up as -1 (no limit) in the terminal and as 128M on the site.
The PHP-FPM binary accepts the same option. Its name changes with the version and the distribution:
php-fpm8.2 -i | grep memory_limit
Even so, each FPM pool can override directives with php_admin_value, and an .htaccess or .user.ini
file can change values per folder. When the question is what the site is using right now, phpinfo() in
the browser is still the exact answer. The difference is to use it for a few seconds, with a file name that
is hard to guess, and delete it right away.
When the php command doesn’t exist
If the terminal answers php: command not found, PHP may be outside the PATH or have the version in its
name. On shared hosting it is common to find php74, php81 and similar, or a path such as
/opt/cpanel/ea-php81/root/usr/bin/php. An ls /usr/bin/php* helps to find it. If nothing shows up, the
hosting provider has to tell you where command-line PHP is.
References
Who writes this
Eduardo Lira
Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.