Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you monitor a file on a web server and log every access, ideally by IP address, in a database (MySQL)?

For security reasons, there is a certain file on my web server I want to be able to monitor access to. Every time it is accessed, I want to have an entry added to a MySQL log table. This way, I can actively respond to security breaches from within the web application.

like image 277
TheEnvironmentalist Avatar asked Nov 03 '22 20:11

TheEnvironmentalist


2 Answers

The Apache HTTP Server provides logging capabilities.

The server access log records all requests processed by the server. The location and content of the access log are controlled by the CustomLog directive. The LogFormat directive can be used to simplify the selection of the contents of the logs. This section describes how to configure the server to record information in the access log.

It can be used to write the log to a file. If you need to store in a MySQL table, run a cron job to import the file into the database.

Further information on logs is here: http://httpd.apache.org/docs/1.3/logs.html#accesslog

like image 98
Rishi Dua Avatar answered Nov 09 '22 08:11

Rishi Dua


Its been removed from PHP7 but for anyone else who finds this post there are a number of options within the FAM (now PECL) extension. This function http://php.net/manual/en/function.fam-monitor-file.php seems to describe what is needed here

Additionally you can access a lot of detail about the files status with http://php.net/manual/en/function.stat.php. Put this within a cron or sleep driven script and you can then see when its changed.

like image 20
Antony Avatar answered Nov 09 '22 08:11

Antony