在phpchina上有朋友谈到了这个问题,由于对这个问题一直也不太清楚,所以就转一下资料。
The Challenge - PHP on Windows
尽管PHP是一个多平台语言,在Windows上运行PHP还是一个挑战。这是因为PHP是基于UNIX平台开发的,每个请求由一个不同的进程来处理。然
而在Windows平台下面,同一类型的请求是由同一进程的不同线程来处理的。这个区别意味运行着PHP的IIS的频繁崩溃。进一步来说,对这一问题的唯
一的解决方案就是把PHP运行在外部方式下(CGI)。最终导致PHP性能的降低。
Multi-threading:
PHP was originally written for a multi-process environment (Apache) and
is therefore most commonly used with the popular LAMP (Linux, Apache,
MySQL, PHP) platform. Under Windows, things work differently. Literally
all of the Web Servers under Windows, including IIS and Apache, are
multi-threaded, which means that a single process handles all
concurrent clients. For PHP to run properly under a multithreaded
server, it must be designed with Thread Safety in mind. Failure to do
so will cause instability and may often result in unexpected crashes.
To make things worse, a crash in a multithreaded environment will
typically cause the entire Web Server to go down, as opposed to
multi-process environment, where a typical crash will only affect one
request and can generally go unnoticed.
Up until today, the only way to prevent PHP crashes on Windows was to
run PHP as an external process to the Web Server via CGI.
CGI:
CGI stands
for "Common Gateway Interface". CGI is a communication platform for a
Web Server to speak with Web Server applications such as PHP. CGI was
created as the standard interface for Web Server applications. Its main
benefits are simplicity, language independence and most importantly -
process isolation. This isolation prevents known bugs in PHP from
crashing the Web Server. CGI also has some significant drawbacks -
mainly performance. The CGI mechanism involves huge overhead resulting
in poor performance caused by the initialization of a new PHP process
for each request, and then its shuts down when the request is done.
Native Web Server Modules:
In response to the performance problems of CGI, several vendors have
developed APIs for their servers. Well-known APIs are NSAPI from
Netscape and ISAPI from Microsoft. The APIs enable PHP to run in the
server process, which is persistent across all requests, saving
initialization overhead. APIs are naturally used when PHP is run on a
single process server, but that’s not the case for multi-threading
servers. While parts of PHP were rewritten to make it thread-safe, the
thread-safe version never matured to be as stable and as efficient as
the multi-process version. In a nutshell, the smallest bug in PHP, one
of its modules, or one of the 3rd party libraries that it uses, that
causes one thread to become corrupted, can infiltrate all server
threads and bring the entire server down. For that reason, implementing
PHP on Windows using a native server plug-in (whether IIS or Apache)
proved to be impractical, resulting in frequent, yet random crashes.