Using PHP on Windows

by Elizabeth Marie Smith (2008-03-03)
  Page: 1  2  [next]

Contrary to popular belief, PHP on Windows is not an oxymoron. The decision to use Microsoft Windows systems as the host for a server operating system, usually a decision made by someone else, doesn't have to send you running for the latest ASP.NET book. PHP runs great on Windows, whether you have legacy applications already running on PHP or intend to write new applications without learning a new language you shouldn't need those anti-anxiety medications. You just need to understand some basics of Windows and how PHP interacts with it.

Windows Basics

First of all, some bad news. PHP has dropped support for older versions of Windows, namely Windows 95, 98 (in its myriad forms) and ME. If you are using those systems I would highly suggest an upgrade. They are a poor choice even for a development server. And the corresponding good news? Microsoft has dropped support for those systems as well so an upgrade is probably already on the “to-do” list already. You may be able to get PHP running on 98 or ME, but why even bother since the host environment will be(should be) very different from your deployed server?

Now a short speech about dynamic libraries on Windows. If you come from a Unix or Linux background you know that “shared objects” (.so files) allow you to extend the functionality of a binary executable. Windows uses file extensions to differentiate types of files. Your executable files, which normally have no file extension in the *nix world, have .exe in windows. For example the PHP command line interface in windows is called php.exe. And those shared objects you have are replaced on Windows by dynamic link library (.dll) files. An example of this is the php5ts.dll (or php5.dll - we'll get to why there are two versions later) file you'll find in the same directory as php.exe. Don't let the file extensions trip you up; they're just a way of helping you understand what each file does.

In order for an executable to load a .dll file, it needs to be able to find it. Windows does this in three ways. First of all, when a .dll is requested it will look in the same directory as the binary that requested the .dll. Then it looks in the system directory (usually c:\windows\system32) and then in the windows directory (usually c:\windows). If it can't find it there it will use an Environment Variable called PATH. Yes this is the same as *nix systems. Try NOT to put items into the system directory unless you have to, it makes upgrading difficult, and any old .dll's left there will be loaded before new .dlls that might be in your PATH. This behavior is where the term “dll hell” comes from, the wrong .dll getting loaded because some program threw it into the windows system directory instead of adding an item to the PATH.

The last Windows basic we'll cover is file permissions. Instead of the Unix-style permissions, Windows uses Access Control Lists to keep track of permissions. But just like Unix style permissions, the process PHP is running inside has to have permission to perform certain actions such as reading and writing files.

Servers on Windows

If you want to use PHP for creating websites, you'll need a web server. There are basically two choices for Windows, Apache or IIS. There are some other more esoteric servers but none have the market share, support, or community involvement associated with Apache and IIS.

Apache is the Apache Server Project, an Open Source cross-platform server. If you intend to use Apache on Windows you should use the latest 2.x version since improvements are constantly being made in stability and speed for Windows. It is very important that you also keep your installation updated to the latest version; Windows is often much more susceptible to bugs and security holes. Installation is also easy with a simple Windows installer file (.msi) and it runs on Windows 2000 or higher.

IIS or Internet Information Services is Microsoft's server offering. Version 5.1 is included with XP and has limited capabilities, as it is meant to be a development only server. Version 6 is designed for Server 2003 and is very similar to 5.1, but has additional features. Version 7 is the latest and has a very different architecture from the earlier versions, taking a modular approach to its design. It also has much easier configuration, but is designed for Vista or Server 2008 and will not run on earlier systems. IIS versions before 5.1 are highly unstable and insecure so please do not use them. Installing these servers differs for each operating system. Because installation varies and can be complicated, here are detailed instructions for XP, Server 2003, Vista, and Server 2008.

There are two different ways that you can install PHP for use with Apache and IIS. The first way is as a module. On Apache this means mod_php5 and is what most people use. This is usually a great fit for development boxes that will deploy to other machines. On IIS, the “module” approach is PHP ISAPI. This is prone to crashes and is generally flaky and is not recommended. The second way you can install PHP is using FastCGI.

FastCGI is an extension to the old CGI method of allowing a webserver to pass a request to an external program for processing. So what's so great about FastCGI and PHP? To understand what makes it so great you need a short overview of threads and processes so put your computer science cap on. Threads and processes are ways to make a program do several things at once – commonly called parallelizing. A process is an independent unit with its own state information, addresses space, and interacts with other processes using special interprocess communication methods. A thread performs a unit of work, like a process, but it shares a parent process with other threads and they share state information and address space.

Unix style systems use a “process” model instead of a “threaded” model for most parallel activities. They fork a process for new activities or jobs, and since the systems are more optimized for this activity, the overhead is small. Windows takes a threaded approach to the same problem; new activities or jobs spawn a new thread and the system is optimized for this, so spawning a new process is fairly resource intensive but spawning a new thread is cheap.

PHP started life in the “process” world, where nothing is shared. As a result, many of the extensions that make PHP so fun and popular are not entirely thread safe and are prone to crashing. CGI spawns a new process for each request to the web server. Multiply this by an average day at any website and the speed impact is obvious. FastCGI speeds this up with a very simple solution– instead of creating a brand new process for each request, it creates a pool of processes and reuses them, meaning an instant speedup. But since the processes don't share anything, you don't get the crashes.

You can use FastCGI with Apache and with IIS, but the FastCGI support for Apache is not as stable and tested. In fact, you probably shouldn't use anything but FastCGI with IIS when you need PHP on Windows.

File under: art  fastcgi  homepage  iis  php  windows 
  Page: 1  2  [next]

Comments

Re: Using PHP on Windows by mark (2008-03-04 01:15:07 (America/Toronto))
What about performance ? Sure, the fastcgi handler is lightning fast, but some benchmarks would be interesting. Especially using opcode cache since the opcode engines (apc, xcache etc.) can't access shared memory between fastcgi processes. I don't mean to sound like an ass, but I don't really see how windows can be used to host high traffic sites running php, without fully functioning opcode cache.
Visit the forum