I’m using Ubuntu 12.04 on my server and local computer
If you use Windows computer for your local. Here is tutorial how to get it working
How to remotely monitor Linux server from Windows
testerError1

In this tutorial I’m going to create simple script that checks every 30 minutes that LAMP is running properly on my server. If something is wrong it shows notification on your local computers desktop.

Install curl to local computer

First install curl and test your site where you going to put your tester index.php. Anwser should be empty.

local$ sudo apt-get install curl
local$ curl tester.net

Create mysql user to server

Create user tester in MySQL

server$ mysql -u root -p

CREATE DATABASE tester;
GRANT ALL ON tester.* TO tester@localhost IDENTIFIED BY "SECRETPASSWORD";
EXIT;

Create tester php file to server

Create folder where is index.php what checks connection to MySQL

server$ cd public_html/
server$ mkdir tester
server$ nano tester/index.php

<?php
	// Create connection
	$con=mysqli_connect("localhost","tester","SECRETPASSWORD","tester");

	// Check connection
	if (!mysqli_connect_errno()) {
		echo "works";
	} else {
                echo "not working";
        }

?> 

Create new site to Apache in server

Add new site to Apache and enable it

server$ sudoedit /etc/apache2/sites-available/tester.net

<VirtualHost *:80>
        ServerName tester.net
        ServerAlias www.tester.net
        DocumentRoot "/home/user/public_html/tester/"
</VirtualHost>

server$ sudo a2ensite tester.net
server$ sudo service apache2 reload

Check that server side works using local

Check that site really works. Curl should print “works”

local$ curl tester.net
works

Create script to local

Create shell script that will get tester.net site with curl and checks if site is still on.

local$ nano serverTesterScript.sh

#!/bin/bash/

URL=$(/usr/bin/curl -s tester.net)

if [ -z "$URL" ]
then
        /usr/bin/notify-send 'Something wrong with the server' 'Answer was empty'
else
        if [ "$URL" != "works " ]; then
                /usr/bin/notify-send 'Something wrong with the server' 'Server doesnt work properly'
        fi
fi

Schedule script to local using crontab

Then add your script to crontab so it runs script every minute.

local$ crontab -e

*/1 *   *   *   *    export DISPLAY=:0.0 && sudo -u user bash /home/user/serverTesterScript.sh

Test everything works in local

Now you can test if it works.

server$ mv public_html/tester/index.php public_html/tester/index.php2

testerError1

server$ mv public_html/tester/index.php2 public_html/tester/index.php
server$ sudo service apache2 stop

testerError2

server$ sudo service apache2 start
server$ sudo service mysql stop

testerError3

server$ sudo service mysql start

Start using

Now everything should work and you can edit crontab so script is runned every 30 minutes.

local$ crontab -e

*/30 *   *   *   *    export DISPLAY=:0.0 && sudo -u user bash /home/user/serverTesterScript.sh