开发者

How to log page requests using Apache?

I am hosting som开发者_高级运维e 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?


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.


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.


I can imagine you have set up a virtual host (so called vhost) for your /var/www/html document root. So you can just add your log set up requirements within that specific vhost, sothat you'll get a specific log file.

Like this example :

<VirtualHost *:80>

    DocumentRoot /var/www/html
    ServerName www.yourdomain.org
    ServerAdmin webmaster@domain.de

    <Directory "/var/www/html">
            Options FollowSymLinks MultiViews
            AllowOverride all
            Order allow,deny
            Allow from all
            # if you need some users basic authentication
            # AuthType Basic
            # AuthName "MY DOMAIN AUTHENTICATION"
            # AuthUserFile ${APACHE_BASEDIR}/conf.d/special_users
            # Require user foobar
    </Directory>

    # your log specific requirements
    # LOGS (overwriting previous conf See /etc/apache2/apache2.conf)
    # Possible values for LogLevel : debug, info, notice, warn, error, crit, alert, emerg.
    LogLevel info
    ErrorLog /var/log/apache2/my_special_error.log
    # Option 1 (recommended): you merge these specific logs with the overall log file name and format
    # CustomLog /var/log/apache2/access.log combined
    # Or, Option 2: you set up a specific log file for that domain
    LogFormat "%h %l %u %t \"%r\" %>s %b" my_special_access.log
    CustomLog /var/log/apache2/my_special_access.log
</VirtualHost>

You will find the way the logs are formatted in the main apache configuration file, eg /etc/apache2/apache2.conf in debian's like distros. Or you take them as they are, or you overwrite them in your virtual host configuration. If you are not working with virtual hosts, that does not change much these principles.

Then you can manipulate the access.log file to extract, analyze, copy or do whatever you want with this. You can do that either in shell bash scripting (or perl or even python or C or...). I would recommend bash, that you can easily automatized as a cronjob.


LogFormat "%h %l %u %t \"%r\" %>s %b" mylog
CustomLog logs/doc_root_access_log mylog

which will output like below (the timestamp) to the log file doc_root_access_log

127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326

You should really refer here as they have plenties of explanation especially on the log modifier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