Configuring your Web server for PHP

Your Web server needs to be configured to recognize PHP scripts and run them.

Configuring Apache on Linux and Mac
Configuring your Web Server on Windows
Configuring Apache on Windows
Configuring IIS

Configuring Apache on Linux and Mac

You must configure Apache to recognize and run PHP files. An Apache configuration file, httpd.conf, is on your system, possibly in /etc or in /usr/local/apache/conf. You must edit this file before PHP can run properly.

Follow these steps to configure your system for PHP:

  1. Open the httpd.conf file so you can make changes.
  2. Configure Apache to load the PHP module.

    Find the list of LoadModule statements. Look for the following line:

       LoadModule php5_module libexec/libphp5.so
    If this line isn’t there, add it. If a pound sign (#) is at the beginning of the line, remove the pound sign.

    For PHP 6, the line will be:

       LoadModule php6_module libexec/libphp6.so

  3. Configure Apache to recognize PHP extensions.

    You need to tell Apache which files might contain PHP code. Look for a section describing AddType. You might see one or more AddType lines for other software. Look for the AddType line for PHP, as follows:

       AddType application/x-httpd-php .php

    If you find it with a pound sign (#) at the beginning of the line, remove the pound sign (#). If you don’t find this line, add it to the AddType statements. This line tells Apache to look for PHP code in all files with a .php extension. You can specify any extension or series of extensions.

  4. Start the Apache httpd server (if it is not running) or restart the Apache httpd server (if it is running).

    You can start or restart the server with a script that was installed on your system during installation. This script might be apachectl or httpd.apache, and might be located in /bin or /usr/local/apache/bin. For example, you might be able to start the server by typing apachectl start, restart it by using apachectl restart, or stop it by using apachectl stop. Sometimes restarting is not sufficient; you must stop the server first and then start it.

Configuring your Web Server on Windows

You can’t have Apache and IIS running at the same time using the same port number. Either shut down one Web server or tell them to listen on different ports.

Configuring Apache on Windows

You must edit an Apache configuration file, called httpd.conf, before PHP can run properly. To configure Apache for PHP, follow these steps:

  1. Open httpd.conf for editing.

    To edit the file, choose Start-->Programs-->Apache HTTPD Server-->Configure Apache Server-->Edit Configuration.

    If Edit Configuration isn’t on your Start menu, find the httpd.conf file on your hard drive, usually in the directory where Apache is installed, in a conf subdirectory (for example, c:\program files\Apache group\Apache\conf). Open this file in an editor, such as Notepad or WordPad.

  2. Activate the PHP module.

    Look for the module statement section in the file and locate the following line:

       #LoadModule php5_module "c:/php/php5apache2.dll"
    Remove the # from the beginning of the line to activate the module. If you install PHP 6, you need the following line:
       LoadModule php6_module "c:/php/php6apache2.dll"

    If you are using Apache 1.3, rather than Apache 2, the module name is php6apache.dll or php5apache.dll. If you are installing Apache 2.2, the module name is php6apache2_2.ddl or php5apache2_2.dll.

  3. Tell Apache which files are PHP programs.

    Look for a section describing AddType. This section might contain one or more AddType lines for other software. The AddType line for PHP is

       AddType application/x-httpd-php .php
    Look for this line. If you find it with a pound sign (#) at the beginning of the line, remove the pound sign. If you don’t find the line, add it to the list of AddType statements. You can specify any extension or series of extensions.

    This line tells Apache that files with the .php extension are files of the type application/x-httpd-php. Apache then knows to send files with .php extensions to the PHP module.

  4. Start Apache (if it is not running) or restart Apache (if it is running).

    You can start it as a service on Windows NT/2000/XP/Vista by choosing Start-->Programs-->Apache HTTPD Server-->Control Apache Server and then selecting Start or Restart. You can start it on Windows 98/Me by choosing Start-->Programs-->Apache Web Server-->Management.

    Sometimes restarting Apache is not sufficient; you must stop it first and then start it. In addition, your computer is undoubtedly set up so that Apache will start whenever the computer starts. Therefore, you can shut down and then start your computer to restart Apache.

Configuring IIS

To configure IIS to work with PHP, follow these steps:
  1. Enter the IIS Management Console.

    You should be able to enter by choosing Start-->Programs-->Administrative Tools-->Internet Services Manager or Start--> Control Panel-->Administrative Tools-->Internet Services Manager.

  2. Right-click your Web site (such as Default Web Site).
  3. Select Properties.
  4. Click the Home Directory tab.
  5. Click the Configuration button.
  6. Click the App Mappings tab.
  7. Click Add.
  8. In the Executable box, type the path to the PHP interpreter. For example, type c:\php\php-cgi.exe.
  9. In the Extension box, type .php. This will be the extension associated with PHP scripts.
  10. Select the Script Engine check box.
  11. Click OK.

Repeat Steps 6-10 if you want any extensions in addition to .php to be processed by PHP, such as .phtml.