Install Apache2 and PHP4 in Windows-PHP Tutorial

Source: Internet
Author: User
Install Apache2 and PHP4 in Windows. Apache2 and PHP are popular solutions for creating interactive websites with low costs. Installing Apache2 in Windows is a breeze, but Apache 2 and PHP are a popular solution for creating interactive websites to make PHP4 and Apache2 run smoothly. Installing Apache 2 in Windows is a breeze, but it requires some skills to make PHP 4 and Apache 2 run in concert.

The Windows installation section in the PHP 4.3 Manual does not explain how to use PHP with Apache 2, but the section on Apache 2 installation lacks a lot of information you need. Other installation guides published on the Internet also contain many errors, so some installers have to keep experimenting and making mistakes. For example, some may even replace php dll with other PHP versions. OK. in order to save your precious time, we have prepared this most authoritative guide article to help you eliminate all your troubles.

Use the correct version of Windows

First, let's talk about compatibility: Apache 2 cannot run on Windows 95; it can barely run on Windows 98, but cannot be used as a service. PHP does not support Windows 95 any more since version 4.3. Therefore, your Windows OS must be Windows NT, 2000, or XP.

Apache 2: source code or binary code?

If you have Visual C ++ 5.0 or a later version, or you have installed Microsoft Visual Studio, you can generate Apache based on the source code, but never do so. In Visual Studio IDE, the generation process in Windows is very complex. Even compiling from the command line is definitely not an easy task. Therefore, unless you like to create makefile, you 'd better get a Windows Installer binary release package, that is, A. msi file. If you have enough knowledge about Apache and know which custom operations you want to perform, you can generate (compile) Apache by yourself.

If you have installed and are running an earlier version of Apache, stop it, uninstall it, and then start installing the new server. Apache 2 of multiple versions cannot coexist.

Double-click the Apache 2. msi file. After agreeing to the license agreement, set the Network Domain and Server Name correctly (if you do not want to install Apache on a remote computer, set localhost ), in the Administrator's Email Address area, enter your Email Address to keep Port 80/service option selected. In the next dialog box, if you select Typical for installation, you can quickly obtain a server environment that can actually work.

Installation prompt

It is recommended that the default installation directory be changed from C: \ Program Files \ Apache Group to C: \ Apache or other names in the 8.3 file name format. In this way, you do not need to add quotation marks for each input Apache installation path.

Wait a moment. the installation wizard will tell you that Apache 2 is successfully installed. The only thing to do next is to perform a test. open your browser and visit localhost. the default Apache 2 homepage is displayed.

You can then delete all the sample files in the directory of the master Web server, which are located in C: \ InstallDirectory \ Apache \ Apache2 \ htdocs by default. If you have a primary page index.html, copy it here. In addition, you can create a blank homepage immediately. For uninitialized HTML, you can use the following code:

<Html>
<Head> title = default server page <Body> Default server page </body>
</Html>

Prepare Apache 2 for PHP

From now on, you need to install it manually. you cannot simply click or select several options. Get used to it as soon as possible. this is the Apache method. You need to edit the file to set different configuration options. if the operation is not proper, your modifications will impede the correct loading of Apache. If an Error occurs, the Error may be recorded in the Apache 2 Error log (C: \ InstallDir \ Apache2 \ Logs \ Error. log by default ). However, there are few records that can be recorded like this. most errors are recorded only in Windows event logs (start | set | control panel | management tools | event viewer ).

Unfortunately, Windows event logs are not a convenient tool when you debug the installation settings. A better way is to test Apache server loading in the command line window, so that you can see the error report immediately. Therefore, after completing the configuration changes I will discuss, open a command line window and switch to the Apache binary Directory (C: \ InstallDir \ Apache2 \ bin ), start Apache there.

The Apache configuration file is C: \ Apache \ Apache2 \ Conf \ Httpd. conf, which can be edited by any text editor. Find the DirectoryIndex and locate the following line:

DirectoryIndex index.html. var # index. php

