Thursday, January 3, 2008

Installing Apache and PHP

Before I can do anything with PHP, my computer needs to be able to recognize it, so I have to install it.

Installing PHP

The simplest way for me was downloading a Windows installer of the most recent version (5.2.5 in my case) and running the installer. You don't need to do anything fancy, just locate the installation where you would put all your regular program, such as in c:\Program Files\PHP. No problem, I thought. But if you write some PHP code, call it test.php and open it with a web browser, the browser will just show you the code, exactly as you entered it. That's because PHP is a server-side scripting language, so there must be a server to recognize it and parse it. Enter the "A" part of the LAMP stack: The Apache web server.

Installing Apache

To get Apache, download the windows installer, run the installer, and install it wherever you like. The tough part is configuring it, which is done through the httpd.txt file, which in my installation is located at C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.txt. When you open this file you are confronted with a mess of hash marks (#) and scary-looking commands. The hash marks are actually there to cancel out the commands that they precede, which means that in order to enable a command, just delete its hash mark. Anyway, here's the important stuff:
  • ServerRoot "C:/Program Files/Apache Software Foundation/Apache2.2/" - this has to point to the directory where you installed Apache. If you used the windows installer it should be properly filled out.
  • ServerName localhost:80 - this what you put if you're using your own computer as a server, which you are if you're installing it on your own machine. Open a web browser and type in "localhost," and you're accessing your own Apache server, in essence serving a web page to yourself. "80" refers to the port, which is probably set by default if you used the windows installer.
  • DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs" - this points to the folder that "localhost" becomes. Anything in the folder is being hosted by Apache. So if you put a file called "index.html" in the "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/" folder, then you can open it by pointing a browser to "http://localhost/index.html." Neat.
These settings are probably enabled by default (as well as a slew of others), if you used the windows installer like I did. Test the installation by opening a browser and typing in "localhost." If it says "It Works!" then the server is working. Remember that "localhost" just points to the server, which opens C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\index.html. So you can edit index.html to make it say anything you want it to, or even replace it with another file called index.html. "localhost" will point to it every time.

If add a bit of PHP code to index.html, the server won't recognize it unless you have installed and configured PHP. If you open the file via "localhost" you won't see any difference. The file will look the same as if you had opened it from the httpd file folder, i.e. not running on a server.

Back to PHP

So now the PHP installation comes in. If you run the windows installer and the program executes, it added a bit of code the very bottom of the config.txt file (C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.txt).
PHPIniDir "C:/Program Files/PHP/"
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
These two lines tell Apache where PHP installed its .ini file, and it told Apache to load another module (the php5_module) and where to find that module (in the PHP installation directory). The php.ini file is just like Apache's config.txt file - it's a list of commands, most of which are blocked by comment characters (semi-colons instead of hashes). If you used the windows installer, these commands should be set correctly. Test the installation by adding some php code to the index.html file, as above. If the browser render the php code correctly (i.e. without the php tags) then PHP is installed and working.

1 comment:

AlexW said...

If you want to save your time, use XAMPP software bundle or WITSuite EasyInstaller (WITSuite installation video: http://www.witsuite.com/products/installer/). It is most simple way to install Apache, PHP, MySQL on Windows.