Friday 24 June 2016

Apache Server Interview Questions & Answers

Q. What is the Apache server ? 
  •  Apache Web Server is one of the most secure, powerful and popular open source HTTP Server which is written in portable C. It can be used to host anything from personal web sites to corporate domains.
  • It mostly serves static content by itself, but there are many add-on modules (some of which come with Apache itself) that let it modify the content and also serve dynamic content written in Perl, PHP, Python, Ruby, or other languages.       
Q. How to check the version of Apache server ?
    First, use the rpm command to check whether Apache installed or not. If it’s installed, then use
    httpd -v command to check its version.
    # rpm -qa | grep httpd

    httpd-devel-2.2.15-29.el6.centos.i686
    httpd-2.2.15-29.el6.centos.i686
    httpd-tools-2.2.15-29.el6.centos.i686

    # httpd -v

    Server version: Apache/2.2.15 (Unix)
    Server built:   Aug 13 2013 17:27:11

    In ubuntu linux try following command.
    mukesh@ubuntu:~$ apache2 -v
    Server version: Apache/2.2.22 (Ubuntu)
  
Q.  On which version of apache you have worked ?
    httpd-2.2.3

Q. What is location of log files in Apache server ?
    /var/log/httpd/access_log
    /var/log/httpd/error_log
  
    In Ubuntu:
    /var/log/apache2/other_vhosts_access.log
    /var/log/apache2/error.log
    /var/log/apache2/access.log
  
Q. Apache runs as which user? and location of main config file?
   Apache runs with the user "nobody" and httpd daemon. Apache main configuration file:
   /etc/httpd/conf/httpd.conf (CentOS/RHEL/Fedora)
   /etc/apache2.conf  or /etc/apache2/apache2.conf (Ubuntu/Debian)
 
Q. On which port Apache listens http and https both?
   By default Apache runs on http port 80 and https port 443 (for SSL certificate). You can also use
   netstat command to check ports.
    [root@mukesh ~]# netstat -antp | grep http

    tcp        0      0 :::80                       :::*                        LISTEN      1076/httpd        
    tcp        0      0 :::443                      :::*                        LISTEN      1076/httpd
  
Q. Where you can find all configuration directories of Apache Web Server?
   /etc/httpd/conf/ (CentOS/RHEL/Fedora)
   /etc/apache2/     (Ubuntu/Debian)
 
Q. You see a error "connection reset by peer" in error log. What could be the reason for this?
   If the end user terminates the connection when the server response was being formed or was being
   transferred, we see "connection reset by peer" in the error log.

Q. What is virtual hosting?
     Virtual hosting is a method for hosting multiple domain names on a server using a single IP
     address. Virtual hosting allows one server to share its resources, such as memory and processor
     cycles, in order to use its resources more efficiently.

   Sample VirtualHost:
    <VirtualHost *:80>
       ServerAdmin webmaster@dummy-host.example.com
       DocumentRoot /www/docs/dummy-host.example.com
       ServerName dummy-host.example.com
       ErrorLog logs/dummy-host.example.com-error_log
       CustomLog logs/dummy-host.example.com-access_log common
    </VirtualHost>

    Where :
    ServerAdmin  : Its usually the email address of the website owner, where the error or notification
                               can be sent.
    DocumentRoot : location where the web files are located in the server(Necessary).
    ServerName   : Its the domain name which you want to access from your web
                               browser(Necessary).
    ErrorLog     : Its the location of the log file where all the domain related logs are being recorded.


Q. What are the types of virtual hosts ?
    There are two types of virtual host name-based and IP-based.

    Name-based virtual host means that multiple names are running on each IP address.
    IP-based virtual host means that a different IP address exists for each website served. Most
    configurations are named-based because it only requires one IP address.
  
Q. If you have only one IP address, but you want to host two web sites on your server.
     What will you do?
     In this case I will use Name Based Virtual hosting.
  
    ServerName 10.111.203.25
    NameVirtualHost *:80
    <VirtualHost *:80>
        ServerName web1.test.com
        DocumentRoot /var/www/html/web1
    </VirtualHost>

    <VirtualHost *:80>
        ServerName web2.test2.com
        DocumentRoot /var/www/html/web2
    </VirtualHost>

  
Q. You need to install Apache web server on Ubuntu server. How can you download and install the
     Apache2 package?
     sudo apt-get install Apache2
         OR
     sudo aptitude install Apache2
     aptitude is known to sort out program dependencies in a better way.


Q. What are Iptables?
     Iptables are user space application programs that allow a system administrator to configure the
     tables provided by the Linux kernel firewall.

Q. Which privilege is required to operate iptables?
     Iptables require root user privilege for execution, otherwise they fail to function.

Q. What type of ports are 80 and 443?
     The port 80 is a regular port while port 443 is a secure port.

Q. How to restart Apache web server ?
    /etc/init.d/httpd  restart  (CentOS/RHEL/Fedora)
    service httpd restart        (CentOS/RHEL/Fedora)
  
    sudo /etc/init.d/apache2 restart (Ubuntu/Debian)

Q. What is the difference between a restart and a graceful restart of a web server?
    During a normal restart, the server is stopped and then started, causing some requests to be lost.
    A graceful restart allows Apache children to continue to serve their current requests until they can
    be replaced with children running the new configuration.
  
Q. What is DocumentRoot ?
     It is a location of web files which are accessible by clients. The default DocumentRoot of Apache
     is /var/www/html or /var/www. This can be changed to anything, by setting up "DocumentRoot"
     in a virtual host of configuration file of domain.
  
Q. How to change default Apache Port and How Listen Directive works in Apache?
    There is a directive "Listen" in httpd.conf file which allows us to change the default Apache port.
    With the help of Listen directive we can make Apache listen on different port as well as different
    interfaces.

   To change the Apache default port, please open your Apache main configuration file httpd.conf or
   apache2.conf or ports.conf(ubuntu/Debian) file with VI editor.
    [root@mukesh ~]# vi /etc/httpd/conf/httpd.conf
    [root@mukesh ~]# vi /etc/apache2/apache2.conf
    [root@mukesh ~]# vi /etc/apache2/ports.conf
  
    Search for the word "Listen", comment the original line and write your own directive below that
     line as following.
    # Listen 80
        OR
    Listen 8080
    Listen 172.16.16.1:8080
    Save the file and restart the web server.
    [root@mukesh ~]# service httpd restart
    [root@mukesh ~]# service apache2 restart
  
Q. Can we have two Apache Web servers on a single machine?
     Yes, we can run two different Apache servers at one time on a Linux machine, but the condition
     for that is they should listen on different ports and we can change the ports with Listen directive
     of  Apache.
 
Q. What is the difference between <Location> and <Directory>?
    <Location> is used to set element related to the URL / address bar of the web server.
    <Directory> refers that the location of file system object on the server

Q. If you have added "loglevel Debug" in httpd.conf file, than what will happen?
   It will give you more information in the error log in order to debug a problem.
   With the help of Loglevel Debug option, we can get/log more information in the error logs which
    helps us to debug a problem.
  
======================Some More Questions ==============================

Q. What type of IP is required for Apache Web Server to host our website?
    a DSL connection with a static IP then we can use the Apache web server to host our website.

Q. While installing Apache HTTP server, do we need SSL and PHP support?
   Yes, SSL and PHP support is required to install Apache HTTP server.

Q. Is there any size limit for URL rewriting and aliasing in Apache web server?
   Apache has no fixed limit on the numbers of Aliases and Redirects which may be declared in the
   config files.

Q. Create a new user file and add a user with username "test" and password "test".
    htpasswd -c /usr/local/etc/httpd/users test
    User will be prompted to provide the password. The -c argument tells htpasswd to create new file
    for users.
    To add more users, -c argument can be omitted.

Q. What do you mean by a valid ServerName directive?
    The DNS system is used to associate IP addresses with domain names. The value of ServerName is
     returned when the server generates a URL. If you are using a certain domain name, you must
     make sure that it is included in your DNS system and will be available to clients visiting your site.

Q. What is the use of mod_perl module?
    mod_perl scripting module to allow better Perl script performance and easy integration with the
    Web server.

Q. Can you record the MAC (hardware) address of clients that access your server.
    No

Q. Can you record all the cookies sent to your server by clients in Web Server logs?
    Yes, add following lines in httpd.conf file.
    CustomLog logs/cookies_in.log "%{UNIQUE_ID}e %{Cookie}i" CustomLog
    logs/cookies2_in.log "%{UNIQUE_ID}e %{Cookie2}i"

