Wednesday, August 6, 2008

How to configure PHP 5 and Apache 2.2

Hi all!

I faced lots of problems while setting up PHP and Apache web server. It took almost 2 days for me to set it up:-(. So I thought I have to write down all the steps I had done to set up so that it can be useful for someone who are intending to setup.

Step 1 - Download PHP 5 zip package and Apache 2.2 installer. Make sure you are downloading only the Zip package for PHP. Never download the PHP installer. Because it does not contain all the support dlls.

Step 2 - First install your Apache 2.2 web server in your local machine. By default it'll install Apache in the folder "C:\Program Files\Apache Software Foundation\Apache2.2"

Step 3 - Click on the "Monitor Apache Services" menu and see all the Apache services are running properly. You'll get an icon on the taksbar .

Step 4 - Create a new folder called PHP under C:\ and unzip the PHP zip package under this folder. Few important files available in that folder are

php.ini-recommended
php.ini-dist
Php.exe
Php5ts.dll
php5apache.dll
php5apache2.dll
php5apache2_2.dll
mysql.dll

Step 5 - Rename file "php.ini-recommended" to "php.ini". Plz take a copy of this file before doing that. Because we have to change some configurations in this file.

Step 6 - Copy file php5ts.dll to windows system32 folder and Apache server's Bin folder.

Step 7 - Make following changes to 'php.ini' file. Find out "doc_root" keyword in php.ini and set the Apache HtDocs folder path here as shown below.

doc_root =C:\Program Files\Apache Software Foundation\Apache2.2\htdocs
You can specify the path of HtDocs folder where you installed Apache.

This is the only configuration I have changed. If you want to change , you can do that according to the Install.txt

Step 8 - Copy Php.ini to C:\Program Files\Apache Software Foundation\Apache2.2 folder.

Step 9 - Now we are going to touch another configuration file 'Httpd.conf' under Apache folder. You can find this fle under the "Conf" folder (C:\Program Files\Apache Software Foundation\Apache2.2\Conf). Before making any change, plz take a copy of this file.

Step 10 - Search for "ServerRoot" configuration. You can find the folder where you installed Apache. No need to change this. Just verify whether this is correct.

Step 11 - Now you can see configurations like Listen 12.34.56.78:80 and Listen 80. If you are configuring Apache in your local machine, instead of these configurations, you can have LISTEN localhost:80.

Step 12 - Next configuration is LoadModule. You have to add the following line under the LoadModules available in the httpd.conf file. You can find so many loadModule statements in the httpd.conf file. Just add the following under the last LoadModule Statement.

LoadModule php5_module "c:/php/php5apache2_2.dll"

If you have installed Apache 2, then you have to add the following line.

LoadModule php5_module "c:/php/php5apache2.dll" . Verify this .dll file is available under the PHP folder. So depends on the version of Apache server, you have to add the respective LoadModule statement.

Step 13 - Add the following lines under LoadModule statement.

SetEnv PHPRC C:/php
Action application/x-httpd-php "c:/php/php-cgi.exe"

Step 14 - If still you have the PHPRC configuration, sometimes it won't work. Better you can add the path in the Windows Environment variable.

Steps to add an environment variable:

1. Select "MyComputer" icon in your desktop.
2. Right click the icon and select the "Properties" menu. System properties dialog box will pop
up.
3. Select "Advanced" tab. In the bottom of the window, you can see a button with caption
"Environment Variables"
4. Click that button and a new dialog box comes up with all available environment variables.
5. In this dialog , you can see 2 lists . One for User variables and one for System variables.
6. Select the Path variable in the "System variables" list.
7. Click on the edit button and you'll get an edit window for this system variable.
8. Go to the end of the "Variable Value" and add following text
;C:\Php
9. Click Ok buttons continuously to save the environment variable.

Step 15 - Search for DirectoryIndex configuration. You can find the following line

DirectoryIndex index.html
Replace this line with
DirectoryIndex index.html index.php index.html.var

Step 16 - Search for the module name "mime_module". You have to add following lines under this module .

AddType application/x-httpd-php .html .php
AddType application/x-httpd-php-source phps
AddHandler php5-script php
AddType text/html .php

Step 17 - You have done all the configurations. Now restart your machine and start your Apache web server.

Step 18 - Copy your Index.html to C:\Program Files\Apache Software Foundation\Apache2.2\htdocs

Step 19 - Open your browser and type http://localhost/ and see whether you are getting the output of the index.html you have copied in step 18.

Step 20 - If your site is working fine, you have done the setup :-).

Now you can create your own PHP files and try with Apache server.
Have a good day!

1 comment:

Anish said...

apart from programming languages,I think there is no relation between delphi and php