Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log page requests using Apache?

I am hosting some documents on my local machine using Apache for my group. I have copied/linked the documents under /var/www/html.

Is there a way to log the requests with timestamps for each request?

like image 486
Lazer Avatar asked Mar 14 '11 06:03

Lazer


2 Answers

Logging should already be enabled out of the box, and be logging to a file called access_log. I've never seen an install where it wasn't already turned on.

Usually the directive for where to log to is set in a file called httpd.conf. Most of the time, the file is in /var/log/apache2.

like image 115
Lynn Crumbling Avatar answered Oct 21 '22 02:10

Lynn Crumbling


Assuming your apache server is running and you have shell access:

do this:

genja ~ # ps aux |grep apache|tail -n1 
root     23605  0.0  0.2 248636 10684 ?        Ss   Jun08   0:06 /usr/sbin/apache2 -D DEFAULT_VHOST -D INFO -D LANGUAGE -D MANUAL -D SSL -D SSL_DEFAULT_VHOST -D PHP5 -D PERL -D PROXY -D SCGI -d /usr/lib64/apache2 -f /etc/apache2/httpd.conf -k start

.. that is an apache daemon process. You are looking for "-f /etc/apache2/http.conf". If your apache server was not told (by the distro init scripts) where to get the config file it will look in the default location which could be under: /etc/apache2/ or /etc/httpd/ (or anywhere else, really, but those two are most common). In that folder you will find a file called apache2.conf or httpd.conf
just try this:

find /etc/ -iname httpd.conf -o -iname apache2.conf

Once you have located the configuration file for the apache server, look for the line where they include the modules configurations. On my system it looks like this:

Include /etc/apache2/modules.d/*.conf

Now you need to figure out where the log file is:

genja modules.d # grep CustomLog /etc/apache2/modules.d/*
00_mod_log_config.conf:# a CustomLog directive (see below).
00_mod_log_config.conf:CustomLog /var/log/apache2/access_log common
00_mod_log_config.conf:#CustomLog /var/log/apache2/referer_log referer
00_mod_log_config.conf:#CustomLog /var/log/apache2/agent_logs agent
00_mod_log_config.conf:#CustomLog /var/log/apache2/access_log combined

you are looking for: CustomLog /var/log/apache2/access_log common.
Note that this CustomLog directive could be in the main apache2.conf file too.
Now you have the location of the log file. The most likely thing to be stopping your server from logging now is file permissions. Make sure the directory given under CustomLog exists and that the apache server can write to it:

as root:

mkdir -p /var/log/apache2
touch /var/log/apache2/access_log
chown -R APACHEUSER /var/log/apache2
chmod 755 /var/log/apache2
chmod 644 /var/apache2/access_log

where APACHEUSER is likely to be apache or www or even httpd. You can figure that out by running:

genja ~ # ps aux |grep apache |awk '{print $1}'
apache
(...)
apache
root

So the user running the apache server on my system is actually called apache. It is not root. Or at least should not be.

restart your server after changing the permission of that file. I dont know how your distro does it. There is a service[s] command on ubuntu I think. But you can always run the init script directly:

/etc/init.d/apache restart (ir may be under a different location on your distro) just reboot the whole computer if you cant figure out how to restart the apache server.

If the access_log file is empty after you restarted the server change APACHEUSER to root in the above command.

after all this just use your favourite pager or text editor to look at the log. Or even tail -f to monitor it in real time. I hope this helps. gl.

like image 35
Ярослав Рахматуллин Avatar answered Oct 21 '22 03:10

Ярослав Рахматуллин