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

Thursday, February 19, 2009

Firebug

These days I'm working on my final year project.
I just came across Firebug when I'm refering to a book on Javascript. It seems like a good addition to the development tool list. So here it is.


Firebug is a Firefox add-on that can be used to debug Javascripts. And it is free.

That's all? Of course not. It can be utilized to do more than that. And Firebug has the distinct capability to monitor and debug asyncronus calls to the server. Yes, that means "Ajax".

Why don't you drop by the firebug web site and check it out.

The address is http://getfirebug.com/

Hope it will be helpful in your developments to come.

Monday, February 16, 2009

Ubuntu

After a long time I guess.

Couldn't really catch up with my blogs recently. And when I signed in today the landscape has been changd a bit (Surely, the Blogger team has been busy lately).

If you are a free and open source software admirer you should probably know what Ubuntu is. It is a Linux based free and opensourced operating system that has gained quite a lot of attention these days. I myself is trying it out. For now that's all to tell about it.



Interested?

Then visit http://www.ubuntu.com/ for more details.

You can get a free Ubuntu CD by post or you can also download an .iso image from the website itself.

Just try it out. It is always good to know about more than one Operating System. Just in case.

Saturday, August 30, 2008

Failed to Access IIS Metabase

I have created a web setup application for an ASP.NET web application. It installs the application fine in my computer but when I try to run the installed web application the following error message appeared.

System.Web.Hosting.HostingEnvironmentException: Failed to access IIS metabase. The process account used to run ASP.NET must have read access to the IIS metabase (e.g.
IIS://servername/W3SVC). For information on modifying metabase permissions, please see
http://support.microsoft.com/?kbid=267904.

For some reason IIS do not give read access to ASP.NET to its metabase.

What was the reason?

And then I remembered that I have installed IIS after installing Visual Studio 2005. So that meant there was a good possibility that IIS and ASP.NET didn't register each other properly. Now I had to find out a way to do that.

Well, back to the good old Internet for that. I gave abit of a search and found out what to do for the matter. The solution turned out to be a pretty straigh forword one.

This is what you do

  • Go to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
  • Then find "aspnet_regiis.exe"
  • Go to Start > Run
  • Type "cmd" and press Enter to go to the command prompt
  • Type cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 and Enter
  • Type "aspnet_regiis -i" and Enter
  • Give it a moment to install
  • And that's it. Your web application will run without the above error.

Thursday, July 31, 2008

A Problem When Running a Java Program

I had recently installed Java 6. And it compiled the source file fine. Then when I tried to run the compiled file it gave the following error.

D:\Java\Examples>javac HelloWorld.java

D:\Java\Examples>java HelloWorld
Exception in thread "main" java.lang.NoClassDe fFoundError: HelloWorld
Caused by: java.lang.ClassNotF oundException: HelloWorld
at java.net.URLClassLo ader$1.run( Unknown Source)
at java.security. AccessController .doPrivileged( Native Method)
at java.net.URLClassLo ader.findClass( Unknown Source)
at java.lang.ClassLoad er.loadClass( Unknown Source)
at sun.misc.Launcher$ AppClassLoader. loadClass( Unknown Source)
at java.lang.ClassLoad er.loadClass( Unknown Source)
at java.lang.ClassLoad er.loadClassInte rnal(Unknown Source)

Now I was wondering what went wrong? I mean, I know that it compiled fine. But it refused to run. So what to do? I tried compiling and running a different source file. Nope the answer is the same. And then I compiled and tried to run a source file that I was really sure that was error free which happens to be HelloWorld.java. The same error occured.

What is the point of being a programmer if you can't run HelloWorld in java?

Then I thought I would need some expert advice on the matter. So I posted the matter to a lecturer at the institute where I study for BIT. This is the reply that I got.

To immediately solve this problem, you can delete the "classpath" variable in Environment variables list. Try in this manner, System properties--> Advanced --> Environment Variables --> Select "classpath" and Delete it.

So I did exactly that and all the java programs are running fine since then.