Tuesday, January 10, 2012

Apache Session January 4th, 2012 noon 3.00 PM

Apache 2.0 (in Linux Ubuntu)


Note: commands are displayed yellow and outputs are displayed red...
   
     In apache pretty much everything is a module. Module is a collection of compiled code which can be loaded whenever necessary.

Install apache 2.0. To view the list of all running apache process use the following command:

     sudo netstat -pan| grep apache
    in netstat  -p  means program
                    -a  means all
                    -n  means show numeric address
You need to have sudo access to get the list of all process.


     tcp6       0      0 :::80                   :::*                    LISTEN      987/apache2

Now it is evident that apache listens on port 80


     whereis apache2

run the above command to get the location of apache...


apache2:   /usr/sbin/apache2   /etc/apache2 /usr/lib/apache2   /usr/include/apache2  /usr/share/apache2   /usr/share/man/man8/apache2.8.gz

apache is available in /usr/bin location. Now we have to find the apache2.conf file, for this give the following command...

/usr/sbin/apache2 -V

this will provide the version details as follows...

Server version: Apache/2.2.20 (Ubuntu)
Server built:   Nov  7 2011 22:49:55
Server's Module Magic Number: 20051115:28
Server loaded:  APR 1.4.5, APR-Util 1.3.12
Compiled using: APR 1.4.5, APR-Util 1.3.12
Architecture:   32-bit
Server MPM:     Worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APACHE_MPM_DIR="server/mpm/worker"
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=128
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

now it is clear that the root directory is /etc/apache2/. You can open the apache2.conf by using vim editor.

Apache runs child modules when and where necessary to avoid overload.
Now let us see how to create our own module in apache.

Creating new module:
     To create a new apache module you should have developer kit. You can install it using
          sudo apt-get install apache2-prefork-dev

You can create a new module by giving,
     sudo apxs2 -g -n 'mymodule' 
     -g  is used to create a sub-directory
     -n  is used to explicitly set the module name for -g option
now a directory named 'mymodule' will be created and three files named:
  • Makefile
  • mod_mymodule.c
  • modules.mk
will be created within it.

Now open mod_mymodules.c file and edit the file as necessary. Here we edit the 
"The sample page from mod_mymodule.c" line as 
"my first module". Open the file as a sudo user:

     sudo vim mod_mymodule.c 

Compilation:

Now the next step is to compile the mod_mymodule.c file. The command for compilation is:

     sudo apxs2 -c -i mod_mymodule.c 
     -c  is used for compilation operation
     -i   is used for installation operation

Now if open mymodule directory, the following file would have been added to it:
  • mod_mymodule.la
  • mod_mymodule.lo
  • mod_mymodule.slo
leave it out. Now open mod_mymodule.c. In this file copy the code for apache2.conf file. It looks like this:
LoadModule mymodule_module modules/mod_mymodule.so
<Location /mymodule>
    SetHandler mymodule
</Location>
 Here we have to edit the first line because the location of .so file for the modules will be /usr/lib/apache2/modules, after editing the looks like this:
LoadModule mymodule_module /usr/lib/apache2/modules/mod_mymodule.so<Location /mymodule>    SetHandler mymodule</Location>
now paste this code at bottom of apache2.conf file which is located in /etc/apache2 directory.

Restart apache:


The final step is to restart the apache server using:

     sudo apachectl restart

Now open the web browser and type localhost/mymodule the browser will display the response from the server.



Hope You enjoyed it....

Documentation for apache can be found at the following sites:

http://httpd.apache.org/docs/2.0/
http://www.apachetutor.org/





No comments:

Post a Comment