BASH! scripting that is…
I’ve been looking at ways to take my new Fedora 10 server offline for maintenance and other things recently and basically came to the conclusion that simply disabling all the websites in one fell swoop with a shell script was the solution so I wrote these two bad boys to automate some of the process for me. There are a couple of pre-requisites to these scripts working of course.
- I use (read: create) a separate vhost conf file for each and every website I host from the server. I added a couple of lines to the end of /etc/httpd/conf/httpd.conf that includes the .conf files from the /etc/httpd/vhosts folder. This is a configuration setting similar to that of Ubuntu’s default but the idea is absent from Fedora. It’s a good idea and, funnily enough, I got the idea for the following scripts from Ubuntu also.
- The scripts assume you want to take all websites (vhosts) hosted on your server down completely. You won’t be able to access them either! If that’s what you want to do, look at mod_rewrite and redirect all requests to a maintenance page but exclude your IP from the rewrite. I’ll probably be looking at this myself in the coming weeks so hang fire if you want.
httpd_maint_down.sh – takes down all vhosts by changing the extension of all configuration files and reloading Apache. The script also touch(es) the default html page served by Apache so that it shows the time the server went down for maintenance.
1 |
#!/bin/bash |
1 2 |
CONF_DIR=/etc/httpd/vhosts ROOT_UID=0 |
1 2 3 4 5 |
if [ "$UID" -ne "$ROOT_UID" ] then echo "Must be root to run this script." exit $E_NOTROOT fi |
1 2 3 |
rename .conf .inactive $CONF_DIR/*.conf # Move all vhosts to inactive state touch /var/www/html/index.php # Update the maintenance down time service httpd restart # Force a reload of all config files (none now) |
1 |
exit # |
httpd_maint_up.sh – reverses everything done above. Except for the touch of course.
1 |
#!/bin/bash |
1 2 |
CONF_DIR=/etc/httpd/vhosts ROOT_UID=0 |
1 2 3 4 5 |
if [ "$UID" -ne "$ROOT_UID" ] then echo "Must be root to run this script." exit $E_NOTROOT fi |
1 2 |
rename .inactive .conf $CONF_DIR/*.inactive # Move all vhosts to an active state service httpd restart # Force a reload of all config files now we're back online. |
1 |
exit # |
You must run each of these scripts as root as it requires that privilege to be able to restart the services. Personally I use:
1 |
su -c "./httpd_maint_down.sh" |
If anything’s unclear pop me a comment and I’ll try answer.
-Lewis
EDIT: I forgot to mention that /var/www/html/index.php is a file that I created that gives the user information that the site is down and shows them the time it went down (as a result of the touch operation above). Here’s that file for anyone interested.
1 2 3 4 5 6 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Maintenance mode</title> </head> |
1 |
<body> |
1 2 |
<?php $last_modified = filemtime('index.php'); ?> <p style="text-align:center">The site is currently offline for maintenance. Back soon!<br> |
1 2 |
Last Updated - <?php echo date("jS F Y - H:i:s", $last_modified); ?> </p> |
1 2 |
</body> </html> |