w3resource

Install PHP 5 on Apache in Windows

As Apache Module

To configure Apache to install PHP as a module, use an ASCII text editor to open the Apache configuration file, "httpd.conf". If you use Apache 1.x, the file is found in "c:\Program Files\Apache Group\Apache\conf\". Apache 2.0.x users can find it in "C:\Program Files\Apache Group\Apache2\conf\" while Apache 2.2.x users can find it in "C:\Program Files\Apache Software Foundation\Apache2.2\conf\". Basically, it's in the "conf" folder of wherever you installed Apache.

Search for the section of the file that has a series of "LoadModule" statements. Statements prefixed by the hash "#" sign are regarded as having been commented out.

If you are using Apache 1.x, add the following line after all the LoadModule statements:

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

If you are using Apache 2.0.x, add the following line after all the LoadModule statements:

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

If you are using Apache 2.2.x, add the following line instead:

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

Note carefully the use of the forward slash character ("/") instead of the traditional Windows backslash ("\"). This is not a typographical error.

If you are using Apache 1.x, search for the series of "AddModule" statements, and add the following line after all of them. You do not have to do this in any of the Apache 2 series of web servers.

AddModule mod_php5.c

Next, search for "AddType" in the file, and add the following line after the last "AddType" statement. Do this no matter which version of Apache you are using. For Apache 2.2.x, you can find the "AddType" lines in the <IfModule mime_module> section. Add the line just before the closing </IfModule> for that section.

AddType application/x-httpd-php .php

If you need to support other file types, like ".phtml", simply add them to the list, like this:

AddType application/x-httpd-php .phtml

Finally, for those using one of the Apache 2 versions, you will need to indicate the location of your PHP ini file. Add the following line to the end of your httpd.conf file.

PHPIniDir "c:/php"

Of course, if you used a different directory for your PHP installation, you will need to change "c:/php" to that path. Remember to use the forward slash ("/") here again.

If you are using Apache 1, you will have already placed your php.ini file in either the Windows directory or somewhere in your PATH, so PHP should be able to find it by itself. You can, of course, do the same if you are using Apache 2, but I find modifying the Apache configuration file a better solution than cluttering your c:\windows directory or your PATH variable.

Running PHP 5 as a CGI Binary

If you have configured PHP 5 to run as an Apache module, skip forward to the next section. This section is for those who want to configure PHP to run as a CGI binary.

The procedure is the same whether you are using the Apache 1.x series or one of the 2.x series.

Search for the portion of your Apache configuration file which has the ScriptAlias section. Add the line from the box below immediately after the ScriptAlias line for "cgi-bin". If you use Apache 2.2.x, make sure that the line goes before the closing </IfModule> for that <IfModule alias_module> section.

Note that if you installed PHP elsewhere, such as "c:\Program Files\php\", you should substitute the appropriate path in place of "c:/php/" (for example, "c:/Program Files/php/"). Observe carefully that I used forward slashes ("/") instead of the usual Windows backslashes ("\") below. You will need to do the same.

ScriptAlias /php/ "c:/php/"

Apache needs to be configured for the PHP MIME type. Search for the "AddType" comment block explaining its use, and add the AddType line in the box below after it. For Apache 2.2.x, you can find the AddType lines in the <IfModule mime_module> section. Add the following line just before the closing </IfModule> for that section.

AddType application/x-httpd-php .php

As in the case of running PHP as an Apache module, you can add whatever extensions you want Apache to recognize as PHP scripts, such as:

AddType application/x-httpd-php .phtml

Next, you will need to tell the server to execute the PHP executable each time it encounters a PHP script. Add the following somewhere in the file, such as after the comment block explaining "Action". If you use Apache 2.2.x, you can simply add it immediately after your "AddType" statement above; there's no "Action" comment block in Apache 2.2.x.

Action application/x-httpd-php "/php/php-cgi.exe"

Note: the "/php/" portion will be recognized as a ScriptAlias, a sort of macro which will be expanded to "c:/php/" (or "c:/Program Files/php/" if you installed PHP there) by Apache. In other words, don't put "c:/php/php.exe" or "c:/Program Files/php/php.exe" in that directive, put "/php/php-cgi.exe".

If you are using Apache 2.2.x, look for the following section in the httpd.conf file:

<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>

Add the following lines immediately after the section you just found.

<Directory "C:/php">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>

Configuring the Default Index Page

This section applies to all users, whether you are using PHP as a module or as a CGI binary.

If you create a file index.php, and want Apache to load it as the directory index page for your website, you will have to add another line to the "httpd.conf" file. To do this, look for the line in the file that begins with "DirectoryIndex" and adds "index.php" to the list of files on that line. For example, if the line used to be:

DirectoryIndex index.html

change it to:

DirectoryIndex index.php index.html

The next time you access your web server with just a directory name, like "localhost" or "localhost/directory/", Apache will send whatever your index.php script outputs, or if index.php is not available, the contents of index.html.

Restart the Apache Web Server

Restart your Apache server. This is needed because Apache needs to read the new configuration directives for PHP that you have placed into the httpd.conf file. The Apache 2.2 server can be restarted by double clicking the Apache Service Monitor system tray icon, and when the window appears, clicking the "Restart" button.

Previous: Install PHP on IIS in Windows 7
Next: Install WAMP



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php/installation/install-php-on-apache-in-windows.php