How to View phpinfo() Output From Command-line in Linux

phpinfo() Output

Here we show you a few ways on how to view the phpinfo() output from the command-line in Linux as an alternative to Apache or Nginx web servers.

Usually, to invoke the phpinfo(), users go through the process of creating a PHP file such as info.php somewhere on your web server such as the document root then insert the following code into the file:

Read Also  This is How to Add JavaScript to an HTML File

The file can be executed on a web server from the web browser to print out details about the PHP set up by going to http://localhost/info.php.

phpinfo() is a PHP function that outputs information about PHP’s configuration. Read more about it in this post.

View phpinfo() Output From Command-line

From your command line you can run the following command to get the configuration from the default php.ini file:

$ php -i

You can also use it in interactive mode:

$ php -a
Interactive shell

php > phpinfo();

You can also run it the command-line:

$ php -r "phpinfo();"

Or even run the function phpinfo() on the shell directly:

$ php -r phpinfo();

The problem with the above commands is that you get to echo the phpinfo() output from the default php.ini file. In the real world, you will find PHP setups with more than one configuration. The most common is where you have two configurations, one for the CLI and the second for Apache. If you use PHP-fpm you will have yet another configuration file.

Read Also  How to Hide Apache and PHP Version From HTTP Headers

The situation can get more complicated depending on your Linux distro. For example, Ubuntu and other major distros would further split the three configuration files into the PHP version.

To get around this you have to tell PHP which configuration you want to print out using the following example:

$ php -c /etc/php/7.4/fpm/php.ini -i

The above example prints out the phpinfo() output from the php.ini file that is used by PHP-fpm and PHP version 7.4.