Apache Vhost Creation Script (Gentoo)
November 13th, 2008
Well I decided to post more of the stuff I write even if I think it’s crappy….
So even though there’s probably a dozen like this and it could be a lot nicer, here’s the bash script I wrote to create new subdomains on my web server. Customize to fit your needs.
My apologies for the line wrapping. Fixed
Code below the fold
#!/bin/bash #Copyright (c) 2008 Brian Ortiz "Ortzinator" # #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal #in the Software without restriction, including without limitation the rights #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #copies of the Software, and to permit persons to whom the Software is #furnished to do so, subject to the following conditions: # #The above copyright notice and this permission notice shall be included in #all copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. if [ $# -ne 1 ] then echo "Exactly one argument required" exit 1 fi dir="/var/www/$1" if [ -d "$dir" ] then echo "ERROR: directory '$dir' already exists!" exit 1 else echo "Creating directory '$dir'" mkdir $dir || { echo "Could not create directory '$dir'"; exit 1; } echo "Creating directory '$dir/htdocs'" mkdir $dir/htdocs || { echo "Could not create directory '$dir/htdocs'"; exit 1; } echo "Setting permissions..." chown apache:htdocs $dir || { echo "Error setting permissions for '$dir'"; exit 1; } chown apache:htdocs $dir/htdocs || { echo "Error setting permissions for '$dir/htdocs'"; exit 1; } echo " Placeholder lol " > $dir/htdocs/index.html chown brian:brian $dir/htdocs/index.html || { echo "Error setting permissions for '$dir/htdocs/index.html'"; exit 1; } chmod -R 775 $dir echo " ServerName $1.ortz.org DocumentRoot /var/www/$1/htdocs Allow from all AllowOverride all Order allow,deny " >> /etc/apache2/vhosts.d/00_default_vhost.conf || { echo "Could not add vhost to apache config!"; exit 1; } echo "Restarting apache....." /etc/init.d/apache2 restart || { echo "Could not restart apache!"; exit 1; } echo "All done!" exit 0 fi
EDIT: Check out the improved version Blokkie posted in the comments.






December 2nd, 2008 at 2:46 pm
“<” and “>” should replace whith “”.
December 2nd, 2008 at 2:48 pm
I mean synbols “gross then” and “less then”
December 2nd, 2008 at 4:21 pm
Oh, I didn’t notice Wordpress encoded them. Thanks Driusha! I’ll try to fix that.
January 12th, 2009 at 12:56 pm
copy and paste FAIL!
January 12th, 2009 at 12:58 pm
More like Wordpress fail.
July 29th, 2009 at 8:02 am
I’ve moved some stuff arround coz I did not like adding everything to the default_vhost.conf
I also added a custom log file and added some allow permission stuff
here it go’s :
#!/bin/bash
#Copyright (c) 2008 Brian Ortiz "Ortzinator"
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
if [ $# -ne 1 ]
then
echo Exactly one argument required
exit 1
fi
dir=/var/www/$1
if [ -d $dir ]
then
echo ERROR: directory $dir already exists!
exit 1
else
echo Creating directory $dir
mkdir $dir || { echo Could not create directory $dir; exit 1; }
echo Creating directory $dir/htdocs
mkdir $dir/htdocs || { echo Could not create directory $dir/htdocs; exit 1; }
echo Setting permissions
chown apache: $dir || { echo Error setting permissions for $dir; exit 1; }
chown apache: $dir/htdocs || { echo Error setting permissions for $dir/htdocs; exit 1; }
echo "
Placeholder lol
" > $dir/htdocs/index.html
chown blokkie: $dir/htdocs/index.html || { echo Error setting permissions for $dir/htdocs/index.html; exit 1; }
chmod -R 775 $dir
echo "
ServerName $1
DocumentRoot /var/www/$1/htdocs
Allow from all
AllowOverride all
Order allow,deny
CustomLog /var/log/apache2/sites-$1.log common
" > /etc/apache2/vhosts.d/$1.conf || { echo Could not add vhost to apache config!; exit 1; }
echo Restarting apache
/etc/init.d/apache2 restart || { echo Could not restart apache!; exit 1; }
echo All done!
exit 0
fi
July 29th, 2009 at 8:06 am
PS: I forgot ,
chown apache: $dir
the ":" automagicly picks the default group of the user.
An actual restart of apache is not needed coz you only added something to the config . So apache2ctl graceful is sufficient I think .
This also prevents users of other sites on thesame server to not get disconnected .
maybe before the restart / gracefull another check can be added like :
/usr/sbin/apache2 -S | grep OK
Just my 5 cents
Cheerio’s
July 29th, 2009 at 3:27 pm
Cool, thanks!
htdocs is the group I use for restricting permissions to web stuff, but I didn’t know you could do that.