Skip to content

Instantly share code, notes, and snippets.

@davit
Last active August 29, 2015 14:21
Show Gist options
  • Save davit/b5489ad4ea3a5da3df7b to your computer and use it in GitHub Desktop.
Save davit/b5489ad4ea3a5da3df7b to your computer and use it in GitHub Desktop.
Creates a virtual host for a given domain
#!/bin/bash
domain=$1
serverRoot=$2
createRootFolder=$3
vhostPath=/etc/apache2/sites-available/
vhostFile=${domain}.conf
function print_usage {
echo "USAGE: create-vhosts [some-domain.com] [path/to/server/root/folder] [OPTIONAL: create the server folder]"
exit 0;
}
if [[ $# -eq 0 ]] ; then
print_usage
fi
if [ -z "${createRootFolder}" ]; then
:
else
documentRoot=${serverRoot}/${domain}/public_html
mkdir -p ${documentRoot}
touch ${documentRoot}/index.html
echo "<html> <body> <h1> Welcome to: ${domain} </h2> </body> </html>" > ${documentRoot}/index.html
fi
echo "<VirtualHost *:80>
ServerAdmin admin@${domain}
DocumentRoot \"${serverRoot}/${domain}/public_html\"
ServerName ${domain}
ServerAlias www.${domain}
ErrorLog \"/var/log/httpd/${domain}-error_log\"
CustomLog \"/var/log/httpd/${domain}-access_log\" common
</VirtualHost>" > ${vhostPath}/${vhostFile}
echo "Include ${vhostPath}/${vhostFile}" >> /etc/httpd/conf/httpd.conf
sed -i "s/#End of file/127.0.0.1 ${domain}\n#End of file/" /etc/hosts
/etc/init.d/apache2 restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment