Skip to content

Instantly share code, notes, and snippets.

@DrizzlyOwl
Last active January 17, 2022 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrizzlyOwl/19c93dc5868c8e484226e2b5fdf57a8e to your computer and use it in GitHub Desktop.
Save DrizzlyOwl/19c93dc5868c8e484226e2b5fdf57a8e to your computer and use it in GitHub Desktop.
#! /bin/bash
#
# A quick bash script to make it easier for non tech-savvy folks
# to create a vhost conf file and enable it for apache2.
#
# @author Ash Davies <a.davies@mixd.co.uk>
# @version 1.1.0
#
asksure() {
echo -n "Are you sure (Y/N)? "
while read -r -n 1 -s answer; do
if [[ $answer = [YyNn] ]]; then
[[ $answer = [Yy] ]] && retval=0
[[ $answer = [Nn] ]] && retval=1
break
fi
done
echo # just a final linefeed, optics...
return $retval
}
if [ $USER == 'www-data' ]; then
echo "Sorry! This script needs to be run as an Administrator"
exit 0
else
echo
echo "This script will scaffold a vhost for you!"
echo
echo "Please enter the domain name of the site you wish to deploy"
read domain
CWD=/etc/apache2/sites-available/
DEF=sample-vhost.conf.do-not-enable
TEMP=$HOME
FILE="$TEMP/$domain.conf"
echo
echo "You've chosen '$domain'."
echo
if asksure; then
echo
echo "OK! Scaffolding a vhost for '$domain' now..."
# Copy the default file
echo
echo "Creating '$domain.conf' in your home directory '$TEMP'"
sudo cp $CWD$DEF $FILE
# Search & Replace
echo
echo "Magically replacing all default paths with '$domain' so you don't have to..."
echo
sed -i -- "s/%default%/${domain}/g" $FILE
# Laravel check
if [ "$1" = "--laravel" ]; then
sed -i -- "s/current/current\/public/g" $FILE
fi
# Move the conf to vhost folder
echo "Moving '$FILE' to '$CWD$domain.conf'..."
sudo mv $FILE $CWD$domain.conf
# Activate the new vhost
echo
echo "Activating '$domain.conf'..."
sudo a2ensite $domain.conf
# Creating an awstats profile
if test -f "/etc/awstats/awstats.conf"; then
echo
if test -f "/root/awstats.done"; then
# awstats already setup
echo "Skipping initial awstats setup"
else
echo "Ensuring CGI module is enabled..."
sudo a2enmod cgi
echo
echo "Setting up awstats..."
sudo mv /etc/cron.d/awstats /root
sudo rm /var/lib/awstats/*
sudo chgrp www-data /var/log/apache2 /var/log/apache2/*log /var/log/apache2/access.log
sudo chmod 755 /var/log/apache2
sudo chmod 644 /var/log/apache2/*
sed -i -- "s|create 640 root adm|create 640 root adm www-data|g" "/etc/logrotate.d/apache2"
sudo mv /root/awstats /etc/cron.d
sudo touch /root/awstats.done
fi
echo "Creating 'awstats.$domain.conf'..."
sudo cp "/etc/awstats/awstats.conf" "/etc/awstats/awstats.$domain.conf"
sed -i -- "s|LogFile=\"/var/log/apache2/access.log\"|LogFile=\"/var/log/apache2/$domain-access.log\"|g" "/etc/awstats/awstats.$domain.conf"
sed -i -- "s|LogFormat=4|LogFormat=\"%host - - %time1 %methodurl %code - %refererquot %uaquot\"|g" "/etc/awstats/awstats.$domain.conf"
sed -i -- "s|SiteDomain=\"\"|SiteDomain=\"$domain\"|g" "/etc/awstats/awstats.$domain.conf"
sed -i -- "s|HostAliases=\"localhost 127.0.0.1\"|HostAliases=\"$domain www.$domain\"|g" "/etc/awstats/awstats.$domain.conf"
sed -i -- "s|AllowFullYearView=2|AllowFullYearView=3|g" "/etc/awstats/awstats.$domain.conf"
sed -i -- "s|AllowAccessFromWebToFollowingIPAddresses=\"\"|AllowAccessFromWebToFollowingIPAddresses=\"167.172.62.38\"|g" "/etc/awstats/awstats.$domain.conf"
echo
echo "Waiting for awstats to catch-up. This might take a while if $domain already exists on this server..."
echo
perl /usr/lib/cgi-bin/awstats.pl -update -config=$domain
echo
fi
# Reload apache
echo
echo "Reloading apache2 for changes to take effect..."
sudo service apache2 reload
sudo mkdir -p "/var/www/vhosts/$domain/httpdocs"
sudo chown mixdremote:mixdremote "/var/www/vhosts/$domain/" -R
echo
echo "Done! The vhost for '$domain' is now setup."
echo "Your deployment path has been created: /var/www/vhosts/$domain/httpdocs"
exit 1
else
exit 0
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment