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.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...