Very often, many users of VDS / Servers buy various control panels, the essence of which is the automation of routine work like adding virtual hosts of Apache.
But which of the panels is able to produce a massive addition of virtual hosts? Yes, like no. A script of a couple of dozen lines - very able. We modify the
previous script a bit and make it more “portable”.
Let the bulk add will occur using a file whose name is the IP address, and the content is a list of domains. Yes, such problems are solved through a virtual-host or rewrite mode, but in this case the example is purely academic.
#!/bin/sh [ -z $1 ] && (echo ; exit 1) HTTPD=/etc/httpd/conf.d BASE=/home USER=username for i in `cat $1`; do VHOST= mkdir -p $VHOST chown -R $USER:$USER $BASE/$USER/$i cat << EOF > $HTTPD/$i.conf <VirtualHost $1:80> ServerName $i ServerAlias www.$i DocumentRoot $VHOST ErrorLog /var/log/httpd/$i.error_log CustomLog /var/log/httpd/$i.log combined </VirtualHost> EOF done
After running the script, do not forget to reload the ApacheImmediately attracts the attention of the fifth row with a cycle. The argument list is the result of the actions of the cat command, which reads the contents of the file, since backquotes execute the commands enclosed in them and return the result.
Next in the loop, actions familiar from the previous script are executed, only here they are executed in a loop.
Too easy? Well, let's try modifying the script to add domains to bind. Let all domains be registered on ns1.mydomain.com and ns2.mydomain.com as NS servers, and mail will be on mx1.mydomain.com and mx2.mydomain.com, respectively. And www prescribe alias. So:
#!/bin/sh [ -z $1 ] && (echo ; exit 1) CONF=/etc/named.conf BASE=/var/named USER=bind for i in `cat $1`; do ZONE= cat << EOF > $ZONE $TTL 14400 @ 86400 IN SOA ns1.mydomain.com. root.$i. ( 2009112801 86400 7200 3600000 86400 ) $i. 86400 IN NS ns1.mydomain.com. $i. 86400 IN NS ns2.mydomain.com. $i. IN MX 10 mx1.mydomain.com. $i. IN MX 20 mx2.mydomain.com $i. IN A $1 www IN CNAME $i. EOF chown $USER:$USER $ZONE echo $i\$ZONE\ >> $CONF done rndc reload
')
I pay attention that in the line of adding lists of zones to the bind config, quotes are used and they are escaped with a backslash.
So, we have two scripts that provide 80% of the functionality provided by commercial control panels. At the same time, they are free, they do not overwhelm resources, and they are easy to change to fit your tasks.