Basics knowledge of Apache HTTP Server

I created this article as a reminder about some basics of Apache HTTP Server with real examples. I will present how to start apache,look for logs, and give information about the configuration.

The HTTP server handles request and does the mapping from URL to Filesystem locations. More information at https://httpd.apache.org/docs/2.4/en/urlmapping.html

How do we start Apache ?

On Unix, the httpd program is run as a daemon that executes continuously in the background to handle requests.
https://httpd.apache.org/docs/2.4/en/invoking.html

According to the documentation, httpd should be invoked by a script called apachectl.
https://httpd.apache.org/docs/2.4/en/programs/httpd.html

Let’s see with an example a running apache daemon. It happens I have an apache server on a test machine .

ps -edf | grep httpd
root     23154     1  0 10:44 ?        00:00:00 /usr/sbin/httpd.worker -f /var/apache/conf/httpd.conf -f /var/apache/conf/httpd.conf -k start

This command ps – edf tells us where is located the configuration file used for this apache running. On our machine exists many old install of apache. Therefore this command helps to find the right configuration file used by the current apache server.

One would wonder why httpd.worker is being run and not httpd? What is httpd.worker?
The answer to this question is here :

http://serverfault.com/questions/213956/what-is-the-difference-between-apachectl-and-httpd-worker

Basically at the installation of our product we install a new Apache/tomcat and deploy the application automatically. More information about how it is installed with shell scripts and deployed from Jenkins at
https://julienprog.wordpress.com/2015/08/05/automate-the-installation-of-a-product-with-bash-scripts/

Here are the commands executed by the shell script to launch our Apache server when installing on our machine :

#----------------------------------------------------------------------------------------------------
# Start the Apache http daemon
#----------------------------------------------------------------------------------------------------
start_httpd()
{
    write_log "Starting Apache..."
            export OPTIONS="-f ${ApacheDir}/conf/httpd.conf"
/usr/sbin/apachectl $OPTIONS -k start
ReturnCode=$?
#more code to handle the response
}

Where are the logs?

To my mind it is an important question if we want to troubleshoot problems on the server.
More about logging and how to understand the format of the logs :
http://httpd.apache.org/docs/2.4/en/logs.html

Previously we find out where is the configuration for the httpd running at /var/apache/conf/httpd.conf. If I look into this file i can find where are located the log :

ErrorLog logs/error_log

Thus , on my server, the error log is located at /var/apache/logs/error_log

NOTE : If you have several Virtualhosts you will have one log for each of them.
It happens that on the directory /var/apache/logs there are more logs than just error_log. It is because we have defined some other configuration in /var/apache/conf.d.

For example if i want to find which module generate ssl_error_log, i will execute on the linux machine :

 grep -rnw '/var/apache' -e "ssl_error_log"
/var/apache/conf.d/mod_jk.conf:57:ErrorLog logs/ssl_error_log

Understand Apache Configuration

The following considerations are about about the HTTP Server configuration : “httpd.conf”, and other custom made configuration files.

What is the purpose of DocumentRoot ?

Extract from httpd.conf :

#
# 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 "/var/apache/www/html"

Files and directories underneath the DocumentRoot make up the basic document tree which will be visible from the web.

Therefore what you see on a website is the tree structure under DocumentRoot :
Then a request for http://www.example.com/fish/ will cause httpd to attempt to serve the file /var/www/html/fish/index.html.

What does IfModule tag in configuration files ?

Did you notice

<IfModule>

tag in configuration files of apache server and wondered what is it exactly ?

In the following example, the MimeMagicFile directive will be applied only if mod_mime_magic is available.

<IfModule mod_mime_magic.c>
    MimeMagicFile "conf/magic"
</IfModule>

https://httpd.apache.org/docs/2.4/en/sections.html

Therefore this tag will apply directives from module.

What is ServerName ?

Extract from httpd.conf :

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If this is not set to valid DNS name for your host, server-generated
# redirections will not work.  See also the UseCanonicalName directive.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make
# redirections work in a sensible way.
#
ServerName apache-myinstance:8080

What is doing the directive Listen ?

The Listen directive tells the server to accept incoming requests only on the specified port(s) or address-and-port combinations.
https://httpd.apache.org/docs/2.4/en/bind.html

For example we have on our server used two times the directive for

# Virtual HOST HTTP **
Listen apache-myinstance:8080
# Virtual HOST HTTPS **
Listen apache-myinstance:447

What is a Virtual Host ?

httpd is also capable of Virtual Hosting, where the server receives requests for more than one host. For example on the same server you would have http://www.mywebsite.com and http://www.myotherwebsite.com,etc.. running.

<VirtualHost apache-myinstance:447>

      JkMount /* myworker

ErrorLog logs/ssl_error_log
CustomLog logs/ssl_access.log java_format
LogLevel warn

SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLCertificateFile /etc/certs/localhost.crt
SSLCertificateKeyFile /etc/certs/localhost.key

<Files ~ "\.(cgi|shtml|phtml|php3?)$">
    SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>

SetEnvIf User-Agent ".*MSIE.*"          nokeepalive ssl-unclean-shutdown          downgrade-1.0 force-response-1.0

</VirtualHost>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s