[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: RFC: new network config (was: Re: network configuration)

On Fri, Feb 05, 1999 at 07:05:07PM +1100, Craig Sanders wrote:
> > * IP aliases
> for ip aliases, the config file should have fields for more than just
> the IP address, and scripts which use it should ignore anything they
> don't need.
> [...deleted...]
> I am more than willing to write the code to do this. the above are my
> ideas, stuff that has proven useful to me in the past...ideas or sample
> code from other people who need similar functionality are very welcome.

here's a first cut at the apache part of the job. only took about half
an hour (but i cheated...i've done very similar stuff before :-). it
works. comments would be appreciated.

it uses md5sum to detect whether the individual apache httpd.conf file
fragments have been edited by the system admin or not.

first, the config file:

---cut here---/etc/virtuals/virtual-hosts.conf---cut here---
#IP:domain:username:flags,...                                      ,php,mod_perl,analog
---cut here---/etc/virtuals/virtual-hosts.conf---cut here---

next, the make-apache-conf.pl script. for now i have this in
/etc/virtuals. it really belongs in /usr/sbin

---cut here---
#! /usr/bin/perl

# this script makes individual httpd.conf fragments for each virtual host
# the fragments will be concatenated together with httpd.conf using
# a makefile.

# Copyright Craig Sanders, 1999.  
# This script is licensed under the terms of the GNU GPL.

# Author: Craig Sanders <cas@taz.net.au>
# $Id: make-apache-conf.pl,v 1.1 1999/02/05 10:26:11 root Exp root $
# Revision History:
#   1999-02-05 - first version

# - adduser if user doesn't exist (maybe - could be beyond scope of this prog)
# - create and chown/chmod public_html, www_logs and cgi-bin/
#   directories if they don't already exist.
# - add support for other apache features: php3, and maybe others.

$confdir='/etc/virtuals' ;
$virtuals="$confdir/virtual-hosts.conf" ;

open(VIRT,"<$virtuals") || die "couldn't open $virtuals: $!\n" ;

while (<VIRT>) {

	# skip blank lines and comments
	next if (/#|^\s*$/) ;

	($IP,$domain,$username,$flags) = split /:/ ;
	foreach (split(",", $flags)) { $flag{$_} = 1 } ;

	$outfile = "$confdir/apache/$domain" ;
	open(OUT, ">$outfile.new") || die "couldn't open $outfile.new for write: $!\n" ;

	# virtual host definition
	print OUT <<__EOF__;
<VirtualHost $domain>
	ServerName $domain
	ServerAdmin $username@$domain
	DocumentRoot /home/$username/public_html
	User $username

	# does the domain have it's own custom cgi directory or do
	# they use the standard /usr/lib/cgi-bin ?
	if ($flag{cgi}) {
		print OUT <<__EOF__;

	ScriptAlias /cgi-bin/shared/ /usr/lib/cgi-bin/
	ScriptAlias /cgi-bin/ /home/$username/cgi-bin/
	} else {
		print OUT <<__EOF__;

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	} ;

	# does this domain have mod_perl support?
	if ($flag{mod_perl}) {
		print OUT <<__EOF__;

	Alias /perl/ /home/$username/mod_perl/
	<Location /perl>
		SetHandler perl-script
		PerlHandler Apache::Registry
		Options ExecCGI
	} ;

	# log files
	print OUT <<__EOF__;
	ErrorLog /home/foobar/www_logs/error.log
	CustomLog /home/foobar/www_logs/access.log combined


	close(OUT) ;

	# if the .conf file already exists, be careful about overwriting it!
	if ( -e "$outfile.conf" ) {
		# if file hasn't been edited by sysadmin, then replace it.
		# use md5sum to detect any edits.
		# otherwise leave the .new file there to make it
		# easier for the system admin to manually merge in any
		# new features of this script.

		$md5new=`md5sum $outfile.conf` ;
		$md5old=`cat $outfile.md5` ;
		if ($md5new eq $md5old) {
			rename("$outfile.new","$outfile.conf") ;
			system("md5sum $outfile.conf >$outfile.md5") ;
		} ;
	} else {
		# rename the new config file and generate an md5sum
		rename("$outfile.new","$outfile.conf") ;
		system("md5sum $outfile.conf >$outfile.md5") ;
	} ;
} ;

close(VIRT) ;
---cut here---

i haven't yet written the Makefile to tie it all together, but i expect
it will look something like this:

---cut here---
#! /usr/bin/make -f

/etc/apache/httpd.conf: /etc/apache/httpd.conf.head /etc/virtuals/apache/*.conf
	cat /etc/apache/httpd.conf.head /etc/virtuals/apache/*.conf >/etc/apache/httpd.conf
---cut here---

the tricky part will be coping with changes in httpd.conf. contrary to
what the Makefile above, that is probably best done by putting some
comment tag in httpd.conf and catting the file up to that point, then
start catting all the *.conf files onto the end of it.

that should work better than messing around with a httpd.conf.head file.

craig sanders

Reply to: