Although I personally prefer an alternate webserver setup (*cough*), for the standard mod_rewrite use and a mostly installer-based setup, there's always the ever-so-popular Apache, which is used in popular distributions such as XAMPP, WAMP, AppServ, etc.
In this guide, however, we won't be using somebody else's pre-configured configuration files, with things enabled we don't need. Instead, we'll be using recommended production defaults (you can tweak yourself if need be).
On Windows, there is a benefit to Apache; with FastCGI Windows setups (i.e. nginx, lighttpd, even IIS if it's not setup as ISAPI) it can end up being slower, at least intermittently. nginx is great at what it does, it's lightweight, efficient and hence performs a lot better than Apache, however, on a Windows setup, FastCGI setups do tend to cause unwanted delays (it appears, every time it needs to reconnect). With Apache however, we load up PHP as a SAPI module and pass off all requests internally. There's no sockets delays between the webserver (Apache) and the scripting engine (PHP).
Additionally, although there is alternatives, Apache's got the popular .htaccess and mod_rewrite use.
So let's get started then!
Pre-requisites
- The current latest stable version of Apache 2.2, which you can find under "Win32 Binary including OpenSSL 0.9.8r (MSI Installer)". Current as of this post is:
- . Current stable version is:
Getting started - installing Apache
For this guide, I'll be using the installer as it covers the extraction, providing default configs, service startup (and an accompanying service monitor), and a shortcut to your config file. This helps simplify the following guide immensely.
To start off with, we'll need to run the installer, as in the above screenshots (note: screenshots proceed in the order of left to right, top to bottom).
- Hit "Next" on the first window (#1) to continue.
- Tick the "I accept" option, to indicate that you've accepted to the agreement on image #2, then hit "Next" again.
- Hit "Next" once more (#3) to skip to the "Server information" window.
- On the "Server information" window, we're assuming you own a domain and have it pointing to your box. If so, fill out the details as specified in the example. If you DON'T have your own domain, which will probably be most of you, then don't fret - it's a (somewhat) optional setting (the installer requires you specify it however) so you can just specify a "." (or anything really), and Apache will detect what it needs to use automatically.
- Hit "Next" once more to proceed."
- We do not need a custom setup (the default is fine), so make sure "Typical" is selected, then hit "Next" on the first window (#1) to continue.
- By default, Apache likes to sit itself in the "Program Files" directory. This can cause unnecessary complications with permissions, so I prefer just moving it out to C:\Apache\ (feels neater, anyway). You can place it wherever you want (just remember where it is for future references in this guide!), but for the purposes of this guide I'll be using C:\Apache\, so hit "Change.." and set it to C:\Apache\, then hit "Next" (again!).
- Finally, hit "Install" in image #3 to start installing Apache.
All done!
Now that it's done, let's open up our browser and go to:
Code:
http://localhost
Onto PHP!
Installing PHP
Now, let's run the PHP installer we downloaded earlier.
Again (this is getting a little repetitive :P), hit "Next" on the first window, tick the "I accept" field and hit "Next".
As with Apache, I prefer as little complications as possible with my setups, so set the install path to "C:\php\" (but as before, you can change it if you wish - just so long as you remember where it is and adjust instructions as required).
As in the last image, tick the "Apache 2.2.x Module" option (as that's the one we're using), and hit "Next" to proceed.
Now it'll ask you to set the Apache configuration directory, which is (if you're following the instructions so far!), "C:\Apache\conf". If you changed it, just add "\conf" to the end of your install path, so you get something like "C:\Wherever I put it\Apache\conf". Hit "Next" once more, and you'll be presented with a feature list. I use the above setup (Script executable + PEAR), as I do make use of the script executable option, and PEAR is often handy... but for simplicity, you can just leave it default (but you're welcome to set it the same :P) and hit "Next".
Finally, we're up to the install stage. Hit "Install" and let it do its thing. As above, it should tell you that you'll need to reboot your PC for everything to work correctly. You can promptly hit "OK" and otherwise ignore that; we won't (need to) be rebooting.
If all's gone well, as in the above image, you're done installing PHP. It'll ask you to reboot, but just hit "No". If you really want to reboot, you can wait until after the guide!
Onto the configuration!
Configuring our webserver
Now we'll need to load up Apache's main configuration file (httpd.conf), which we can do by going "Start -> [Program Files] -> Apache HTTP Server 2.2 -> Configure Apache Server -> "Edit the Apache httpd.conf Configuration File", as in the above image.
With httpd.conf currently loaded in Notepad, we'll start configuring in the order the settings appear in httpd.conf for simplicity, so first up is enabling mod_rewrite (disabled by default).
Press CTRL+F, search for "rewrite_module" and hit "Find Next". On the line that it finds, remove the pre-leading "#" (that is, we're "uncommenting" the line) so the line becomes the following:
Code:
LoadModule rewrite_module modules/mod_rewrite.so
To enable the use of .htaccess, we'll need to allow for setting overrides. So press CTRL+F again, and search for "Options Indexes FollowSymLinks". Underneath which, you should see the "AllowOverride" setting, set to "None" (disallowing overrides). Set AllowOverride to "All", as follows:
Code:
<Directory "C:/apache/htdocs"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.2/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride All # # Controls who can get stuff from this server. # Order allow,deny Allow from all </Directory>
Now we need to setup the directory indexes, so that Apache also checks for the existence of index.php (as well as index.html) when no file is specified, i.e. by simply going to "http://www.mysite.com" (see how we don't specify the file we're trying to access, in this case index.php?).
So press CTRL+F, search for "<IfDir dir_module>" (or just "DirectoryIndex"), as above. As in the above screenshot, add index.php before index.html (that means it checks for index.php BEFORE index.html, so if both [index.html and index.php] exist, Apache will "choose" index.php), so the config block now looks like:
Code:
<IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
Two more things - firstly, tweaking the PHP installer's slight mistake with the PHPIniDir setting (which can cause a bit of grief), and configuring it to handle (only) files ending in ".php". As in the above image, press CTRL+F and this time, search for "LoadModule php5_module".
Fix up the PHPIniDir setting to read (otherwise you may face some nasty "unusual" crashes on startup, referencing php5ts.dll):
Code:
PHPIniDir "C:/php"
Code:
<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
Before we restart Apache, we need to make one more tweak, but this time to our PATH environment variable. This is so that everything can find each other correctly, and has the added benefit of being able to call (if you enabled the "Script executable" option in the PHP installer) php.exe from anywhere, via command-line.
As in the above screenshot, click on "Start", right-click on "[My] Computer", and select "Properties".
Click on the "Advanced" tab, and then - at the bottom! - find and click on the "Environment Variables" button.
In the BOTTOM section, titled "System variables" (not the top; that is for the current user only), find the "Path" variable. Select it, then press the "Edit" button below the list. Add the following to the very start of your path:
Code:
c:\php;
In a nutshell, the path variable's used as a list of "known locations" for applications and their libraries (that is, including DLLs). If it doesn't know where a file is, it'll check each and every path in that list in order to find it.
Once added, you can "OK" all the way out of there. Just close it all off, we're done there.
Now that we're all configured, you'll need to restart Apache to load our new config (and hence, PHP).
From your task tray, you should see this icon (note: it didn't quite show up the in the screenshot very well, but it's the one on the left). Right-click this icon, and open up the Apache monitor
From here, select Apache2.2 and just hit Restart. Easy as that!
Finishing up
Now that Apache's all configured (we shouldn't need to touch the PHP config - the PHP installer did this for us using the production defaults as a base), and our changes are effective, let's test that our config is working correctly.
Open up "[My] Computer", and browse to C:\Apache\htdocs. This is where our website will go.
Firstly, if you cannot see file extensions, you'll need to enable them (otherwise you'll end up making a file that is really named index.php.txt, despite it simply showing index.php) - go "Tools" [if on Vista+, hold ALT down to see the menu!], "Folder Options..", "View" tab, and untick "Hide extensions for known file types". You can see this ain the following screenshot:
If that's fine, we can now create a new (text) document named index.php in C:\Apache\htdocs, which will contain the following:
Code:
<?php phpinfo();
Open up your web browser, and again, navigate to:
Code:
http://localhost
You should now have a perfectly functioning webserver running on Apache!
Have fun.