Friday, December 17, 2010

Downloading, Installing and Configuring Apache

PHP is a server side scripting language which means that all of the processing regarding to a php page is done on the server not on the client. If the browser has requested for a php page the web server then passes that request to the php Engine. PHP Engine is the part that handles processing of php pages. When php pages are processed in the php Engine they generate html pages, which then are sent to the browser. So you see that in order to get an output from a php page you need a Web Server.

What is a Web Server?

When you browse the Internet or any other web site your browser requests for web pages from another computer. This is known as a web request. The Web Server that resides in that computer identifies that a browser is requesting a web page and do the required processing and deliver the web page to the browser.

Now you have to understand this, a Web Server is not a Server. A Server is a high end computer whereas a Web Server is a program running on a computer that processes web requests. But both are referred to as Servers for convenience.

There are many Web Servers that you can use for this purpose such as
Microsoft IIS (Internet Information Server)



We will be using Apache as the Web Server. In fact many web hosts uses Apache for its reliability and configurability. And it’s free and open sourced.

If you need to know more about Apache please visit this page.



Now let’s Download, Install and Configure Apache.

These steps are for the Windows platform.


Downloading Apache

The installation files for apache can be downloaded from www.apache.org.

Click HTTP Server under "Apache projects"

Download the HTTP Server installation (.msi) file for Win32.



Installing Apache

  • Once you have downloaded the installation file simply double click and launch it.
  • Click "Next"
  • Read and accept the license agreement to proceed and click "Next".
  • Read all of the instructions if you like to. Click "Next".

  • Since Apache is going to be used for development requirements put the following information.

    Network Domain: localhost
    Server Name: localhost
    Administrator’s Email Address: any email address

    Keep the recommended selection
  • Click "Next"
  • Keep the selection as “Complete” and click "Next".
  • Choose a path for the destination folder. You can keep the default path if you are OK with it. Click "Next".
  • Click "Install". Be patient while the apache server is being installed.


And that’s it.



Configuring Apache

Now when you open your browser and type http://localhost or 127.0.0.1 (This is the IP address for localhost for all computers) or the IP address of your computer you will get the following page.



Now you are seeing the default web page in the "htdocs" folder. Assuming that you have given the default path as the installation directory for Apache this will be at

C:\Program Files\Apache Group\Apache2\htdocs

The reason that you see the web page that is in htdocs is that Apache is configured to show you that folder when you browse to the network domain (in our case it is http://localhost).

You can put your web projects here and run them. But however this is not recommended.

Why?

Well, first of all you are in a directory that a program is saved to. When Apache is uninstalled or repaired for some reason you most probably will lose all the files saved inside htdocs. And when you delete and rearrange your own files within this folder there is the possibility that you might accidently delete or renames a file or a folder that is vital to the functionality of Apache.

The best thing to do is that you create your own folder somewhere else and configure Apache to that folder.

Here is how it is done.

Go to Start > All Programs > Apache HTTP Server 2.0.54 > Configure Apache Server > Edit the Apache httpd.conf Configuration File

You can also open this file by navigating to
C:\Program Files\Apache Group\Apache2\conf\httpd.conf

Once you opened the file scroll down and find this part.

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "C:/Program Files/Apache Group/Apache2/htdocs"

Now let’s assume that you need “C:\Projects” to be your “Document Root” then you edit this part as follows.

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
#DocumentRoot "C:/Program Files/Apache Group/Apache2/htdocs"
DocumentRoot "C:/Projects"

Note: Always remember to comment the line that you intend to change and add the changed line below the commented line. This way you will know exactly what you changed.


Then scroll down to find this part

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

Change this part as follows

#
# This should be changed to whatever you set DocumentRoot to.
#
#Directory "C:/Program Files/Apache Group/Apache2/htdocs"
Directory "C:/Projects"

It is a good practice to backup the original httpd.config file before you start messing with it. And don’t delete any line from the file. Just comment it out. The "#" is used for commenting.

When you’re done, save the file and restart Apache. This is very important because all the changes that you made to the httpd.config will take effect only after restarting the service.

Now you can put an html file in to the folder you specified and then go to the browser and navigate to http://localhost to see the result.

Downloading and Installing php 5

Downloading php

To download php, visit www.php.net. This is the official php site. And go to Downloads. In the download section go to Windows Binaries. You have two options. Either you can download the installer package or you can download the zip package. Don’t go for the installer package because it will only configure IIS and not Apache. Download the zip package.



Installing php

Extract the php zip package to a folder in your computer.

You will notice that php 4 and php 5 are slightly different at the first glance of the directory content. So to install php 5 the method too is slightly different. Mostly you can use the steps in installing php 4 to install php 5. But there are several exceptions. That is why I write this blog entry separated from php 4 installation steps. Differences in the php 5 installation from php 4 installation are in different text colour in this blog entry.

Make sure you extract the zip file into a folder with the folder name not having spaces. This is very important because otherwise you will have some configuration problems when you are trying to configure php with Apache.

Open the “install.txt” file in the php folder.
Go to the “Installing as an Apache module” section and follow the instructions in there.

Or you can directly do this. But it is always better to have a look at the “Installing as an Apache module” section in the “install.txt” file.

OK, let’s assume that you have php 5 and you extracted the contents of the zip file into C:\php.

Now, open the Apache httpd.conf file (Start > All Programs > Apache HTTP Server 2.0.54 > Configure Apache Server > Edit the Apache httpd.conf Configuration File)
Go to the “LoadModule” block and add this line to the end.

LoadModule php5_module C:/php/php5apache2.dll

And to enable Apache to recognize and handle php files add the following line to the “AddType” section.

AddType application/x-httpd-php .php

Now save the file and restart the Apache server.



Configuring The php.ini File

This is where php 5 installation differs from php 4 installation. Although you can gain access to MySQL module without configuring the php.ini file in php 4, you have to configure the php.ini file in order to gain access to MySQL module in php 5
Here is how
In your php directory (ex. C:\php) there is a file called “php.INI-RECOMMENDED”. First make a copy of it in the php directory. And then rename the copy of “php.INI-RECOMMENDED” in to “php.ini”. It is very important that you don’t use the original “php.INI-RECOMMENDED” for configurations.

Now open the Apache httpd.conf file (Start > All Programs > Apache HTTP Server 2.0.54 > Configure Apache Server > Edit the Apache httpd.conf Configuration File)
Add this part to the very bottom of the file

# configure the path to php.ini
PHPIniDir "C:/php"

Note: This directive only works for Apache 2 web servers and above. If you are working with an earlier version of Apache then you will have to use the PHPRC environment variable. Check out the install.txt in the php directory to know more about this method.
Save the file and restart Apache

Now right click on “My Computer” and select “Properties” from the popup menu. Then go to “Advanced” tab and select “Environment Variables”. Choose “Path” from “System variables” and click “Edit”.

Don’t delete the content of the “Variable value” text box (That’s very important).
Now find the part of the variable string that gives the path value for MySQL bin directory.
Then put the path of the php directory before the path of the MySQL bin directory. (You have to make sure that each of these path variables are separated with a “;”).
Remember! This is very important. Unless you do this, MySQL module will fail to load in php 5. This was the point that I stuck when I first tried to install php 5. And I am very thankful to the person who left a comment in the php web site telling why this happens and how to get around this problem.

Click “Ok” to save the changes and then restart your computer. This is done because changes to Environment Variables only take effect after restarting the computer.
Once all of these are done you can configure php using “php.ini”

Open “php.ini” in the php directory

This file is mainly used to dynamically load extensions for php. To do so first you will have to tell php where it can find these extensions. These extensions are in a directory named “extensions” inside the php directory. Give the path of this directory to “extension_dir” directive.

extension_dir = "C:\php\ext"

You are almost done

Now when you want to load a dynamic extension all you have to do is to go to the “Dynamic Extensions” section of the “php.ini” and uncomment the appropriate directive (remove the “;” from the beginning of the directive)

Like this

change

;extension=php_mysql.dll

to

extension=php_mysql.dll

You will have to save the file and restart Apache for these changes to take effect

Downloading and Installing php 4

Downloading php

To download php, visit www.php.net. This is the official php site. And go to Downloads. In the download section go to Windows Binaries. You have two options. Either you can download the installer package or you can download the zip package. Don’t go for the installer package because it will only configure IIS and not Apache. Download the zip package.



Installing php

Extract the php zip package to a folder in your computer.

Make sure you extract the zip file into a folder with the folder name not having spaces. This is very important because otherwise you will have some configuration problems when you are trying to configure php with Apache.

Open the “install.txt” file in the php folder.
Go to the “Installing as an Apache module” section and follow the instructions in there.

Or you can directly do this. But it is always better to have a look at the “Installing as an Apache module” section in the “install.txt” file.

OK, let’s assume that you have php 4 and you extracted the contents of the zip file into C:\php.

Open the folder named “SAPI” in C:\php.

Copy the file named “php4apache2.dll” in that folder and paste it in C:\php.
Now, open the Apache httpd.conf file (Start > All Programs > Apache HTTP Server 2.0.54 > Configure Apache Server > Edit the Apache httpd.conf Configuration File)
Go to the “LoadModule” block and add this line to the end.

LoadModule php4_module C:/php/php4apache2.dll

And to enable Apache to recognize and handle php files add the following line to the “AddType” section.

AddType application/x-httpd-php .php

Now save the file and restart the Apache server.



Configuring The php.ini File

When you have completed above steps, php will work without much of a problem. However there is another step to complete the installation. That is setting up the php.ini file. By doing this you can harness the full potential of php. Otherwise you won’t be able to load dynamic modules to support php.

Here is how

In your php directory (ex. C:\php) there is a file called “php.INI-RECOMMENDED”. First make a copy of it in the php directory. And then rename the copy of “php.INI-RECOMMENDED” in to “php.ini”. It is very important that you don’t use the original “php.INI-RECOMMENDED” for configurations.

Now open the Apache httpd.conf file (Start > All Programs > Apache HTTP Server 2.0.54 > Configure Apache Server > Edit the Apache httpd.conf Configuration File)
Add this part to the very bottom of the file

# configure the path to php.ini
PHPIniDir "C:/php"

Note: This directive only works for Apache 2 web servers and above. If you are working with an earlier version of Apache then you will have to use the PHPRC environment variable. Check out the install.txt in the php directory to know more about this method.
Save the file and restart Apache

Now right click on “My Computer” and select “Properties” from the popup menu. Then go to “Advanced” tab and select “Environment Variables”. Choose “Path” from “System variables” and click “Edit”.

Don’t delete the content of the “Variable value” text box (That’s very important). First add a “;” at the end of the content. Then add the path of your php directory after the “;” character (This is the divider that divides different system variables in the same string)

Click “Ok” to save the changes and then restart your computer. This is done because changes to Environment Variables only take effect after restarting the computer.

Once all of these are done you can configure php using “php.ini”

Open “php.ini” in the php directory
This file is mainly used to dynamically load extensions for php. To do so first you will have to tell php where it can find these extensions. These extensions are in a directory named “extensions” inside the php directory. Give the path of this directory to “extension_dir” directive.

extension_dir = "C:\php\extensions"


You are almost done

Now when you want to load a dynamic extension all you have to do is to go to the “Dynamic Extensions” section of the “php.ini” and uncomment the appropriate directive (remove the “;” from the beginning of the directive)

Like this

change

;extension=php_mysql.dll

to

extension=php_mysql.dll


You will have to save the file and restart Apache for these changes to take effect