Configure Apache to support PHP in Windows

Source: Internet
Author: User
Tags sapi server website

Ideas:

First, install Apache and ensure that Apache works properly. For more information about how to install Apache2.2.x in Windows (http://indian.blog.163.com/blog/static/1088158200755105546704), see my other article ).

The second step is to install PHP and configure Apache to support PHP.

There are two ways to enable Apache to support PHP in Windows: using Windows environment variables or Apache's own support.

Download from http://www.php.net/downloads.php before installation
Php zip binary release package. Although there are many multi-in-one installation packages, and a Microsoft Windows PHP
Install the program, but it is recommended that you manually install PHP later. In this way, you can better understand the system and install PHP extensions as needed.

Method 1: use Windows Environment Variables

1. The first step is to ensure that Apache can correctly find PHP and related DLL extensions.

PHP 5.0 was previously officially recommended to use php. ini and multiple DLL
File to the system folder (for example, C: \ WINDOWS ). This step simplifies the installation process, but causes difficulties in upgrading. We recommend that you use the new PHP version.
Delete all these files (for example, delete php. ini and PHP related DLL files from the Windows system folder ).
File ). Make sure to back up these files before deletion, because this operation may damage the entire system. The original php. ini version is installed in PHP
Can also be used for reference. Soon users will understand that the best way to install PHP is to put all PHP-related files in the same directory and
Set this directory in environment variables.

Decompress the downloaded PHP 5.2.x ZIP package to the target directory, such as C: \ php
I decompress the package to E: \ usr \ php. You can select other paths, but it is best not to use a path with spaces in the middle (for example, C: \ Program Files \ PHP
Not good). Some web servers will crash.

PHP installation in Apache can be performed in the form of server module or CGI executable program, and a CLI version is designed to use the PHP Command Line script. Server modules provide better performance and more functions than CGI executable programs. So I mainly introduce how to install the server module.

Php5ts. dll (used in php4ts. dll and php4) is required for CGI, CLI executable files, and web server modules. You must confirm that the file can be found in the PHP installation path. The search order for this DLL is:

  • The directory where php.exe is called, or if the directory of the web server (for example, C: \ Program Files \ Apache Group \ Apache2 \ bin) is used when the SAPI module is used ).
  • Any directory specified in the PATH environment variable of Windows.

To make php4ts. dll/php5ts. dll
If the file can be correctly searched, there are three options: copy the file to the Windows System directory, copy the file to the web Server Directory, or set the PHP Directory (for example
C: \ php) to the path environment variable. For better maintenance in the future, we recommend that you add the PHP Directory to the PATH
Environment variables, because this makes it easier to Upgrade PHP in the future.

① Add the PHP Directory to the Windows PATH

In Windows NT, 2000, XP, and 2003:

  • Go to the control panel and open the "System" icon (Start> Settings> Control Panel> system, in Windows XP/2003, Start> Control Panel> l System)
  • Select "advanced" tab
  • Click "environment variable ".
  • In the system variables Column
  • Locate Path (you may need to scroll down to find it)
  • Double-click the Path item.
  • Add your PHP Directory at the end, including the Semicolon ";" (for example,; C: \ php, my path is; E: \ usr \ php)
  • Click "OK" and restart the computer.

② Make the php. ini file used by PHP in Windows

(This step is very important. I have tested it. Without this step, PHP will not be able to find php. ini)

In Windows NT, 2000, XP, and 2003:

  • Go to the control panel and open the "System" icon (Start> Settings> Control Panel> system, in Windows XP/2003, Start> Control Panel> l System)
  • Select "advanced" tab
  • Click "environment variable ".
  • In the system variables Column
  • Click "new" and enter "PHPRC" in "variable name". In "variable value", enter the directory of the php. ini file (for example, C: \ php)
  • Click "OK" and restart the computer.

2. The next step is to set a valid configuration file for PHP, php. ini.

