Skip to content

Instantly share code, notes, and snippets.

@tombeynon
Last active August 25, 2023 06:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tombeynon/5433461 to your computer and use it in GitHub Desktop.
Save tombeynon/5433461 to your computer and use it in GitHub Desktop.
Programmatically create Apache VHost
#!/bin/bash
# /usr/local/bin/createSite.sh -d domain -t com -u user -p password -r git@github.com:user/repo.git
# TODO: SCP could be done in reverse
domain=''
tld=''
username=''
password=''
repo=''
function make_vhost(){
cat <<- _EOF_
# $domain.$1
<VirtualHost *:80>
ServerAdmin digital@bitecp.com
ServerName $domain.$1
# Directory Root.
DocumentRoot /var/www/vhosts/$domain.$tld/httpdocs
# Logfiles
ErrorLog /var/www/vhosts/$domain.$tld/logs/error.log
CustomLog /var/www/vhosts/$domain.$tld/logs/access.log combined
Include "/var/www/vhosts/$domain.$tld/conf/vhost.conf"
</VirtualHost>
# template.$domain.$1
<VirtualHost *:80>
ServerAdmin digital@bitecp.com
ServerName $domain.$1
ServerAlias template.$domain.$1
# Directory Root.
DocumentRoot /var/www/vhosts/$domain.$tld/template
# Logfiles
ErrorLog /var/www/vhosts/$domain.$tld/logs/error.log
CustomLog /var/www/vhosts/$domain.$tld/logs/access.log combined
</VirtualHost>
_EOF_
}
while getopts "d:t:u:p:r:" opt; do
case $opt in
d)
domain=$OPTARG
;;
t)
tld=$OPTARG
;;
u)
username=$OPTARG
;;
p)
password=$OPTARG
;;
r)
repo=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ -n "$domain" -a -n "$tld" -a -n "$username" -a -n "$password" -a -n "$repo" ]; then
echo -e "\n======== Initialising Mercury ========"
ssh root@mercury.bitecp.com <<- _EOF_
# Create subscription
/usr/local/psa/bin/subscription --create $domain.$tld -owner admin -service-plan 'Default Domain' -ip 184.106.119.47 -login $username -passwd '$password'
# Create vhost.conf (not included in VC but worth it for consistency)
touch /var/www/vhosts/$domain.$tld/conf/vhost.conf
# Add template folder and holding file
mkdir /var/www/vhosts/$domain.$tld/template/
touch /var/www/vhosts/$domain.$tld/template/index.html
_EOF_
echo "Created website files"
# Site is now initialised on Live, send over gitignore
scp -r /usr/resources/templates/git/.gitignore_base root@mercury.bitecp.com:/var/www/vhosts/$domain.$tld/.gitignore
echo "Base gitignore transferred"
# SSH back in and initialise git
ssh -t root@mercury.bitecp.com <<- _EOF_
git init /var/www/vhosts/$domain.$tld/
cd /var/www/vhosts/$domain.$tld/
git add .
git commit -am "Initial import"
git remote add -t master -m master origin $repo
git push origin master
_EOF_
echo "Git initialised.. all done!"
echo -e "\n======== Initialising Mercury-Stage ========"
make_vhost bitestage.com > /tmp/$domain.$tld
scp /tmp/$domain.$tld root@mercury-stage.bitecp.com:/etc/apache2/sites-available/
rm -f /tmp/$domain.$tld
ssh root@mercury-stage.bitecp.com <<- _EOF_
# Clone github repository
cd /var/www/vhosts/
su bite -c "git clone $repo $domain.$tld"
cd $domain.$tld
su bite -c "git checkout -b staging; git branch -D master; git push origin staging; git branch --set-upstream staging origin/staging;"
echo "Cloned GitHub repository"
# Make logs
mkdir -p /var/www/vhosts/$domain.$tld/logs/
touch /var/www/vhosts/$domain.$tld/logs/access.log
touch /var/www/vhosts/$domain.$tld/logs/error.log
echo "Created logs"
# Make vhost.conf
if [ ! -d "/var/www/vhosts/$domain.$tld/conf/" ]; then
mkdir /var/www/vhosts/$domain.$tld/conf/
touch /var/www/vhosts/$domain.$tld/conf/vhost.conf
echo "Created vhost.conf"
fi
# Make template directory
if [ ! -d "/var/www/vhosts/$domain.$tld/template/" ]; then
mkdir /var/www/vhosts/$domain.$tld/template/
echo "Created template directory"
fi
chown -R bite:bite /var/www/vhosts/$domain.$tld/
echo "Set ownership to bite"
a2ensite $domain.$tld
echo "Enabled Vhost"
service apache2 reload
echo "Reloaded apache config.. all done!!"
_EOF_
echo -e "\n======== Initialising Terra ========"
make_vhost dev > /etc/apache2/sites-available/$domain.$tld
# Clone repository
cd /var/www/vhosts/
su bite -c "git clone $repo $domain.$tld"
cd $domain.$tld
su bite -c "git checkout -b development; git branch -D master; git push origin development; git branch --set-upstream development origin/development;"
echo "Cloned GitHub repository"
# Make logs
mkdir -p /var/www/vhosts/$domain.$tld/logs/
touch /var/www/vhosts/$domain.$tld/logs/access.log
touch /var/www/vhosts/$domain.$tld/logs/error.log
echo "Created logs"
# Make vhost.conf
if [ ! -d "/var/www/vhosts/$domain.$tld/conf/" ]; then
mkdir /var/www/vhosts/$domain.$tld/conf/
touch /var/www/vhosts/$domain.$tld/conf/vhost.conf
echo "Created vhost.conf"
fi
# Make template directory
if [ ! -d "/var/www/vhosts/$domain.$tld/template/" ]; then
mkdir /var/www/vhosts/$domain.$tld/template/
echo "Created template directory"
fi
chown -R bite:bite /var/www/vhosts/$domain.$tld/
echo "Set ownership to bite"
a2ensite $domain.$tld
echo "Enabled Vhost.. all done!!"
service apache2 reload
echo "Reloaded apache config.. all done!!"
echo -e "\n!======== Website created! ========!"
else
echo "Missing required options (-d -t -u -p -r)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment