Thursday, October 18, 2012

WAMP Step 2: Install PHP

As detailed in our previous post, we are building towards a WordPress installation on a desktop PC. In step 1, we successfully installed and tested the Apache web server.

As mentioned last time, some documents that are served by the web server to your web browser are static documents - just files sitting on the hard drive. Other documents are actually queries into a database, where the data has been dressed up and presented as a web page. Still other documents are mostly static, but with some customization by a script running on the server.

To support WordPress, we are going to need both a database and a script engine. A very popular scripting language for web pages is PHP, and that is what WordPress requires. The rest of this post will walk through installing PHP.




1 - Installing PHP

Go to PHP’s main website: www.php.net, to learn more about PHP, or head straight to http://windows.php.net/download/ to get the latest Windows binary install. Please read the warning on the left side of the page! Since we got Apache from apache.org, the last safe version according to this warning is 5.2.17. Click on the Installer link for the thread safe choice.
Locate the downloaded installer .msi file and double click on it to launch the installation wizard.
A “Welcome to the PHP X.X.X Setup Wizard” screen will appear.  Click “Next”.
Accept the terms of use. Click “Next”.
The install path should look like:
C:\Program Files\PHP\
Click “Next”.
Select how PHP will be used. For our situation, it should be “Apache 2.2.x Module”. Click “Next”.
Set the location of the Apache Configuration directory.  It should be:
C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\

The next screen will ask you what items you want to install.  Expand the “Extensions” section and click the arrow next to “MySQL”. Select “Entire feature will be installed on local hard drive”. Other extensions that will be useful to our intended WordPress setup are GD2, Multi-Byte Strings, mcrypt, XSL, and zip. Select these as well. I also went for the manual because I am a geek. Click “Next”.
Click “Install”.
Wait for installation to complete. Click “Finish”.
Keep the .msi file so that you can add new extensions from it later.

2 - Configuring Apache to work with PHP

Apache is configured through entries in several files. As I warned earlier, you must have Modify permission to edit and save the changes to these.
Go to:
C:\Program Files\Apache Software Foundation\Apache2.2\conf
Open “httpd.conf” in Notepad. This is Apache's main configuration file. You might want to save a copy of the original first. Notice that this file uses forward slashes (Unix style) instead of Windows-style backward slashes in directory names, and also doesn't put quotes around directory names that contain spaces.

Replace the line:
DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
With:
DocumentRoot "C:/sites/example/content"
This change tells Apache where the web content can be found, since we are not using the default location.

Replace the line:

With:

Ibid.

Replace the line:
DirectoryIndex index.html
With:
DirectoryIndex index.html index.php
This change tells Apache what files to look for as the home page for a web site. In addition to looking for index.html, Apache will look for index.php if it did not find the first file name in the directory.

The PHP installation process has added a few lines to the bottom of the configuration file:

#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir ""
LoadModule php5_module "php5apache2_2.dll"
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL


Add the following lines above the lines added by PHP:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
These lines tell Apache which files it should understand as PHP files - ones that contain ".php" in the file name. Not just at the end of the file name, like a Windows file extension, but anywhere in the file name. There are ways to change this to be more like our expectation.

The PHP lines also need editing to include the directory information. Add the path "C:/Program Files (x86)/PHP/" to both lines so that they look like this:


#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/Program Files (x86)/PHP/"
LoadModule php5_module "C:/Program Files (x86)/PHP/php5apache2_2.dll"
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL



Search for the line:
# AllowOverride controls what directives may be placed in .htaccess files.
Replace this line (which should be below the line you just searched for):
AllowOverride None
With:
AllowOverride All
Click File -> Save.
Click File -> Exit.

3 - Configuring PHP

This first part is entirely optional. Go to:
C:\Program Files (x86)\PHP
Make sure you have Modify permissions, and then open “php.ini” in Notepad.
Replace the line:
upload_max_filesize = 2M
With:
upload_max_filesize = 10M
Replace the line:
post_max_size = 8M
With:
post_max_size = 20M
Replace the line:
short_open_tag = Off
With:
short_open_tag = On
Replace the XXX on the line with your ISP’s SMTP server, YYY:
SMTP = XXX
With:
SMTP = YYY
Example:
SMTP = mail.earthlink.net
Save the file. Done with the optional part.

Now we are going to create our first scripted web page! Go to:
C:\sites\example\content
Create a file called “phpinfo.php” and open it with Notepad.
Enter in:
phpinfo();
?>
Save and close the file.
Restart Apache. As I said in the previous post, you can do this in several ways - through the Services tool, the Apache Monitor available in the System Tray, or with the command prompt. Here is yet another way:
Click Start->Programs->Apache HTTP Server X.X->Control Apache Server->Restart.
Now to test that Apache and PHP are correctly configured, go to:
http://localhost/phpinfo.php
You should see a web page with detailed information about your PHP configuration.

4 - Problems?

The main problems I encountered at this stage was the connection between Apache and PHP not working. This is why I changed the PHP lines at the bottom of the httpd.conf file - to make sure Apache knew where to find the PHP module.

No comments: