Linux News|Linux Install sidebyside windows|Ubuntu|Linux Help|Installing Apache and php from source

Wednesday, January 4, 2012

Installing Apache and Php from source in Linux


In this article you will learn how to install Apache and Php from source.If you are installing from binary files whether it is rpm or debian based then everything will be installed by default and you don't have to worry about inner details but if you are new to php and apache and I am assuming so,you should know some working details before going into programming. When I write this post I did not have any experience in php programming but I have installed php and apache from source but it has taken time. So I am here to solve your problem and give some details regarding inner working.So lets start to setup environment for php programming----

Installing Apache from source in any linux distribution:

I am using ubuntu 10.04(Lucid) for all compilation and installing in this tutorial. Before installing from source make sure that all tools required for installing is already installed on your linux system such as gcc compiler,make utility because all these tools are needed for installation from source.
I am using httpd-2.2.17.tar.gz ,extract it to the desired location and open the terminal and reach that location where you have extracted the httpd file.
Before proceeding further make a directory it is your installation directory where you will install apache,I am using "/home/tarun/apache" where all installed files will be stored.
After extracting a folder "httpd-2.2.17" will be made. Enter this folder using terminal.
Now follow these steps:

[] is used to differentiate commands from strings
Step 1:
[./configure --prefix="/home/tarun/apache" --enable-so] and press enter.
Here prefix option redirects the installation to your directory and second option enables shared library which I don't know as I am like you,we will learn as we will go on.
Other option are also available and to view them use command as [./configure --help].

Step 2:
If everythings goes fine without any error then proceed:
[make && make install]
if  your have read and write permission of your installation directory then you don't need to have root permission using sudo but if you don't have permission then use sudo before commands.Here I am assuming that you have some basic experience of any Linux OS.

Now check the folder now it contains files and folders.
Before moving further edit the httpd.conf file located in /home/tarun/apache/conf as follows:
Search for User & Group and set user to your name and group to your group.


Checking Apache installation:

Start the apache using this command and here you will need root permissions so use sudo as follows:
[sudo /home/tarun/apache/bin/apachectl start] and press enter Now open your browser and run http://localhost this will open by default html file for your server and will show positive results if installed correctly. By positive result means that the default html file is run correctly as default file may differ from one version to another. In my case default file shows "It Works" string if installed correctly.

Stop the server using command below:
[sudo /home/tarun/apache/bin/apachectl stop]



Php Installation in any Linux distribution from source:

Make a folder in your previous installation directory which will now be installtion directory for php and for this I am using "/home/tarun/apache/php".
As php is build to talk to many products and distribution so there are lots of option provided as in apache but here we will only configure those option which are sufficient for running php on your machine. So before going further extract the tar file on some location and reach to that php directory from terminal. Here I am using php-5.3.4.
So reach to php-5.3.4 in terminal where you have placed it and make sure you have reqired permission or you can use sudo to fulfill that.

Step 1:
[./configure --help] This will list the options available with php.

Step 2:
[./configure --with-apxs2=/home/tarun/apache/bin/apxs --prefix=/home/tarun/apache/php]
Here first option is for apache extension support for version 2,as previously said we will learn with use and second option directs the installation to specified directory.
If there is error regarding libxml2 then install the libxml library which is used to parse xml files as follows:

[sudo apt-get install libxml2-dev]

Step 3:
[make && make install]
Again I am specifying you should have permissions or other wise use sudo .

Now you have to edit certain text files so as to configure php as follows:
Now there are certain ini files in your php-5.3.4 which is source folder of php from where you have configured & installed php using configure and make commands and from which that particular ini file is to be copied to /usr/local/lib and rename it as php.ini.
[ls -l php*] will list files starting with php as follows:

-rw-r--r-- 1 tarun tarun  1489 2011-12-30 00:10 php5.spec
-rw-r----- 1 tarun tarun  1489 2007-09-26 21:14 php5.spec.in
-rw-r----- 1 tarun tarun  2523 2006-03-07 00:40 php.gif
-rw-r----- 1 tarun tarun 68760 2010-12-08 05:23 php.ini-development
-rw-r----- 1 tarun tarun 68990 2010-12-08 05:23 php.ini-production

Here we are interested in php.ini-development file and copy it to /usr/local/lib and rename it to as php.ini
Edit this file with the editor you want whether you are using gedit or nano or vim but the condition is that you should know how it works.[gedit php.ini] after reaching /usr/local/lib.
So search for "include" in that file until you find something like this:

Before Modification-----------

; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"
;
; PHP's default setting for include_path is ".;/path/to/php/pear"
; http://php.net/include-path

Here lines starting with ; are comments,so according to our requirement we will add this line as follows:

After Modification-----------

; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
;
#Modified by Tarun_Chawla on 4 jan 2012
include _path ="/home/tarun/apache/php"
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"
;
; PHP's default setting for include_path is ".;/path/to/php/pear"
; http://php.net/include-path

Now edit some apache files,move to /home/tarun/apache/conf/ and edit the httpd.conf in this directory.
[gedit httpd.conf]
Search for "php" until you find this line as follows:

# LoadModule foo_module modules/mod_foo.so
LoadModule php5_module        modules/libphp5.so
#

And modify it to :

# LoadModule foo_module modules/mod_foo.so
LoadModule php5_module        /home/tarun/apache/modules/libphp5.so
LoadModule php5_module        modules/libphp5.so
#

Now search for "AddType" until you see this as follows:

# AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
 
And modify it as follows so that apache recognizes .php and .phps as php files,.phps file when opened with apache will open php source file without compiling this will used for debugging purposes and .php files will be treated as php files and php will interpret them as required.

# AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
     Addtype application/x-httpd-php .php 
     Addtype application/x-httpd-php-source .phps
    #

There is another section you will have to look through,search for "index" in httpd.conf until you find these lines which follow as:

</Directory>

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#

#
# The following lines prevent .htaccess and .htpasswd files from being 
# viewed by Web clients. 
#
<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>

After Modification:

</Directory>

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

#
# The following lines prevent .htaccess and .htpasswd files from being 
# viewed by Web clients. 
#
<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>

This is done so when your computer is accessed remotely as a server index.html or index.php will serve them according to the order in which you have written the names.
Now save the files and exit.

Start the apache server as:
[sudo /home/tarun/apache/bin/apachectl start] or restart if it is already running as [sudo /home/tarun/apache/bin/apachectl restart].

Now check your server which ready with php support as follows:

make a text file as hello.php in so path will be like this /home/tarun/apache/htdocs/hello.php and paste the following code below in it :

<html>
<head>
<title>
This is our first php script</title>
</head>
<body>
<?php 
phpinfo();
?>  

</body>

</html>

Now go to your browser and run http://localhost/hello.php
It will show the details regarding php installation and version.

If any problem persists write down here I will be pleased to help you.

No comments:

Post a Comment

ubuntuway.blogspot.com