Q. Can we do automatically roll over the Apache logs at specific times without having to shut down
     and restart the server?
    Yes
    Use CustomLog and the rotatelogs programs
    Add following line in httpd.conf file. CustomLog "| /path/to/rotatelogs/path/to/logs/access_log.%Y-%m-%d 86400" combined

Q. What we can do to find out how people are reaching your site?
     Add the following effector to your activity log format. %{Referer}

Q. Can I serve content out of a directory other than the DocumentRootdirectory?
    Yes, by using "Alias" we can do this.

Q. If you have to more than one URL map to the same directory but you don't have multiple Alias
     directives. What you will do?
    In this case I will use “AliasMatch” directives.
    The AliasMatch directive allows you to use regular expressions to match arbitrary patterns in
     URLs and map anything matching the pattern to the desired URL.

Q. I want to stop people using my site by Proxy server. Is it possible?
    <Directory proxy:http://www.test.com/myfiles>
        Order Allow,Deny
        Deny from all
        Satisfy All
    </Directory>

Q. What is mod_evasive module?
    mod_evasive is a third-party module that performs one simple task, and performs it very well. It detects when your site is receiving a Denial of Service (DoS) attack, and it prevents that attack from doing as much damage. mod_evasive detects when a single client is making multiple requests in a short period of time, and denies further requests from that client. The period for which the ban is in place can be very short, because it just gets renewed the next time a request is detected from that same host.

Q. How to enable PHP scripts on your server?
   If you have mod_php installed, use AddHandler to map .php and .phtml files to the PHP handler.
   AddHandler application/x-httpd-php .phtml .php

Q. Which tool you have used for Apache benchmarking?
    ab (Apache bench)
    ab -n 1000 -c 10 http://www.test.com/test.html

Q. Can we cache files which are viewed frequently?
    Yes we can do it by using mod_file_cache module.
    CacheFile /www/htdocs/index.html

Q. Can Apache be secured with TCP wrappers?
    No, It can’t be secured with the TCP wrappers since it doesn’t support libwrap.a library of Linux.

Q. Can we host files in different folder and what is Alias directive?
   Yes, this can be achieved by Alias directive in the main Apache configuration file. Alias directive
    maps resources in File system, it takes a URL path and substitute it with a file or directory path on
    the system with is set up to redirect.
   To use Alias directive, Its the part of mod_alias module of Apache. The default syntax of Alias
    directive is:

    Alias /images /var/data/images/
  
    Here in above example, /images url prefix to the /var/data/images prefix that mean clients will query for "http://www.test.com/images/sample-image.png" and Apache will pick up the "sample-image.png" file from /var/data/images/sample-image.png on the server. It’s also known as URL Mapping.

Q. What do you understand by "DirectoryIndex"?
   DirectoryIndex is the name of first file which Apache looks for when a request comes from a domain. For Example: www.test.com is requested by the client, so Apache will go the document root of that website and looks for the index file (first file to display). The default setting of DirectoryIndex is .html index.html index.php, if you have different names of your first file, you need to make the changes in httpd.conf or apache2.conf for DirectoryIndex value to display that to your client browser.


Q. How to disable Directory listing when an index file is missing?
   If, the main index file is missing in the website root directory, then the Apache will lists all the contents like files and folder of the website on the browser instead of Main website pages. To stop Apache directory listing, you can set the following rule in the main configuration file globally or in .htaccess file for a particular website.
    <Directory /var/www/html>
       Options -Indexes
    </Directory>

Q. What do you understand by MPM in Apache?
   MPM stands for Multi Processing Modules, actually Apache follows some mechanism to accept and complete web server requests.

Q. What is the difference between Worker and Prefork MPM?
   Both MPMs, Worker and prefork has their own mechanism to work with Apache. It totally depends on you that in which mode you want to start your Apache.
   Basic difference between Worker and MPM is in their process of spawning the child process. In the Prefork MPM, a master httpd process is started and this master process starts manages all other child processes to serve client requests. Whereas, In the worker MPM one httpd process is active, and it uses different threads to serve client requests.
   Prefork MPM uses multiple child processes with one thread each, where worker MPM uses multiple child processes with many threads each.Connection handling in the Prefork MPM, each process handles one connection at a time, whereas in the Worker mpm each thread handles one connection at a time.Memory footprints Prefork MPM Large memory footprints, where Worker has smaller memory footprints.

Q. What is the use of LimitRequestBody and how to put limit on your uploads?
   LimitRequestBody directive is used to put a limit on the upload size.
   For example: I want to put limits of 100000 Bytes in the folder /var/www/html/mks/uploads. So, you need to add following directive in Apache
    configuration file.
    <Directory "/var/www/html/mks/uploads">
        LimitRequestBody 100000
    </Directory>

Q. What is mod_perl and mod_php?
    mod_perl is an Apache module which is compiled with Apache for easy integration and to
    increase the performance of Perl scripts.
    mod_php is used for easy integration of PHP scripts by the web server, it embeds the PHP interpreter inside the Apache process. Its forces Apache child process to use more memory and works with Apache only but still very popular.

Q. What is the use of mod_ssl and how SSL works with Apache?
   Mod_ssl package is an Apache module, which allows Apache to establish its connection and transfer all the data in a secure encrypted environment. With the help of SSL certificates, all the Login details and other important secret details get transferred in an encrypted manner over the Internet, which prevents our data from Eavesdropping and IP spoofing.

Shell Script To Read a Text File Line by Line

Here i am going to show you two methods of reading a text file with shell script. For our entire exercise consider a demo.txt file which having the following contents .

mukesh@ubuntu:~/Desktop$ cat demo.txt
this is line one
Hello everyone\\ how are you.
this is\ line third with back slash.
This is sample last line.


Method 01: cat file then read line

#! /bin/bash

# Script name : readFile.sh
# Description : Script will read demo.txt which is in current directory.

cat demo.txt | while read LINE
do
        echo $LINE
done


Output:
this is line one
Hello everyone\ how are you.
this is line third with back slash.
This is sample last line.



Method2: Redirecting file into while loop and then read line.

#! /bin/bash

# Script name : readFile.sh
# Description : Script will read demo.txt which is in current directory.

while read LINE
do
    #do what you want to $LINE
    echo $LINE
done < demo.txt

Output:
this is line one
Hello everyone\ how are you.
this is line third with back slash.
This is sample last line.


Note:
If you observe above both the program you will notice in the output back slash "\" is in not printing.
This is happening because program treating "\" as a special character. To avoid this problem use -r while reading line as in the following script.

#!/bin/bash
# Script name : readFile.sh
# Description : Script reading the demo.txt file line by line and treating backslash as a normal character.


if [ $# -ne 1 ]
then
    echo "Usage : $0 filename "
    echo "Give the file name as command line argument."
    echo "Ex.: $0 demo.txt"
    exit 1
fi

filename="$1"

while read -r LINE
do
    echo "$LINE"
done < "$filename"


Output:
this is line one
Hello everyone\\ how are you.
this is\ line third with back slash.
This is sample last line.


where :
-r  Do not treat a backslash character in any special way.
    Consider each backslash to be part of the input line.
   

Shell Script Programs For Beginners - SET 02 ( Case and Loop )

1). Switch case Example 01
2). Switch case Example 02
3). Switch case Example 03
4). For loop Example01 : Simple for loop.
5). For Loop Example02 : List all content of current directory.
6). For loop Example03: Executing unix command in loop.
7). While loop Example 01: simple while loop
8). Until loop Example01: Simple until loop
9). For loop with break keyword
10). For loop with continue keyword

1) Switch case Example 01

#!/bin/bash
#
#Script Name : caseExample01.sh 
#Description : Script will read a value from user and choose accordingly choice.
#
echo "Enter the number from 1 to 4"

read x

case $x in 
    1) echo "One";;
    2) echo "Two";;
    3) echo "Three";;
    4) echo "Four";;
    *) echo "Wrong input, Please enter number 1 to 4";;
esac

Output:
Case 1 : if x=2
Two
Case 2 : if x=4
Four
Case 3 : if x=0 or 5 or 10 or d
Wrong input, Please enter number 1 to 4


2) Switch case Example02
#!/bin/bash
#
#Script Name : caseExample02.sh 
#Description : Script will read a value from user and choose accordingly choice.
#               Keeping default '*)' in between rather keeping it at end and observe the output of program.
#