To allow Apache to process PHP pages, delete the comment character (#) and change it:

DirectoryIndex index.html. var index. php

You can also use the. htaccess file in any Directory. Therefore, search for AllowOverride in the configuration file and change the setting from None to All. After saving the changes, you can continue to open the file in the text editor, because you need to edit the file again later when installing PHP.

Install PHP

Although PHP source code can be downloaded, it is best to directly use the binary distribution package like Apache 2. Apache 2 can run the PHP program in two ways: run the program through a cgiinterface (external call php.exe), or run the php dll file inside Apache. The latter method is faster. Therefore, for each version of PHP, two Windows binary release packages are provided. The smaller one is the. msipackage. the cgiexecutable program php.exe is installed, but the module required to run the PHP script through Apache DLL is removed. The larger. zip package contains all these items. you can download them from the Win32 region of the snaps.php.net website. After the file is downloaded, decompress it to C: \ Php (keep the folder name ). Unless you are absolutely certain, please download the PHP manual at the same time. it has multiple language versions available.

Configure Apache 2 to run PHP4

Now we come to the most interesting part: the perfect combination of Apache and PHP. First, copy all DLL files in the C: \ Php \ Dlls directory to the Windows System directory (% windir % \ System ). Next, find the section containing a large number of AddType commands in the Apache configuration file (Httpd. conf) and add the following line by yourself:

AddType application/x-httpd-php. php

Copy C: \ Php. ini-recommended to the Windows directory (% windir %), rename it Php. ini, and open it in a text editor. Edit the three rows set for doc_root, extension_dir, and session. save_path to make them consistent with the one shown below. Note that you should replace InstallDir with the name of your Apache 2 installation directory.

Doc_root = c: \ apache \ apache2 \ htdocs
Extension_dir = c: \ php \ extensions
Session. save_path = c:/temp

Both forward and backward slashes are allowed in session. save_path. The PHP manual claims that these path parameters need to be added with a backslash at the end, but this statement is incorrect. This is not required in PHP 4.3. In addition, if C: \ Temp does not exist, create it by yourself.

The next step is to allow Apache to run the PHP program as a module, which requires two steps. First, copy C: \ Php \ Php4ts. dll to the Windows System folder (% windir % \ System ). Find the LoadModule section in Httpd. conf and add the following line by yourself:

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

If you run the PHP program (use php.exe) in the cgimodel for a specific reason, change the above line to a comment and add the following lines in Httpd. conf:

ScriptAlias/php/"c:/php /"
Action application/x-httpd-php "/php/php.exe"

Is everything done?

After saving the changes, you need to prove two points in person to ensure that Apache 2/PHP 4 becomes a perfect partner: can Apache load normally? Can It process PHP pages correctly?

To check whether Apache can be loaded properly, enter the following command in the command line window that you have opened:

Apache-k start

In addition, if Apache is running, run the following command to restart it:

Apache-k restart

The benefit of starting Apache from a command line is that Apache immediately reports an error. The most common problem you may encounter is that Apache may be unable to load Php4apache2. dll for some reason. If Apache reports this error, repeat the preceding steps to ensure that everything is correct.

To know if Apache can process the PHP page correctly, use your text editor to create a simple PHP page named Phptest. php, which contains only the following line:

<? Phpinfo ();?>

Save the file to the main Web server directory (C: \ InstallDirectory \ Apache \ Apache2 \ Htdocs) and access http: // localhost/phptest. php in a browser. If everything is normal, you should see a webpage containing the PHP logo, which contains a lot of settings and other information. To know whether PHP is currently running through CGI or inside Apache, check the environment variable orig_script_name. If PHP runs through CGI, the value of this variable is/Php/Php.exe. If Apache runs the PHP script as a module, the value of this variable should be/Phptest. php.

It is not easy to install Apache 2 and PHP on Windows, but there is no need to cross the river by feeling the stones. Through the instructions in this article, you should be able to quickly build the best and lowest-cost Web development environment in history.

Bytes. Installing Apache2 in Windows is a breeze, but you need to make PHP4 and Apache2 run smoothly together...

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.