If you’re like me, you prefer installing Apache and PHP on your own without the use of WAMPServer. The goal is to setup the latest Apache server with PHP 5 and PHP 4 using FastCGI.
If you’re like me, you prefer installing Apache and PHP on your own without the use of WAMPServer. Typically I install PHP as an Apache module, but this time I wanted to try using FastCGI and get acquainted with the setup. I’ve just finished setting up my laptop as an extra dev machine and have been using it with no issues.
The goal is to setup the latest Apache server (2.2.17 as of this writing) with PHP 5 and 4 (5.3.3 and 4.4.9) using FastCGI (2.3.6). I will be making the assumption that you’re pretty familiar with computers and perhaps have done this once or twice before.
Getting It All Installed
You will first need to get the install packages/binaries for Apache, PHP 5 (read the V6/V9 instructions carefully), PHP 4 (or other releases) and FastCGI.
- Installing Apache is pretty straight forward, I am using the MSI installer and the wizard pretty much guides you through the installation. I generally install Apache at “C:/bin/apache”.
The PHP 5 installation is also pretty straight forward, I like to use the MSI installer to make things go a little quicker and I like being able to select/deselect the extensions I want activated.
During the wizard setup you will be prompted select a web server setup, select “Other CGI” (if Apache is currently running, stop the service so the installer can properly access the httpd.conf file). I generally install PHP at “C:/bin/php”.
- If you want to install PHP 4, simply get the latest ZIP release (4.4.9 as of this writing), unzip it and set it up at “C:/bin/php-4.4.9”.
- Open up the FastCGI ZIP package, you will need to place the “manual” and “modules” folders in “C:/bin/apache”.
Open up “C:/bin/apache/conf/httpd.conf” and do the following:
- Find the default
<Directory>block, find the
Optionsdirective and add
- Find the
DirectoryIndexdirective and add
- Find the
#Include conf/extra/httpd-vhosts.confand uncomment the line
At the bottom of your httpd.conf file add the following, my PHP installation is at “C:/bin/php” (if yours is different you may need to make adjustments):
LoadModule fcgid_module modules/mod_fcgid.so FcgidInitialEnv PHPRC "c:/bin/php" AddHandler fcgid-script .php FcgidWrapper "c:/bin/php/php-cgi.exe" .php
Important: If you install PHP in “Program Files” or any directory containing a SPACE in the name, you will need to use an 8-char DOS directory name.
This won’t work:
FcgidWrapper “C:/program files (x86)/php/php-cgi.exe” .php
But this will:
FcgidWrapper “C:/progra~2/php/php-cgi.exe” .php
If you are having problems restarting the apache service, I find that a computer reboot helps.
Virtual Hosts Setup, With PHP 4
I am going to show you the way I setup virtual hosts on my system. I use the “httpd-vhosts.conf” file to define the vhosts that I need. I typically assign a project to its own dedicated virtual host.
I also use the system “hosts” file to assign custom domain names to each vhost. I use “.dev” as the TLD for all my vhosts (you’ll see what I mean shortly).
First off lets setup the vhosts, here is my “httpd-vhosts.conf” file:
NameVirtualHost *:80 <Directory "C:/bin/apache/projects"> Options FollowSymLinks Includes ExecCGI AllowOverride All Order allow,deny Allow from all </Directory> <VirtualHost *:80> ServerName farinspace.dev ServerAlias *.farinspace.dev DocumentRoot "C:/bin/apache/projects/dimas/farinspace" </VirtualHost> <VirtualHost *:80> ServerName wpalchemy.dev ServerAlias *.wpalchemy.dev DocumentRoot "C:/bin/apache/projects/dimas/wpalchemy" FcgidInitialEnv PHPRC "C:/bin/php-4.4.9" FcgidWrapper "C:/bin/php-4.4.9/php.exe" .php </VirtualHost>
So lets talk about the file … you’ll notice that I setup a “projects” folder at “C:/bin/apache/projects”. Each of my projects are then subdivided in this folder, see “farinspace” and “wpalchemy”.
Also notice the
ServerAlias directives, when I type “http://farinspace.dev” or “http://wpalchemy.dev” in my browser I am able to access each vhost by name (remember, before you can do this you will still need to setup your “hosts” file, see below).
The second vhost above uses PHP 4 (you can setup a vhost with any version of PHP that you need). The two directives that you need to use are
FcgidWrapper, you will have to make adjustments depending on the version and location of your PHP installation.
Here is my “hosts” file:
# Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address <span class="hiddenGrammarError" pre="name ">should # be</span> placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 184.108.40.206 rhino.acme.com # source server # 220.127.116.11 x.acme.com # x client host # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 127.0.0.1 farinspace.dev 127.0.0.1 www.farinspace.com 127.0.0.1 assets.farinspace.dev 127.0.0.1 wpalchemy.dev 127.0.0.1 www.wpalchemy.dev
As you add more Apache vhosts, simply modify your “hosts” file with new names (you will likely need administrator rights to edit this file, run your editor as administrator).
At this point you will need to restart Apache and create yourself a “phpinfo.php” file to test your installation.
14 responses to “Windows Apache PHP FastCGI Setup”
I’ve always ran php as an apache module, but since 5.3, the thread safe versions and the zend debuggers no longer agree. So like many other windows zend debugger users, I’m switching to CGI. I didn’t realize how much faster fastcgi is until I followed your excellent instructions and got it going.
Thanks again for sharing!
With my existing apache 2.2.9 – I don’t know in this case Apache version matters – I installed php5.3.4 on my Vista box as you described. Everything worked fine, but only the non thread safe v6 version worked, the thread safe version broke with unmature end of script headers.
thank you very much for your information on installing fastcgi for apache.
i run it on windows 7, everything fine as your recommend.
Thanks for documenting this. I have tried several times unsuccessfully to get this working, but this tutorial took the confusion out of the process.
I am running Apache 2.2 with PHP 5.3.3 VC9 NTS on Windows 7.
You dimas! ..I mean ‘da Man’. 🙂
Worked great and fixed my ZendDebugger issue.
I am running Apache 2.217 with PHP5.3.6 VC9 NTS on Win Vista.
I am having issues getting this to work, everything works fine but my website keeps saying Forbidden when I try to access a .php file
You don’t have permission to access /phpmyadmin/index.php on this server.
it works fine with index.html files so I suppose something is wrong with the php installation or something else? but I already tried editing the apache config and googling a lot with no success…
and yes I did do: DirectoryIndex index.php, idk how to fix this please help me.
@gijs, before trying to get started with your phpmyadmin, do a basic test with a phpinfo.php file. Apache config has never been my strong point, but I am persistent. You may need to get it to a working point and then slowly add/remove directives to see what works and what doesn’t. Additionally I have been puzzled at times and a reboot (for what ever reason) has helped clear issues.
This might be late but gijs might be missing the ExecCGI option in his httpd directory configuration
I found I had to add Indexes and ExecCGI options to and also to the directory block for the document root in order to make this work.
Thanks — very helpful!
Thanks for an easy-to-follow guide on this set-up. It is perfect for someone new to working with Apache.
I am having trouble understanding one part. In the Installation section step 4, you say to unpack the FastCGI files and place the manual and modules folders in the Apache directory. Those directories already exist. Should I assume I am to move files from the FastCGI directories into these folders as they fit? Can you elaborate a bit on where exactly these files go, please?
Thanks a lot! This is the only guide that actually worked for me.
I am beginning to return to web programming after a substantial hiatus (due to extreme illness) and am working on setting up Apache 2.4 along with all else I need for web programming. First question: “Has anything of significance changed in these packages or the server since this was written?”
Most of my web programming experience, from years ago, was done using Perl (which I am getting back to), but I also have an interest in PHP, python and ruby. I never had set up anything for cgi programming except mod_cgi. But I want to experiment with FastCGI, so this page is priceless to me. But what is the relation between this package (mod_fcgid) an modules I have seen, but not yet downloaded, such as mod_FastCGI, mod_perl, mod_php, mod_ruby, mod_python, &c. I want to strengthen my understanding of PHP and learn ruby and python.
I also want to do a bit of experimenting, testing and comparing each package that purports to provide FastCGI services (on my Windows box) with the ancient default of mod_cgi.
Also, I need to know, if anyone here knows, whether or not the FastCGI libraries will work properly with products out there such as WordPress.
Any guidance will be appreciated. Thanks.
Ted, while I haven’t used this exact setup in years, Apache and FastCGI/FPM are still relevant, my article may be outdated, so I do encourage you and others to continue to search. However, the one tech I would recommend investing some time in would be “Docker”. Additionally, “nodejs” has interwoven itself into nearly every part of my dev process. Good luck!