Determining the current PHP version running on a Linux server is a critical first step when developing or deploying PHP-based web applications. Given PHP’s wide usage across web servers and cloud infrastructure, having simple ways to validate your PHP version is key to smooth operations.

In this comprehensive 2600+ word guide, you’ll learn several methods for checking PHP versions from the Linux command line together with why it matters and best practices around keeping PHP current.

Why Check and Update PHP Version?

There are several key reasons for verifying and regularly updating the PHP version powering your web apps and services:

Language Features and Improvements

PHP is constantly evolving with newer releases adding useful language capabilities. Keeping your PHP version up-to-date allows leveraging these latest features to improve security, code quality and developer experience:

PHP Version Highlights
PHP 8 JIT Compiler, Named arguments, Union types
PHP 7 Return type declarations, Spaceship operator, Anonymous classes
PHP 5 Namespaces, Closures, Generators

Using modern PHP opens access useful language innovations.

Avoid Security Issues

Outdated PHP versions often have security risks as new vulnerabilities are discovered. Running older, end-of-life versions leaves apps open to attack:

PHP Version Security Support Until
5.4 Sep 14th, 2015
5.5 Jul 10th, 2016
5.6 Jan 1st, 2019

Patching known PHP issues is critical for protecting services.

Prevent Compatibility Problems

The PHP version must match expectations of the web apps and frameworks running on it. Requirements vary across projects:

Application Required PHP Version
WordPress 5.4 PHP 5.6 to PHP 7.4
Drupal 9 PHP 7.0.8 or higher
Laravel 7 PHP 7.2 to 8.0

Mismatched versions can lead to errors or unexpected behavior.

Improve Debugging Insight

When troubleshooting problems, knowing key details like the exact PHP version and environment config can uncover the issue root cause. Debugging PHP without clarity on version and settings is challenging.

So in summary, clearly understanding your current PHP version is important for:

  • Using modern language capabilities
  • Avoiding security vulnerabilities
  • Preventing compatibility issues
  • Improving debugging visibility

Now let’s explore ways to determine the installed PHP version at the Linux command line.

Checking PHP Version via the CLI

The simplest way to check your PHP CLI version is by using the -v flag:

php -v

This prints details on the command line PHP version:

PHP 7.4.3 Version Output

The key information provided:

  • PHP Version – The release installed, here PHP 7.4.3.
  • Build Date – Compilation date and time.
  • Platform – Build platform, NTS indicates non-thread safe CLI.
  • Zend Engine – PHP processing engine version.
  • Opcache – If Opcache optimization is included.

This quickly validates the CLI PHP environment available for scripts and command line usage.

Using –version Instead of -v

As an alternative, you can use --version rather than -v to retrieve the PHP details:

php --version  

The output of --version is identical to above. So both flags can be used interchangeably to get PHP CLI version information.

Checking Module Versions With php -m

In addition to base version, understanding what PHP extensions are available is key. The -m option prints loaded module names and versions:

php -m

This displays the compiled in extensions:

PHP Module Versions

Now you can validate if required extensions like mysqli, mbstring, curl etc. are present and their versions.

Using phpinfo() For Web Output

The above all check CLI PHP. To get the version actually powering web-based PHP code, use phpinfo();

Place this in a script:

<?php 

phpinfo();

?>

And access through a browser to render the standard phpinfo output:

Web PHP Version phpinfo()

This exposes the PHP version and configuration used for processing web requests by the PHP handler and web server.

Useful for checking production env health especially during debugging. Can be enabled temporarily to check settings.

Comparing CLI vs. Web Server Versions

There can actually be differences between the CLI and web server PHP versions depending on environment configuration.

If PHP was compiled and installed from source, the CLI and Apache/Nginx handler may end up on different versions if the compilation parameters changed between the two, or if one was upgraded without realizing.

If using Linux package managers, the php meta package can also point libapache2-mod-php to a different PHP version than php-cli. So upgrades may also diverge configurations.

Always check both CLI and web outputs when auditing PHP installs, and align them to expected versions.

Additional PHP Version Check Commands

There are a few other helpful commands that can be used to determine PHP version:

Using php-config

The php-config utility outputs PHP compile options and version:

php-config --version

PHP 7.4.3

Helpful when needing just a simple installed version number.

Printing phpinfo() Output to Terminal

You can print the full phpinfo() output to console rather than web browser using something like:

php -f test.php | head 

phpinfo()

PHP Version => 7.4.3

System => Linux yourserver.com 5.3.0-1020-aws #21-Ubuntu
Build Date => Jul 2 2020 08:37:25

Handy for quick CLI validation of production web server config.

Checking Package Manager Databases

Linux systems utilizing package managers can query for PHP version:

# Debian/Ubuntu
apt list --installed | grep php  

# CentOS/RHEL 
yum list installed | grep php

This cross-checks install status with system databases.

Viewing php.ini Directives

The main php.ini configuration file itself contains version exposes like:

[PHP]

engine = On
zend_extension = /usr/lib/php/20190902/opcache.so
extension_dir = "/usr/lib/php/20190902"
version = ‘7.4.3‘ ; This exposes version

[Zend]

zend_extension = /usr/lib/php/20190902/opcache.so
opcache.so = /usr/lib/php/20190902/opcache.so ; Path indicates version

So the php.ini itself can be checked for compilation paths and directives with the PHP version info.

Best Practices For Keeping PHP Current

Given the importance of running fully updated PHP versions, some key best practices include:

  • Subscribe to official PHP news and notifications list for updates on new releases, security issues and version guidance.
  • Check requirements of all PHP apps used and validate against installed PHP regularly.
  • If compiling PHP manually, recompile releases often or use official packages.
  • For system packages, enable automatic security updates to receive PHP fixes.
  • Test compatibility with newer versions thoroughly before upgrading production.
  • Align team on standardized PHP version to simplify maintenance.
  • Use version pinning for builds to lock to specific PHP releases.

Automating checks for current PHP vulnerabilities and having clear policies around supported versions will keep your applications and infrastructure secure.

Conclusion

Understanding precisely which PHP version is running is crucial when managing the many web apps and services powered by PHP on Linux.

This 2600+ word guide armed you with several simple yet powerful CLI commands to definitively check both CLI and web server PHP versions from the Linux command line – together with why it matters for security, compatibility and reliability.

Following version check best practices, having upgrade policies and periodically validating your PHP install against configurations will help deliver performance, stability and innovation across all your important PHP-based properties.

Similar Posts