The ZIP package contains two ini files: php. ini-dist and
Php. ini-recommended. Recommended in the official manual
Php. ini-recommended, because the performance and security are optimized in this file. Please read the comments in this file carefully because it comes from php. ini-dist
The modification will have a great impact on the settings. For example, set display_errors to off and magic_quotes_gpc
Off. In addition to reading these sections, you can also learn about ini settings and manually set each configuration item. If you want the most secure settings, this is the best method, although PHP
It is also safe in the default configuration. For beginners of PHP, I suggest using php. ini-dist, because in this file, set display_errors
On, so that you will receive an error prompt when debugging PHP Code to facilitate debugging PHP code. Copy the selected ini file to the directory that PHP can find and rename it
Php. ini. For example, I copied php. ini-dist to E: \ usr \ php and named it php. ini.

3. Make sure that the values of the extension_dir and doc_root commands are set correctly in php. ini.

These commands depend on the PHP installed system. Open php. ini
File, search for "extension_dir". In PHP 4, extension_dir is extensions, and in PHP 5 is
Ext. Therefore, in PHP 5, the value of extensions_dir is as follows:
"C: \ php \ ext ". For example, my php Directory is E: \ usr \ php, so I set the value of extensions_dir
E:/usr/php/ext. That is, extensions_dir = E:/usr/php/ext.

Note: The Path in php. ini can be written as "E:/usr/php/ext ".
", You can also write it as" E: \ usr \ php \ ext ", that is, the difference between the slash (/) and the backslash. Php. ini
You can use either a slash (/) or a backslash. In Windows, slash (\) is used to represent the path separator, while in Unix and Linux, backslash (/) is used to represent the path separator. Therefore, compatibility
It is recommended to use a backslash (/). If the path contains spaces, it must be enclosed in English quotation marks ("), but the paths in Unix and Linux do not allow spaces. Therefore
The path should not contain spaces. The following is the same.

Search for "doc_root" in php. ini. This is your WEB root directory. IIS
The doc_root value example is "c: \ Inetpub \ wwwroot". The doc_root of Apache is the htdocs of the installation directory by default.
Folder, for example, I am E: \ usr \ Apache2.2 \ htdocs (I installed Apache in usr
Directory ). So the example I wrote is: doc_root = E:/usr/Apache2.2/htdocs

4. Configure Apache to support PHP.

Add the following two lines to the ApacheHttpd. confSet the PHP module of Apache 2.0 in the configuration file:

# Use these two lines for PHP 4:

LoadModule php4_module "c:/php/php4apache2. dll"

# Do not forget to copy php4apache2. dll from the sapi directory!

AddType application/x-httpd-php. php

# Use these two lines for PHP 5:

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

AddType application/x-httpd-php. php

Note: Remember to replace the path in the above example with the actual path of your PHP.C:/php/. Note that the LoadModule command usesPhp4apache2. dllOrPhp5apache2. dllInsteadPhp4apache. dllOrPhp5apache. dllThe latter is designed for Apache 1.3.x. Note that Apache2.2 uses php5apache2_2.dll. All path separators in the Apache configuration file are represented by a backslash.

========================================================== ========================================================== ======

Method 2: Apache support

Apache itself integrates support for PHP and can work collaboratively with it with simple configuration.

Decompress the downloaded PHP package and set a valid configuration file for PHP in the previous method. ini, and in php. the extension_dir and doc_root commands are set correctly in ini.

Add the following three lines to the ApacheHttpd. confSet the PHP module of Apache 2.x in the configuration file:

# Use these two lines for PHP 5:

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

AddType application/x-httpd-php. php

# Configure the php. ini path

PHPIniDir "C:/php"

Note: Change the PHP path to your own path. Note that Apache2.2 uses php5apache2_2.dll. All path separators in the Apache configuration file are represented by a backslash.

As you can see, there is only one more command for configuring php. ini. This command is only valid in Apache 2.

Now, PHP configuration is complete in Apache. You can create a PHP file containing the phpinfo () function to test whether PHP configuration is successful in Apache. The content of the test file is as follows:


<?php
echo phpinfo();
?>

Check "Configuration File (php. ini)
Whether the Path or Loaded Configuration File item points to your php Directory. View PHP Core
Does the "extension_dir" item point to your php extension directory.

SET index. php as the default homepage
<IfModule dir_module>
DirectoryIndex index.html index. php
</IfModule>

Set the server website startup directory to d: \ php
First modify
The DocumentRoot value is d:/php.

DocumentRoot "d:/php"

Modify the node statue to <Directory "d:/php">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.