Simulating Debian’s Apache Configuration Management on CentOS

I am a big fan of how debian/debian-based distro maintain apache’s configuration and specially virtualhosts and mods.

If you don’t know its like this:

  • you have 2 directories under /etc/apache2 called sites-available and sites-enabled.
  • Under sites-available you can define new hosts but they aren’t automatically loaded into apache. They just sit there.
  • When you are sure that you have defined it the right way, either you create a symlink in sites-enabled to your vhosts or run a2ensite your-vhost. And in a similar way to disable it you do a2dissite your-vhost
  • Mods are managed exactly like this except that their dirs are mods-available and mods-enabled, thus the command is a2enmod and a2dismod.

It really easy to manage , track and temporarily enabled/disable your stuff this way. Like the biggest benefit i get from it is that i always have two vhosts against one domain, one is like domain-com.vhost and one is domain-com-down.vhost. The second one is just a symlink to a file where i keep adding commented ServerAliases and it shows a nice page to visitors that “we are sorry….blah blah blah”. Now whenever i plan to down a site, i open the down.vhost and uncomment the relevant lines, run a2dissite domain-com.vhost and a2ensite domain-com-down.vhost, there, i have a nice apology page.

Why do i feel like this post is not what its subject says? Don’t worry you will find what subject address down the road, keep reading an interesting story (i think it is…).

So after i shifted my job, i had to migrate myself to CentOS and i try. One of the things that i missed was that Mod and vHost management. No worries, here is what i did:
-/
–/etc
—/etc/httpd
—-/etc/httpd/vhosts.d
—–/etc/httpd/vhosts.d/available
—–/etc/httpd/vhosts.d/enabled
–/root/Scripts/apache/a2ensite
–/root/Scripts/apache/a2dissite
–/root/Scripts/apache/a2enmod
–/root/Scripts/apache/a2dismod

I create vhosts.d and then available and enabled dirs inside that in /etc/httpd. Add “Include /etc/httpd/vhosts.d/enabled/*.vhost” in /etc/httpd/conf.d/vhosts.conf. Now i define couple of vhosts in /etc/httpd/vhosts.d/available but exclude the “.vhost” suffix intentionally. Create two scripts as a2ensite and a2dissite. What a2ensite does is that it create a symlink called /etc/httpd/vhosts.d/enabled/domain-com.vhost for /etc/httpd/vhosts.d/available/domain-com. As you noticed that it also adds the “.vhost” part so that now the vhost is loaded when apache conf gets reloaded/restart. And a2dissite just removes the symlink from vhosts.d/enabled/.

Its almost the same story for a2enmod and a2dismod but there i exclude “.mod” suffix which i add in a2enmod

And now i enjoy the same configuration management as in debian. Loving it, took long but totally worth it, so much more flexible. Give me couple of days so that i get some free time and improve the script and i will upload them here and paste a link for you. Enjoy Open Source. :)

PS:
I could have copied these scripts from a debian box from /usr/bin but then where would have been the fun part? plus it a big different setup as you see and i did that intentionally.

Tags: , , , , , , , , ,

One Comment

  1. Nate P says:

    Hey Shoaibi,Can you upload or share these scripts? I recently got a centos 5.5 linode account and would like to use your scripts on there for at least managing the vhosts. if you could share your httpd.conf as well that would be k-fab!Thanks,Nate

Leave a Comment