114

I am downloading PHP for Windows. I got 2 options on the website.

  1. PHP Thread Safe
  2. PHP Non-Thread Safe

Please answer the following questions:

  1. What is the difference between the two? What are the advantages and disadvantages over one another?
  2. I am developing an e-commerce website which will have heavy traffic, which one is more recommended and why?
hakre
  • 193,403
  • 52
  • 435
  • 836
sumit
  • 10,935
  • 24
  • 65
  • 83
  • 1
    possible duplicate of [What is thread safe or non thread safe in PHP](http://stackoverflow.com/questions/1623914/what-is-thread-safe-or-non-thread-safe-in-php) – Shef Aug 26 '11 at 12:46
  • 2
    What do you understand by "heavy traffic" and how can you be sure that you will get it? – piotrp Aug 26 '11 at 12:59
  • @Crack- Heavy traffic means that many-many users will be using the website at a given time. Why can't I be sure of that?! Something wrong with it? – sumit Aug 26 '11 at 13:55
  • 1
    Only that "heavy traffic" can be defined in many ways, and what for some people is heavy traffic for some may be moderate or even light traffic. A useful metric here is page hits / day/hour/minute, any kind of objective measure. – piotrp Aug 26 '11 at 20:24

3 Answers3

120

From PHP documentation:

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.

So it really depends on the way that you want to use PHP:

  • Apache + LoadModule: Thread Safe
  • Apache + FastCGI: Non-Thread Safe
  • IIS: Thread Safe
  • IIS + FastCGI: Non-Thread Safe

PHP manual has nice installation instructions.

AFAIR running PHP with FastCGI is the preferable way, it performs faster and allows for more fine-grained security configuration.

Community
  • 1
  • 1
piotrp
  • 3,755
  • 1
  • 24
  • 26
  • 11
    [This](http://windows.php.net/download/) page tell that: "If you are using PHP with IIS you should use the Non-Thread Safe (NTS) versions of PHP." compared to the answer telling: "IIS: Thread Safe" – umutm May 24 '13 at 09:32
  • 9
    Yes, and it mentions FastCGI. Thread Safe is for when using ISAPI (php5isapi.dll) – piotrp May 24 '13 at 10:29
  • 4
    Why bother with non-thread-safe option at all? It's like: if I could choose between buying a safe car and a non-safe car, why would I ever choose the non-safe car? Does it perform better or something? – Simon East Jul 18 '17 at 01:53
  • 4
    @SimonEast The NTS version has some overhead managing threads, that are already done by webserver, so performance will be degraded – ReZa Jul 24 '17 at 14:13
  • 2
    @ReZa I see... so does that mean that if your web server is thread-safe then you don't need PHP to be thread-safe? – Simon East Jul 30 '17 at 03:02
  • 3
    @SimonEast Exactly. Its like doing a job two times, once in PHP and once in webserver – ReZa Aug 02 '17 at 13:46
  • 3
    @ReZa Good comment! Finally someone who actually tells why you need or not need it. – Hugo Cox Apr 15 '18 at 09:12
  • How do I disable `CGI/FastCGI` and enable` Apache 2` on a Laragon local server? – Razvan Zamfir Aug 08 '19 at 14:07
  • I have an installation of a PHP app (osTicket) in IIS 8 with FastCGI enabled, and using the NTS version, as recommended in this post, caused intermittent breakage, presumably from multi-thread race conditions. Switching to TS fixed everything. When in doubt, use TS. – Jacob Stamm Sep 04 '19 at 18:48
  • good answer but it lacks a performance test, in my case I use fast CGI so it is not necessary TS, does it have any impact on performance? –  Sep 29 '21 at 21:11
  • If I am installing PHP/Apache for local dev only, does it matter which version I install thread save or non-thread safe? – Steve G Jun 16 '22 at 02:18
  • Yes, please use the correct version for your installation, otherwise you risk running into issues that nobody will be willing to debug - it _may_ work, but even single-page scripts may send multiple concurrent requests when loading page dependencies – piotrp Jun 20 '22 at 17:03
3

Quick and simple: If you are using Apache edit your Apache24\conf\httpd.conf file and search for "loadmodule". If you see your loadmodule is referencing a .dll something like:

LoadModule php7_module "e:/x64Stack/PHP/php7.1.9/php7apache2_4.dll"
AddHandler application/x-httpd-php .php
PHPIniDir "e:/x64Stack/PHP/php7.1.9"

Then you want Thread Safety enabled or TS - Thread Safe version.

Else if you are using IIS or Apache with CGI then NTS flavor.

I use multiple stacks and within those multiple servers and versions of PHP so don't let the paths / php or server versions throw you.

Jim
  • 404
  • 3
  • 6
-2

In addition to Crack, since 5.4 you can use built-in web server (it works nice!).

Warning This web server was designed to aid application development. It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. It is not intended to be a full-featured web server. It should not be used on a public network.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Saso.Filipovic
  • 117
  • 2
  • 1
  • 14
    You need to add some sort of reference to what you are talking about. What built-in web server? How does this relate to ThreadSafe vs NotThreadSafe? – Despertar Mar 08 '14 at 00:43
  • 3
    I think by the built-in web server, he means [PHP's built-in webserver](https://secure.php.net/manual/en/features.commandline.webserver.php) (`php -S ip:port`) – no idea about the relation to thread safety though. – David Refoua Nov 28 '18 at 02:26