create your own

Using PHP on Windows

74
rate or flag this page

By adyesha



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.

Installation Hints

And now the part you've been waiting for, practical application. Installing PHP isn't as hard as you think, so don't get discouraged. First of all you'll need to download a binary (big plus on Windows, you don't have to compile your own). Don't be scared of the five different links to get PHP for Windows. But let's have that talk about the two versions of PHP.

PHP has two distinct versions, a thread safe version that runs a little slower but doesn't let competing threads kill each other, and a faster version that isn't thread safe. The thread safe version uses ts to identify itself, the non-thread safe version doesn't. This is why if you look in your PHP directory, you'll have php5ts.dll if you're running a thread safe version of PHP, and a php5.dll if you're not. If you're going to use mod_php with Apache or ISAPI with IIS you'll need the thread safe version of PHP. You can also simply use the nice PHP installer and it will set everything up for you.

Remember that your web server needs permission to use the PHP files. This is usually not an issue with Apache, but the newer versions of IIS run under a "Network Account" that will need to be given execute permissions on the files.

The installer can also set up FastCGI. On IIS I wouldn't recommend it, for the simple reason that FastCGI doesn't need to be thread safe. This can save you a lot in speed, if you don't mind taking the time to install things by hand. So if you're going the FastCGI route, grab the non-thread-safe Windows binaries.

Now, FastCGI is not included by default in Apache, IIS 5.1 or IIS 6. FastCGI is included with IIS7 on Server 2008 and on Vista if you install Service Pack 1. To use FastCGI with IIS just download the FastCGI installer from http://IIS.net for your system and version of IIS. Apache has two modules that allow you to use FastCGI - mod_fastcgi from http://fastcgi.com and mod_fcgid. One of these modules must be used in order to have FastCGI support for Apache.

The web server you choose needs to have permission to execute your php binaries. Permissions are usually the issue when you have problems after installation. For additional debugging, Process Monitor is a great (and free) tool to help figure out access issues.

Remember if you're using PHP sessions, your web server needs to have permission to read and write to the directory you choose in your php.ini. The same applies to file uploads. If you are getting errors with either of these items check your permissions.

After the Installation

So now that your PHP installation is finished and you have a pretty "phpinfo" page showing, what happens? Well, first of all you may want to change some of your configuration settings. This is usually done with your php.ini file. By default, PHP will look in your %WINDIR% (which is usually C:\WINDOWS) for that php.ini file. But you really don't want to put it there. There are different ways of telling PHP where to look for your php.ini file. The only way that works properly with PHP using FastCGI is to have an environment variable called PHPRC that has the path to the directory where your php.ini is stored.

Also you might want several php.ini files. Maybe you want one with settings for command line PHP (using php.exe) and one with settings for your IIS ISAPI, and maybe you installed Apache and mod_php as well.

PHP has a solution for that. The trick is naming your php.ini files a little differently. For your command line ini file, call it php-cli.ini Your ISAPI php.ini file can be known as php-isapi.ini. Apache can have the php-apache2handler.ini name. PHP will automatically use the ini file with the extension that matches the PHP SAPI you're using. You can figure out how to name your ini files by using the function php_sapi_name();.

You can also download PECL extensions from http://pecl4win.php.net, or grab the packs distributed on http://php.net. Remember, you can't use extensions built for a thread safe version of PHP with a non-thread safe PHP. Make sure your extensions version and thread safety match your PHP! Just put the extensions you want to use into your PHP extensions directory, and add a line that says extension=php_mysql.dll (or whatever the name of the extension is that you want to enable). If you have problems with extensions not loading correctly check the following:

* That you have your extension_dir set properly in your php.ini file * That your web server has permission to access the extension dll * That any additional libraries that extension needs can be loaded by PHP * That you've restarted your web server This means if you're trying to use php_mysql.dll, which requires libmysql.dll, that you need to add the location of libmysql.dll to your PATH variable.

Once you get over that fact that yes, you are using PHP on Windows, you'll find you forget about the technology underneath and return to the fun of developing new applications with PHP.

Here is some trivia; you can have a functioning Windows CLI version of php with three files, php.exe, php5(ts).dll, and a php.ini file. Dump them onto a flash drive and have fun anywhere.

Print   —   Rate it:  up  down  flag this hub

Comments

RSS for comments on this Hub

artfuldodger profile image

artfuldodger  says:
8 months ago

very solid, adyesha. great information for people getting started with web design.

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working