#!/bin/bash
#
# Script to add virtual sites
# Usage: addsite.sh sub.sitename.ext username password
# Check /etc/apache2/listvirts to make sure the last groupnumber makes sense!
#
# Robin Edgar 31/8/4
#
# v6.1 16/8/18
# letsencrypt functionality is changed and set to a note due to conflicts with edgarbv.com r
oot cert
#
# v6.0 8/3/17
# added letsencrypt / certbot functionality
#
# v5.1 12/1/16
# changed filename of apache conf file to add .conf at the end so a2ensite / a2dissite can f
ind it
#
# v5 3/2/14
# Changed to new edgarbv server
#
# v4.1 16/3/9
# Apache2 compatibility from Tripwraith
#
# v3.0 1/3/6
# Changed to work on Tripghost, added weblog to statistics processing
#
# v2.2 25/11/4
# Tidied the variables to $SITENAME, $USER, $PASS
#
# v2.1 20/10/4
# Added statistics to http://tripghost.tripany.com/sitestats/ for all sites.
#
# v2 7/10/4
# Added user addition
#################################################### Set Variables
THISDOM=`echo "$1" | awk -F . '{ print $2}'`
THISEXT=`echo "$1" | awk -F . '{ print $3}'`
# Get the group
LASTGROUP=`tail -c4 /etc/apache2/listvirts`
# Calculate the next group number
NEXTGROUPNR=`bc << EOF
$LASTGROUP + 1
EOF
`
NEXTGROUPNAME=site$NEXTGROUPNR
# Make it all a little more readable
SITENAME=$1
USER=$2
PASS=$3
######################################################### Here we start the processing
echo "Creating new Virtual Site"
######################################################### Apache
# Add to the virtual host file
echo "<VirtualHost *:80>" >> /etc/apache2/sites-available/$SITENAME.conf
echo " ServerName $SITENAME" >> /etc/apache2/sites-available/$SITENAME.conf
echo " DocumentRoot /var/www/$SITENAME/site" >> /etc/apache2/sites-available/$SITENAME.conf
echo " ServerAdmin robin@edgarbv.com" >> /etc/apache2/sites-available/$SITENAME.conf
echo " ServerAlias $THISDOM.$THISEXT" >> /etc/apache2/sites-available/$SITENAME.conf
echo " <Directory /var/www/$SITENAME/site/>" >> /etc/apache2/sites-available/$SITENAME.conf
echo " Options FollowSymLinks" >> /etc/apache2/sites-available/$SITENAME.conf
echo " AllowOverride Options Authconfig" >> /etc/apache2/sites-available/$SITENAME.conf
echo " Order allow,deny" >> /etc/apache2/sites-available/$SITENAME.conf
echo " allow from all" >> /etc/apache2/sites-available/$SITENAME.conf
echo " </Directory>" >> /etc/apache2/sites-available/$SITENAME.conf
echo " ErrorLog /var/log/apache2/error.log" >> /etc/apache2/sites-available/$SITENAME.conf
echo " LogLevel warn" >> /etc/apache2/sites-available/$SITENAME.conf
echo " CustomLog /var/log/apache2/access.log combined" >> /etc/apache2/sites-available/$SITENAME.conf
echo "</VirtualHost>" >> /etc/apache2/sites-available/$SITENAME.conf
a2ensite $SITENAME
echo "1. added site $SITENAME to /etc/apache2/sites-available/$SITENAME.conf and linked to /etc/apache2/sites-enabled/$SITENAME"
# Add the group
addgroup $NEXTGROUPNAME
# Then create the directory
mkdir /var/www/$SITENAME/site -p
mkdir /var/www/$SITENAME/logs -p
mkdir /var/www/$SITENAME/users -p
chgrp $NEXTGROUPNAME /var/www/$SITENAME/site
chgrp $NEXTGROUPNAME /var/www/$SITENAME/logs
chgrp $NEXTGROUPNAME /var/www/$SITENAME/users
echo "2. group $NEXTGROUPNAME added and directories in /var/www/$SITENAME added and groups chgrpd"
# Add name to domainlist for processing by apachesplitlog script
echo "$SITENAME - $NEXTGROUPNAME" >> /etc/apache2/listvirts
echo "3. added sitename and groupname to /etc/apache2/listvirts"
################################################### Logrotate
# Add to Logfile rotation directory
#touch /etc/logrotate.d/site.$SITENAME
#echo "/var/www/$SITENAME/logs/$SITENAME.log {" > /etc/logrotate.d/site.$SITENAME
#echo " missingok" >> /etc/logrotate.d/site.$SITENAME
#echo " compress" >> /etc/logrotate.d/site.$SITENAME
#echo " size 5M" >> /etc/logrotate.d/site.$SITENAME
#echo " rotate 9" >> /etc/logrotate.d/site.$SITENAME
#echo "}" >> /etc/logrotate.d/site.$SITENAME
#echo "4. file /etc/logrotate.d/site.$SITENAME added"
#################################################### Statistics
#echo "creating statistics"
# add the statistics directory
#mkdir /var/www/tripghost.tripany.com/site/sitestats/$SITENAME
#chown razor:admin /var/www/tripghost.tripany.com/site/sitestats/$SITENAME
#echo "5. created and chmownd central stats dir"
# add the index file
#echo "<?" > /var/www/tripghost.tripany.com/site/sitestats/$SITENAME/index.php
#echo "Header('Location: http://tripghost.tripany.com/sitestats/$SITENAME/awstats.$SITENAME.html')" >> /var/www/tripghost.tripany.com/site/sitestats/$SITENAME/index.php
#echo "?>" >> /var/www/tripghost.tripany.com/site/sitestats/$SITENAME/index.php
#echo "6. created index file to statsdir"
# Add to AWSTATS Logfile analysis script
THISDOM=`echo "$1" | awk -F . '{ print $2}'`
THISEXT=`echo "$1" | awk -F . '{ print $3}'`
#sed -e "s/thislogfile/\/home\/sites\/$1\/logs\/$1.log/g" -e "s/thissitedomain/$1/g" -e "s/thisdomname/$THISDOM/g" -e "s/thisdomext/$THISEXT/g" /etc/awstats/model.conf > /etc/awstats/awstats.$1.conf
sed -e "s/thisdomname/$THISDOM/g" -e "s/thisdomext/$THISEXT/g" /etc/awstats/model.conf > /etc/awstats/awstats.$1.conf
echo "7. created awstats conffile"
# All this for the weblog stuff has been deleted
# Add to the statistics runscript
#echo "echo Statistics for $SITENAME >> /var/log/statistics" >> /opt/triphost/sitestatistics.sh
#echo "/usr/share/doc/awstats/examples/awstats_buildstaticpages.pl -config=$SITENAME -update -awstatsprog=/usr/lib/cgi-bin/awstats.pl -dir=/var/www/tripghost.tripany.com/site/sitestats/$SITENAME >> /var/log/statistics" >> /opt/triphost/sitestatistics.sh
#echo "8. added to statistics runscript"
# Add to the WEBLOG Logfile analysis script
#sed -e "s/sitename/$SITENAME/g" /opt/weblog/model.conf > /opt/weblog/etc/$SITENAME.conf
#echo "9. created weblog conffile"
# Add to the statistics runscript
#echo "perl /opt/weblog/etc/$SITENAME.conf >> /var/log/statistics" >> /opt/triphost/sitestatistics.sh
#echo "10. added to statistics runscript"
# add .htaccess file for Statistics
#echo "AuthType Basic" > /var/www/tripghost.tripany.com/site/sitestats/$SITENAME/.htaccess
#echo "AuthName \"$SITENAME Statistieken\"" >> /var/www/tripghost.tripany.com/site/sitestats/$SITENAME/.htaccess
#echo "AuthUserFile /var/www/tripghost.tripany.com/site/.htpasswd" >> /var/www/tripghost.tripany.com/site/sitestats/$SITENAME/.htaccess
#echo "Require user $USER webstats" >> /var/www/tripghost.tripany.com/site/sitestats/$SITENAME/.htaccess
#echo "11. added .htaccess protection to statistics directory"
# add user and password to .htpasswd files
#htpasswd -b /var/www/tripghost.tripany.com/site/.htpasswd $USER $PASS
#htpasswd -b /usr/lib/cgi-bin/.htpasswd $USER $PASS
#chown razor:admin /var/www/tripghost.tripany.com/site/sitestats/$SITENAME -R
#echo "12. added user to .htpasswd file $SITENAME"
#echo "statistics directories and files added"
################################################# Add the user and group to the system
# Add the user to the system
/usr/sbin/useradd -s /bin/false -g $NEXTGROUPNAME -m -d /var/www/$SITENAME/users/$USER $USER
echo "13. User $USER Added"
# Change site directory permissions
chown $USER:$NEXTGROUPNAME /var/www/$SITENAME/site -R
echo "14. Site Directory permissions changed"
# create the input file for chpasswd
echo "$USER:$PASS" > userpasslist
echo "15. user added to userpasslist"
# This should theoretically set the password for the user in userpasslist DOESN'T WORK YET
cat ./userpasslist | /usr/sbin/chpasswd
echo "16. password set - not sure if working yet! check this by hand!"
# reload apache
/etc/init.d/apache2 reload
echo "17. reloaded apache"
# Empty the username and password from the userpasslist
#cat /dev/null > userpasslist
#echo "18. userpasslist file emptied"
# TODO Still have to 'admin' and 'rsync' to the $NEXTGROUPNAME group!
# Throw some important information on the screen
echo ""
echo "REMEMBER:"
echo "add the users admin and rsync to the $NEXTGROUPNAME group in /etc/group"
echo "This script doesn't add the domain to /etc/postfix/virtual and /etc/postfix/virtual-domains"
echo "Add other domains to /etc/awstats/awstats.sitename and /etc/apache2/sites-enabled/sitename"
echo "If this is a subdomain.edgarbv.com, edit /etc/apache/sites-enabled/subdomain.edgarbv.com.conf and -ssl-le.conf and remove the ServerAlias edgarbv.com to prevent conflicts when asking for a certificate and when serving the website pages."
echo "Generate the certificates for Let's Encrypt using the following command:"
echo "certbot -a webroot -i apache -d www.domain.ext,domain.ext,other.domain.ext -w /var/www/www.domain.ext/site/"
exit