Site icon Ryadel

PHP: determine PHP version, architecture and thread safety mode

PHP: determine PHP version, architecture and thread safety mode

In order to use PHP to its full extent (installing extensions, use third-party libraries, and so on) you often need to know the exact PHP version installed on your webserver (5.x, 7.x, 8.x), as well as the CPU architecture (32 or 64 bit) and the thread safety mode (ts or nts) used. Retrieving these info can be simple enough in most scenarious but sometimes it can be tricky, especially if you don't own and/or have built your own server but you're using a VPS, VM, container or other cloud-based IaaS solutions.

In this post we'll briefly explain how to retrieve these values with three simple PHP CLI commands.

Check the PHP version

Go to the PHP folder (unless you don't have it in your PATH environment variable) and run the following CLI command:

Check the thread safety mode

Go to the PHP folder (unless you don't have it in your PATH environment variable) and run the following CLI command:

You’ll have a value of enabled for thread safe or disabled for not thread safe.

Check the architecture

Go to the PHP folder (unless you don't have it in your PATH environment variable) and run the following CLI command:

You’ll have a value of x86 for 32 bit and x64 for 64 bit.

What does thread-safe mean?

Before ending this post it can be useful to briefly recap what we do mean with "thread-safe" and "non thread-safe" PHP and what thread safety actually means.

In a nutshell:

  • A thread-safe PHP version creates a local storage copy in each thread, so that the data won't collide with another thread; that's great when we want PHP to work in a multithreaded webserver context, such as Apache 2 on Windows PHP modules - which gets loaded only once to serve all requests. However, these "safety" comes with an additional expense in terms of performance, therefore it's wise to use those version only when we do actually have a single PHP instance that is expected to serve multiple requests.
  • Conversely, a non thread-safe PHP version uses a single storage copy for all threads; these PHP versions usually perform better in terms of performance than their thread safe counterparts and should therefore be used whenever we don't need thread safety - such as when we're running PHP as a CGI binary, since the binary is invoked at each request.

If you're not sure on how your web server does actually run PHP, the best thing you can do is to use the phpinfo() method in the following way:

Put the one-liner above in a phpinfo.php file, publish it to your web server and run it once to see the Server API section value:

PHP: determine PHP version, architecture and thread safety mode

If the "cgi"string is present, such in the above screenshot, it means that the server is running PHP in CGI/FastCGI mode; conversely, if you see something such as apache, apache2handler, or something like that, it means that PHP is handled through a webserver module (such as Apache or IIS), therefore you should either use the thread-safe version or switch to CGI mode.

WARNING: be sure to delete the phpinfo.php file as soon as you don't need it anymore: leaving it publicly available is a major security risk, as it exposes your entire PHP configuration, as well as some sensitive system settings and folder structures.

Conclusion

That's it, at least for now: once you've determined your PHP version, architecture and thread safety mode you'll be ready to install your PHP extensions and/or third party tools without the risk of getting an incompatible build.

Exit mobile version