The success of mapofstrange.com has caused no end of problems! Every host it’s been on over the past year has told us where to go. We’ve been told in no uncertain terms to move to a different host or sign up for a dedicated server. The problem is that it keeps bringing the server down, which isn’t exactly pleasant for other people who are being hosted on that server…
We recently migrated to a VPS (Virtual Private Server), which essentially means that we’ve got full control over the hosting of the site - if the server goes down it’s up to us to fix it. Unfortunately, the server has been going down. A lot. Every couple of days the site just dissappears. What had happened was that Apache had somehow managed to fall over. The fix for this was relatively straightforward. A simple cron job running every minute of every day checks to see if the site is running and if it isn’t, it restarts Apache.
The script that does this is a python script -
import httplib, os
conn = httplib.HTTPConnection(”www.mapofstrange.com”)
r1 = conn.getresponse() #can it get a response from mapofstrange.com
os.system(’sh /etc/init.d/httpd restart’)
print “Apache restarted successfully”
print “Failed to restart Apache”
This does a quick check (using httplib) to see if it can connect to the server. If it can’t it simply trys to restart Apache. You can also set it up to restart other services like MySQL (
os.system('sh /etc/init.d/mysql restart') ). We’re lucky that we’ve got cpanel running, as it provides us with these scripts to restart essential services under etc/init.d. We saved this onto the server, then setup the cron job to run this script every minute. To get the cron job to work, we simply added the following line to the root cron file under /var/spool/cron in my file system.
* * * * * python /tmp/restart.py
We also got it to email us the results, so we know when it’s gone down and how quickly it managed to restart.
The site/server still goes down (probably about once a day), but it’s only ever down for a minute (or two if there is a serious problem). This really simple solution means that we don’t have to concern ourselves with server admin on a regular basis - the biggest issue is automatically taken care of by the system itself.