What are the basic configuration settings I need to run PHP with Apache?

There are 9 basic steps to setting up Apache to work with PHP. The following steps (in chronological order) will help you get there, but some of the values will be different for your set up. In the httpd.conf file make the following changes:

Step 1

Add this to the LoadModule Section.

LoadModule php5_module c:/php5/php5apache2_4.dll

Step 2

Immediately after the LoadModule Section, add the code shown below. Comment is self explanatory.

# configure the path to php.ini
PHPIniDir "D:/php-5.4.1"

Step 3

Change Servername to Localhost

Step 4

Change DocumentRoot to where you want the webroot to be located. E.g.

DocumentRoot "C:/MyWebpages"

This directory resolves back to localhost when used as part of the URL. Thus, to access a file (file1.php) located in the MyWebpages directory via the web-browser use this URL.


Step 5

Find this section of code in the configuration file. Follow the instructions to change the directory to whatever you specified the DocumentRoot to be from step 4.

# This should be changed to whatever you set DocumentRoot to.
<Directory "D:/Program Files/Apache Group/Apache2.4/htdocs">

Step 6

Find this section of code in the configuration file.

# DirectoryIndex: sets the file that Apache will serve if a directory is requested.
<IfModule dir_module>
    DirectoryIndex index.html index.php

Add the index.php if you want Apache to automatically search for index.php in the directory if no webpage was specified. E.g. if you went to http://localhost, Apache will first search for index.html to display in the webbrowser, and then index.php to display in the browser if index.html was not found.

Step 7

Find this section of code in the configuration file. Goto the end of the section and add the x-httpd part.

<IfModule mime_module>
    . . . 
    AddType application/x-httpd-php-source .phps
    AddType application/x-httpd-php .php

The .phps extension means that any file with this extension has the php code displayed (and formatted) on the browser instead of being executed. This is handy if you want to demonstrate code use etc.

Step 8

Install Apache as a service. From the command line (and from the bin directory of Apache24), enter the following instructions.

httpd -k install

This will install Apache as a service. This means that Apache will automatically start up and run when you turn the computer on.

Step 9

Make sure you restart your computer. The computer needs to be restarted in order for the changes to take effect. Attempting to start Apache manually without restarting will cause you no end of grief.

That's it. If everything has been done properly PHP should be ready to run through Apache.



Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.