3

Following another question I just solved, I wonder: is there anything we can do within Apache to avoid repeating lines of configuration? What about outside apache? In my case it would be AWS EC2's RHEL4, but I trust any **nix* would have a similar solution. Maybe something like sed, maybe using .htaccess... Dunno. But this should be pre-processed, nothing at runtime or something: just like the conf files, once apache is loaded that is it.


Here I'll copy my past solution just to illustrate:

UseCanonicalNames off

NameVirtualHost *:8888
<VirtualHost *:8888>
  ServerName example1.com
  ServerAlias *.example1.com

  # below, stuff that will be repeated
  # redirect to HTTPS
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^login\.(.*)$
  RewriteRule ^(.*) https://%1/login$1 [L]
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$
  RewriteRule ^/login(.*) https://%1/login$1 [L]
</VirtualHost>

<VirtualHost *:8888>
  ServerAlias *
  VirtualDocumentRoot /var/www/html/%-3

  # below, stuff that need to be repeated
  # redirect to HTTPS
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^login\.(.*)$
  RewriteRule ^(.*) https://%1/login$1 [L]
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$
  RewriteRule ^/login(.*) https://%1/login$1 [L]
</VirtualHost>

And I'm sure there are many instances in which things like this redirection_to_HTTPS could have been a function / procedure / macro / template / include / etc.

1

4 Answers 4

2

Apache has an include directive. Something like

UseCanonicalNames off

NameVirtualHost *:8888
<VirtualHost *:8888>
  ServerName example1.com
  ServerAlias *.example1.com

  Include /etc/apache2/redirect_to_https.conf    
</VirtualHost>

<VirtualHost *:8888>
  ServerAlias *
  VirtualDocumentRoot /var/www/html/%-3

  Include /etc/apache2/redirect_to_https.conf    
</VirtualHost>

with /etc/apache2/redirect_to_https.conf being

  # below, stuff that will be repeated
  # redirect to HTTPS
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^login\.(.*)$
  RewriteRule ^(.*) https://%1/login$1 [L]
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$
  RewriteRule ^/login(.*) https://%1/login$1 [L]

The other answers are good for the more general problem of converting code into configuration files, though.

2

Well, thanks all for such great answers, but... Probably mod_macro would be the best to do the trick.

And its ideals are aligned with the benefits I was looking for:

  • smaller configuration files to maintain.
  • less bugs due to copy-pastes only partially updated.
  • better readability. for instance: Use AllowLocalAccess might be considered clearer than allow from 192.54.172.0/24 192.54.148.0/24 10.0.0.0/8
  • all this not at the price of perl or m4 programming.

Next time I have a chance I'll try all solutions, starting with this one, and add a template of what I've used... Unless someone is kind enough to do all that first! :)

3
  • 1
    +1: don't fear setting your answer as the correct one (green tick), when it's actually the case.
    – ceztko
    May 7, 2012 at 9:17
  • @ceztko I don't even remember why I accepted the other one or which solution I've actually used back 1 year ago... I never got properly paid by this project and didn't use apache since. But clearly I haven't tested this one, so I couldn't have said it worked as nicely as its description made me think it would. Anyways, thanks for the kind words! :-)
    – cregox
    May 7, 2012 at 13:58
  • I tested it myself: it's the correct solution. :)
    – ceztko
    May 9, 2012 at 17:13
1

I may get called "old school" just for posting this, but I think the classic solution for this problem is to use a macro processor like M4, which is a POSIX standard. There's a GNU implementation:

http://www.gnu.org/software/m4/

That page mentions autoconf as its most notable user, but I think most sysadmins come into M4 for the first time through the also classic sendmail.

You could, for example, mantain M4 versions of your config files and some Makefiles and then "compile" them into actual running configurations via a simple make.

3
  • for now, I'll love old school!
    – cregox
    May 20, 2011 at 23:36
  • 1
    Here's a link to a nice tutorial on M4: linuxjournal.com/article/5594?page=0,0 and a more comprehensive one here: mbreen.com/m4.html . The official m4 manual, like most GNU documentation, is almost useless as an introductory material - after scrolling through 10 pages of text you're at the section which conveniently informs you that "François Pinard took over maintenance of GNU m4 in 1992". You need to consume the next 15 screens of text to only be able to write and invoke a simple macro, but you'll know all its obscure invocation options. Talk about first things first... Jun 9, 2011 at 8:54
  • @Aleksander: thanks for the link. And the GNU docs bit is so true most of the time. Jun 9, 2011 at 14:41
0

Things like this can be done with management frameworks like Puppet or Capistrano. In these scripts are created to generate httpd.conf files (and associated include files) based on defined conditions. These sorts of routine "do this one thing X places" actions can be functionialized. I don't believe there are out-of-the-box methods for doing this kind of thing, but these frameworks are designed to allow you to make your own.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .