Monit actually isn’t terribly bad.
When you have it installed and done basic changes on the .conf.
Throw this in and adjust as per needed
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/sshd start"
stop program "/etc/init.d/sshd stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
check process mysql with pidfile /var/run/mysqld/mysqld.pid
group mysql
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout
check process apache with pidfile /var/run/httpd.pid
group apache
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if failed host www.daworm.net port 80 protocol http
and request "/monit/token" then restart
if cpu is greater than 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 500 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if 3 restarts within 5 cycles then timeout
check process postfix with pidfile /var/spool/postfix/pid/master.pid
group mail
start program = "/etc/init.d/postfix start"
stop program = "/etc/init.d/postfix stop"
if failed port 25 protocol smtp then restart
if 5 restarts within 5 cycles then timeout
The main things will be the locations of your PID files and the group/user names of your applications.
Where I got this from for example apache was in as “www” for the user/group. I have to change it to “apache”.
In addition, throw this into a file chmod +x the file and then run it.
#!/bin/bash
echo "This is intended as a guideline only!"
if [ -e /etc/debian_version ]; then
APACHE="apache2"
elif [ -e /etc/redhat-release ]; then
APACHE="httpd"
fi
RSS=`ps -aylC $APACHE |grep "$APACHE" |awk '{print $8'} |sort -n |tail -n 1`
RSS=`expr $RSS / 1024`
echo "Stopping $APACHE to calculate free memory"
/etc/init.d/$APACHE stop &> /dev/null
MEM=`free -m |head -n 2 |tail -n 1 |awk '{free=($4); print free}'`
echo "Starting $APACHE again"
/etc/init.d/$APACHE start &> /dev/null
echo "MaxClients should be around" `expr $MEM / $RSS`
The above will give you a simple output based on your system resources.
After which you adjust your httpd.conf file appropriately for the MaxClients etc.
Here’s what I changed my settings to based on the recommended above and Apache stablised for myself.
<IfModule prefork.c>
StartServers 4
MinSpareServers 3
MaxSpareServers 5
ServerLimit 19
MaxClients 19
MaxRequestsPerChild 4000
</IfModule>
You will have to adjust the MaxClients and ServerLimit to be the same and drop your StartServers down a few (I think default is like 8?.
If you are low traffic this would be more than enough.
AND again, adjust your MinSpareServers and MaxSpareServers.
Yours will be lower than 19. 19 is based on my 2GB ram in the server I have.
And LAST last thing
Look in /var/log/virtualmin/xxx.xxx_error_log
Where xxx.xxx is your domain names.
You may notice some php errors or similar showing up in there. I identified a script from that (which was a few years old and seriously out of date) and since disabling it, i’ve only had to worry about the server doing graceful restarts on log rotates.
:::EDIT:::
Just noticed an entry in my apache portion for Monit…
the “if mem greater than xxx” then restart.
You may want to adjust that down to say 200? Seeing as you only have 256mb.
5 cycles is basically 5 checks, in my case I check every minute.