How to Quickly Check PHP Version: CLI, NginX, Laravel

Sometimes you may see PHP version errors when running a Terminal command.

PHP Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.1.0". You are running 8.0.29. in /home/user/example.org/vendor/composer/platform_check.php on line 24
 
Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.1.0". You are running 8.0.29. in /home/user/example.org/vendor/composer/platform_check.php on line 24

Or get syntax errors when you visit your site on the browser.

Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) in app/vendor/package/utilities/src/Functions.php on line 17

One common issue between these two problems is the different PHP versions in the Terminal and on the website. It is possible to install several different PHP versions, and command line interface (CLI) PHP versions differ from your site's version.

You need the same PHP version on the CLI and the site to get things running smoothly. Let's see how to check these versions.


Check CLI PHP Version

php -v
PHP 8.0.29 (cli) (built: Jun 8 2023 15:24:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.29, Copyright (c) Zend Technologies
with Zend OPcache v8.0.29, Copyright (c), by Zend Technologies

Check NginX Sites PHP Versions

Using the grep command, you can quickly identify each site's PHP version.

grep -H fastcgi_pass /etc/nginx/sites-enabled/*

From the socket in the output, we can see that they all run on PHP 8.1.

/etc/nginx/sites-enabled/web1.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;
/etc/nginx/sites-enabled/web2.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;
/etc/nginx/sites-enabled/web3.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;
/etc/nginx/sites-enabled/web4.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;
/etc/nginx/sites-enabled/web5.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;

Alternative: Check the Site's PHP Version Using phpinfo()

Here's an example Laravel route to do that.

routes/web.php

Route::get('/version', function () {
phpinfo();
});

phpinfo

Notice: please make sure that you don't leave that route or phpinfo() public, as showing that information is considered a security issue. Remove that route as soon as you have the information.


In short, to avoid the errors above, make sure your CLI PHP version and your web-server project's PHP version always match.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 79 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials