899

I saw different binaries for PHP, like non-thread or thread safe?

What does this mean?

What is the difference between these packages?

Arsen Khachaturyan
  • 7,335
  • 4
  • 37
  • 38
O..
  • 10,555
  • 5
  • 19
  • 8
  • 23
    You can read about Thread Safety on http://en.wikipedia.org/wiki/Thread_safety Regarding PHP: [Is PHP thread-safe](http://stackoverflow.com/questions/681081/is-php-thread-safe) [Don't Believe The Lies: PHP Isn't Thread-Safe Yet](http://neosmart.net/blog/2008/dont-believe-the-lies-php-isnt-thread-safe-yet/) [Difference between PHP thread safe and non thread safe binarie](http://www.iis-aid.com/articles/my_word/difference_between_php_thread_safe_and_non_thread_safe_binaries) There are plenty of resources available. Search here at Stackoverflow or Google. – TigerTiger Oct 26 '09 at 09:58

4 Answers4

752

Needed background on concurrency approaches:

Different web servers implement different techniques for handling incoming HTTP requests in parallel. A pretty popular technique is using threads -- that is, the web server will create/dedicate a single thread for each incoming request. The Apache HTTP web server supports multiple models for handling requests, one of which (called the worker MPM) uses threads. But it supports another concurrency model called the prefork MPM which uses processes -- that is, the web server will create/dedicate a single process for each request.

There are also other completely different concurrency models (using Asynchronous sockets and I/O), as well as ones that mix two or even three models together. For the purpose of answering this question, we are only concerned with the two models above, and taking Apache HTTP server as an example.

Needed background on how PHP "integrates" with web servers:

PHP itself does not respond to the actual HTTP requests -- this is the job of the web server. So we configure the web server to forward requests to PHP for processing, then receive the result and send it back to the user. There are multiple ways to chain the web server with PHP. For Apache HTTP Server, the most popular is "mod_php". This module is actually PHP itself, but compiled as a module for the web server, and so it gets loaded right inside it.

There are other methods for chaining PHP with Apache and other web servers, but mod_php is the most popular one and will also serve for answering your question.

You may not have needed to understand these details before, because hosting companies and GNU/Linux distros come with everything prepared for us.

Now, onto your question!

Since with mod_php, PHP gets loaded right into Apache, if Apache is going to handle concurrency using its Worker MPM (that is, using Threads) then PHP must be able to operate within this same multi-threaded environment -- meaning, PHP has to be thread-safe to be able to play ball correctly with Apache!

At this point, you should be thinking "OK, so if I'm using a multi-threaded web server and I'm going to embed PHP right into it, then I must use the thread-safe version of PHP". And this would be correct thinking. However, as it happens, PHP's thread-safety is highly disputed. It's a use-if-you-really-really-know-what-you-are-doing ground.

Final notes

In case you are wondering, my personal advice would be to not use PHP in a multi-threaded environment if you have the choice!

Speaking only of Unix-based environments, I'd say that fortunately, you only have to think of this if you are going to use PHP with Apache web server, in which case you are advised to go with the prefork MPM of Apache (which doesn't use threads, and therefore, PHP thread-safety doesn't matter) and all GNU/Linux distributions that I know of will take that decision for you when you are installing Apache + PHP through their package system, without even prompting you for a choice. If you are going to use other webservers such as nginx or lighttpd, you won't have the option to embed PHP into them anyway. You will be looking at using FastCGI or something equal which works in a different model where PHP is totally outside of the web server with multiple PHP processes used for answering requests through e.g. FastCGI. For such cases, thread-safety also doesn't matter. To see which version your website is using put a file containing <?php phpinfo(); ?> on your site and look for the Server API entry. This could say something like CGI/FastCGI or Apache 2.0 Handler.

If you also look at the command-line version of PHP -- thread safety does not matter.

Finally, if thread-safety doesn't matter so which version should you use -- the thread-safe or the non-thread-safe? Frankly, I don't have a scientific answer! But I'd guess that the non-thread-safe version is faster and/or less buggy, or otherwise they would have just offered the thread-safe version and not bothered to give us the choice!

Community
  • 1
  • 1
Amr Mostafa
  • 21,962
  • 2
  • 26
  • 24
  • 2
    So PHP-FPM is not threaded? That solves the problem then since Fast CGI is used on nginx servers. – Xeoncross Oct 28 '11 at 02:30
  • 58
    Awesome detail, I have been programming in PHP for years and never knew this. – Patrick Apr 23 '13 at 19:40
  • 1
    @Xeoncross: Generally that's correct, and in practice it's one of the great reasons to manage PHP processes outside of Apache. I go over this aspect in my answer. – J. M. Becker Jul 09 '13 at 13:56
  • The only problem here is that PHP-FPM is not available under Windows. At least as a native build. – Denis V Oct 04 '13 at 10:59
  • Just for clarity, when you say "thread safety does not matter", does that mean that I should install the "non-thread safe" version of PHP in that situation? – Nigel Alderton Nov 13 '13 at 12:00
  • Right, you "should" install the "non thread-safe" version. Installing the thread-safe version would still work but your PHP will be doing extra work that you don't need in this case. That, and the fact that the thread-safe version of PHP is much less tested and supported by the community at large. – Amr Mostafa Nov 13 '13 at 12:21
  • @user968808 this is for not constructive at all. It's not that Windows cannot run something similar at all, it's the code that's written the way that's not portable. And also, as a desktop developer system, Windows is OK for me, but unfortunately I cannot have PHP-FPM in my environment... – Denis V Apr 24 '14 at 20:50
  • 1
    The only situation that forces me to install a VC Thread Safe version was when playing around Pthreads extension. No more, thank you :) It's better to let PHP do the normal way then control threads/process on the Handler, this case, Apache prefork. You can do threads using Ajax or with a set of php files executing them from shell(). – m3nda Apr 24 '15 at 03:34
  • @sam: http://www.php.net/manual/en/faq.installation.php#faq.installation.apache2 they still recommend not using in a threaded environment. – Rune Schjellerup Philosof Aug 03 '15 at 10:38
  • @RuneSchjellerupPhilosof, All the link stated is that **nts is more robust than ts**, which is effectively stating the obvious. For the same piece of functionality, ts code will need to do extra to ensure that it's ts. more code = more chance for bugs to occur. – Pacerier Aug 14 '15 at 06:19
  • 9
    Is PHP's thread safety still "highly disputed" (in 2015 and version 7) ? – Altiano Gerung Dec 12 '15 at 12:31
  • Apart from **setlocale** and **gettext**, I have never seen another non-thread-safe PHP function. Granted, threaded code brings with it memory leak and fragmentation issues, but there's a MaxConnectionsPerChild setting in Apache to help with that. Other than that if it doesn't coredump and produces the right results under load, there's no reason not to use php ts with mpm_worker. – rustyx Jan 18 '17 at 20:30
  • 1
    In addiotion to prefork MPM and worker MPM, there is [**event MPM**](https://httpd.apache.org/docs/2.4/mod/event.html) available in Apache version 2.3.13 and later – Accountant م Jul 10 '18 at 05:56
  • I suppose the new-in-PHP-8.1 `Fiber`s are not really multithreaded, so everything written still applies, doesn't it? – s3c Nov 28 '21 at 17:56
314

For me, I always choose non-thread safe version because I always use nginx, or run PHP from the command line.

The non-thread safe version should be used if you install PHP as a CGI binary, command line interface or other environment where only a single thread is used.

A thread-safe version should be used if you install PHP as an Apache module in a worker MPM (multi-processing model) or other environment where multiple PHP threads run concurrently.

Greg
  • 20,248
  • 16
  • 79
  • 106
  • 1
    greg your answer should have been: "A Thread Safe version should be used if you want to install PHP as an Apache module where worker is the MPM" – wlf Dec 29 '12 at 19:11
  • 15
    so php that coming with xampp on windows is NTS or TS ? – Abudayah Oct 01 '14 at 13:30
  • 1
    What if you use PHP built-in web server? Which variant of PHP to use for optimal performance? – Ariod Jun 01 '15 at 09:58
  • @Dario Optimal performance doesn't come from PHP's inbuilt server, it is for development only. – Greg Jun 01 '15 at 11:53
  • @Greg, thanks, yes I am aware of this. However, I noticed that the performance of the built-in server is far worse on my Windows machine than my Mac machine, so I am trying to see if this has any impact. – Ariod Jun 01 '15 at 15:56
  • 1
    @Dario I think that's just Windows. – Greg Jun 01 '15 at 16:23
  • @Dario, For the same piece of code, TS version will never be faster than NTS. That's obvious, because TS needs to do extra work to ensure that it's TS. You might see ["significant performance gains"](https://archive.is/QqrHK#InstallPHP), depending on what your code does. – Pacerier Aug 14 '15 at 06:38
  • 1
    @ChristopherShaw dude, the answer contains ONLY opinion. There is no source or reason WHY to use ZTS in combination with an Apache module. Its not a valid answer. Even 217 ppl can be wrong m8. – Daniel W. Mar 15 '18 at 12:35
  • @Abudayah 7 years later I have the same doubt! I am using xampp with php 7.4 and it seems that it is TS: Server API: Apache 2.0 Handler, Thread Safety: enabled – eniel.rod Nov 28 '21 at 08:18
30

Apache MPM prefork with modphp is used because it is easy to configure/install. Performance-wise it is fairly inefficient. My preferred way to do the stack, FastCGI/PHP-FPM. That way you can use the much faster MPM Worker. The whole PHP remains non-threaded, but Apache serves threaded (like it should).

So basically, from bottom to top

Linux

Apache + MPM Worker + ModFastCGI (NOT FCGI) |(or)| Cherokee |(or)| Nginx

PHP-FPM + APC

ModFCGI does not correctly support PHP-FPM, or any external FastCGI applications. It only supports non-process managed FastCGI scripts. PHP-FPM is the PHP FastCGI process manager.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
J. M. Becker
  • 2,641
  • 29
  • 31
24

As per PHP Documentation,

What does thread safety mean when downloading PHP?

Thread Safety means that binary can work in a multithreaded webserver context, such as Apache 2 on Windows. Thread Safety works by creating a local storage copy in each thread, so that the data won't collide with another thread.

So what do I choose? If you choose to run PHP as a CGI binary, then you won't need thread safety, because the binary is invoked at each request. For multithreaded webservers, such as IIS5 and IIS6, you should use the threaded version of PHP.

Following Libraries are not thread safe. They are not recommended for use in a multi-threaded environment.

  • SNMP (Unix)
  • mSQL (Unix)
  • IMAP (Win/Unix)
  • Sybase-CT (Linux, libc5)
Community
  • 1
  • 1
Somnath Muluk
  • 51,453
  • 32
  • 215
  • 222