PHP (Hypertext Preprocessor) is a popular programming language generally used for web development. It was originally developed by the Danish-Canadian programmer Rasmus Lerdorf in the year 1994. It can be used either via the command line or used with popular web servers like Apache or Nginx. Apache has a PHP module to directly run PHP files but Nginx required a PHP-FPM package to work with PHP files.

Advertisement

This tutorial will help you to check the PHP version installed on your system. Also, what is the active version of PHP on the command line as well as the check PHP version used by Apache or Nginx? As we can install multiple PHP versions on a single system, it can be the there are different version’s active on CLI and Web servers.

1. Verifying the PHP Version in CLI

Typically, a single default PHP version is associated with the standard binary. To determine the PHP version that is currently set as default for command line operations, run the subsequent command in the terminal:

php -v 
Output
PHP 7.4.8 (cli) (built: Jul 13 2020 16:45:28) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.8, Copyright (c), by Zend Technologies

On Ubuntu and Debian systems, you can also access another version by pointing their versions like: php7.2 or php7.3 etc.

2. Verifying the PHP Version for Apache/Nginx

It’s common to find that the PHP version active on the command line differs from the one utilized by Apache or Nginx web servers. Furthermore, it’s feasible for two separate virtual hosts to operate on different PHP versions through PHP-FPM.

Therefore, to ascertain the PHP version currently in use by Apache or Nginx, proceed to the document root of the specific website whose PHP version you wish to check. For instance, if my document root is “/var/www/html”:

cd /var/www/html 

Then create and edit a PHP script file in your favorite text editor:

nano phpinfo.php 

And, add the following content to the files. Here we used built-in phpinfo() function, which prints the active PHP version details:


<?php
  phpinfo(); 
?>

Press “CTRL +X” and hit Enter button. Then press ‘Y’ and again hit Enter to save and exit from text editor.

Next, open a web browser and access `https://localhost/phpinfo.php` URL. Update localhost with the IP address or the configured domain name on the system checking php version.

https://localhost/phpinfo.php

How to Check PHP Version
phpinfo() Function Result

The output will show you all the details related to active PHP along with the PHP version. It will show you the values of the PHP variables, a list of enabled PHP modules along with their version and other details.

Conclusion

In conclusion, understanding how to check the PHP version in different environments is crucial for developers and system administrators. Whether you’re working directly in the command line interface or dealing with web servers like Apache or Nginx, being able to quickly ascertain the active PHP version can aid in troubleshooting, ensure compatibility with applications, and help maintain a secure and efficient environment. This article has guided you through the process of checking the PHP version via CLI and for web servers, offering a straightforward path to keeping your PHP-based projects running smoothly. Remember, staying informed about your PHP version is key to leveraging the full potential of your development and server environments.

Share.

1 Comment

  1. Gwyneth Llewelyn on

    Hm. That’s for PHP; what about figuring out the web server‘s version? Under Apache, apache_get_version() should work, but what about all the others?

    It would be nice to get a few examples of that as well 🙂

Leave A Reply

Exit mobile version