Monitor file changes in your Website
This script can be used to notify you when a file changed in your website. This is useful when you want to know when a file changed or your site is hacked and you to monitor your site for file changes so you know when hacker upload or modify a file.
First you need to add your web site to GIT.
This can be with
1 2 3 4 |
cd /var/www/html git init git add . git commit -a -m "inital commit" |
Replace /var/www/html with actual DocumentRoot for your web site.
Every time you modify or add a file, you need to commit it to git you will get alerted. You can commit a new file to git with command
1 2 |
git add FILE_NAME git commit -a |
Create a file
1 2 |
mkdir /usr/serverok/ vi /usr/serverok/check-files.php |
Add following content
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php # Author: ServerOk # https://serverok.in/monitor-file-changes-in-your-website chdir("/var/www/html"); exec("git status", $result); $strNoChange = "nothing to commit, working tree clean"; if ($result[1] == $strNoChange) { exit; } $gitStatus = implode("\n",$result); |
In the script, replace /var/www/html with actual document root of your web site. Change email and site name with your email and domain name.
Set following cronjob.
1 |
0 * * * * /usr/bin/php /usr/serverok/check-files.php |
Cronjob will run every 1 hour and email you if any file change is detected. You can modify cronjob if you want to monitor more frequently, every 1 hour will be fine for most uses.
If you have a folder or file that you need to ignore, you can create a file with name “.gitignore” and add path to file/folder in it, git will ignore files/folders listed in it.