echo "Enter the number from 1 to 4"
read x
case $x in 
    1) echo "One";;
    2) echo "Two";;
    3) echo "Three";;
    *) echo "Wrong input, Please enter number 1 to 4";;
    4) echo "Four";;
esac

Output:
Case 1 : if x=1
One
Case 2 : if x=4
Wrong input, Please enter number 1 to 4
Case 3 : if x=0 or 5 or 10 or d
Wrong input, Please enter number 1 to 4


If you observe the above program it is clear that the case which are places after default case those does not come under consideration of program.

3) Switch case Example 03
#!/bin/bash
#
#Script Name : caseExample03.sh 
#Description : Script will read a value from user and choose accordingly choice.
#               We can use expression also in case.
#

case `expr 20 + 5` in 
    10) echo "Ten";;
    25) echo "Twenty Five";;
    30) echo "Thirty";;
    *) echo "Wrong input";;
esac

Output:
Twenty Five

Loop:
Loops are very powerful tool to perform a set of instructions repeatedly until a certain condition is reached.
For,While and Until are the popular loop in shell program . Following are the example of each kind of loops.

4) For loop Example01 : Simple for loop
#!/bin/bash
#
#Script Name : for01.sh
#Description : Script showing demo of a simple for loop
#

for val in a b c d e f g h i j
do
        echo $val
done


5) For Loop Example02 : List all content of current directory
#!/bin/sh
#
#Script Name : for02.sh
#Description : Script showing demo of a for loop in which program listing all the contents of current directory.

for val in *
do
  echo "$val"
done

Remember * is Wildcard which will consider all the contents of current directory hence the above program will loop all the file and directory available in current directory.

6) For loop Example03: Executing unix command in loop
#!/bin/sh
#
#Script Name : for03.sh
#Description : Script showing demo of a for loop in which program is executing unix commands

for val in date cal exit who

do
   "$val"
done

Output:
Mon Dec 12 22:05:57 IST 2016
   December 2016
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Note : Replcae "$val" to echo "$val" in above program and observe the output.


While Loop:

In 'while' loop, we give one control command to control the iteration of the loop
A set of commands will be executed as long as the control command's exit status is 0
When the control command's exit status becomes 1, the 'while' loop is quit.

7) While loop Example 01: simple while loop
#!/bin/sh
#
#Script Name : while01.sh
#Description : A simple while loop demo

a=0

while [ $a -lt 10 ]
do
   echo -ne "$a\t"
   a=`expr $a + 1`
done


Output:
0       1       2       3       4       5       6       7       8       9

Note : In above program i used -n and -e option where -n option is used to disable trailing new line character.
and -e option is used to enable escape sequences and \t is used for tab.

Until loop:
A until loop is used to repeat set of commands as long as the control command's exit status is 1
When the control command's exit status becomes 0, the loop is get quit.

8) Until loop Example01: Simple until loop
#!/bin/sh
#
#Script Name : until01.sh
#Description : A simple until loop demo


echo  "Type 'end' to exit  "
read var
until  [  "$var"  =  "end"  ]
do
    `echo $var`
     read  var
done

echo  "End of the script and exiting"


Output:
Type 'end' to exit
date
Mon Dec 12 23:13:00 IST 2016
end
End of the script and exiting


Break and Continue Keywords:
In shell scripts we can use break and continue keywords only in the looping statements (i.e., while , until and for).

9) For loop with break keyword
In the following example as soon variable i become equal to 5 the loop breaks and quit.
#!/bin/sh
#
#Script Name : break.sh
#Description : A simple break keyword in a loop


for i in 1 2 3 4 5 6 7 8
do
    echo $i
    if [ $i -eq 5 ]
    then
        break
    fi

done

Output:
1 2 3 4 5

10) For loop with <b>continue</b> keyword
Following example showing the a demo of 'continue' keyword in which it print which are not equal to 5, as soon controller reach to 5 continue keyword move it to the next number.

#!/bin/sh
#
#Script Name : continue.sh
#Description : A simple continue keyword in a loop

for i in 1 2 3 4 5 6 7 8

do
#   echo $i
    if [ $i -ne 5 ]
    then
        echo $i
    else
        continue
    fi

done


Output:
1 2 3 4 6 7 8
Related Posts Plugin for WordPress, Blogger...