Skip to content

Instantly share code, notes, and snippets.

@nbrew
Last active December 22, 2015 20:39
Show Gist options
  • Save nbrew/6527860 to your computer and use it in GitHub Desktop.
Save nbrew/6527860 to your computer and use it in GitHub Desktop.
Custom vhosts setup on Mac OS X 10.8. * Stores vhost config files in /etc/apache2/extra/vhosts.d * Creates directories at ~/Sites/{domain} to house files to be served and log files * Appends an entry to your /etc/hosts file (if none exists)
#!/bin/bash
# Custom vhosts setup on Mac OS X 10.8.
#
# * Stores vhost config files in /etc/apache2/extra/vhosts.d
# * Creates directories at ~/Sites/{domain} to house files to be served and log files
# * Appends an entry to your /etc/hosts file (if none exists)
#
# See: https://gist.github.com/nbrew/6527860
#
# Installation
#
# mkdir ~/bin
# cd ~/bin
# wget https://gist.github.com/nbrew/6527860/raw/vhost_setup.sh
#
# Author: Nathan Hyde
#
#sed -i -e 's;#\(Include /private/etc/apache2/extra/httpd-vhosts.conf\);\1;' /etc/apache2/httpd.conf
if [ ! -d /etc/apache2/extra/vhosts.d ]; then
echo "Making the vhosts directory."
sudo mkdir /etc/apache2/extra/vhosts.d
fi
if [ ! -d ~/Sites ]; then
echo "Creating your Sites directory."
echo "Your virtual hosts will be placed in this directory."
mkdir ~/Sites
fi
if [ ! -f /etc/apache2/extra/httpd-vhost-config.conf ]; then
echo "Adding custom httpd-vhost-config.conf file for apache"
echo 'NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents/
</VirtualHost>
Include /etc/apache2/extra/vhosts.d/*.conf' | sudo tee /etc/apache2/extra/httpd-vhost-config.conf
fi
if [ ! -f ~/bin/ls_vhosts ]; then
echo "* Installing ls_vhosts script into ~/bin"
echo '#!/bin/bash
ls -l /etc/apache2/extra/vhosts.d/*.conf' >> ~/bin/ls_vhosts
chmod 755 ~/bin/ls_vhosts
fi
if [ ! -f ~/bin/new_vhost ]; then
echo "* Installing new_vhost script into ~/bin"
echo '#!/bin/bash
function new_vhost() {
if [ -z ${1} ]; then
echo
echo "USAGE: new_vhost <domain>"
exit
fi
domain="${1}"
user=$(whoami)
domain_root="/Users/${user}/Sites/${domain}"
if [ ! -d "${domain_root}" ]; then
echo "* Creating root directories in "
echo " ${domain_root}"
mkdir -p ${domain_root}/{public,log}
fi
if [ ! -f /etc/apache2/extra/vhosts.d/${domain}.conf ]; then
echo "* Creating the vhosts file in "
echo " /etc/apache2/extra/vhosts.d/${domain}.conf"
echo "<VirtualHost *:80>
ServerName ${domain}
ServerAlias www.${domain}
DocumentRoot \"${domain_root}/public\"
<Directory \"${domain_root}/public\">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
CustomLog \"${domain_root}/log/access_log\" common
ErrorLog \"${domain_root}/log/error_log\"
</VirtualHost>" | sudo tee /etc/apache2/extra/vhosts.d/${domain}.conf
touch "${domain_root}/log/access_log"
touch "${domain_root}/log/error_log"
else
echo "Domain conf already exists."
fi
grep "${domain}" /etc/hosts >/dev/null
if [ $? -ne 0 ]; then
echo " * Adding entry to /etc/hosts"
echo "127.0.0.1 ${domain} www.${domain}" | sudo tee -a /etc/hosts
else
echo "Nothing added to hosts"
echo " It appears that the domain already exists in your hosts file."
fi
echo "* Reloading apache"
sudo apachectl configtest && sudo apachectl restart
}
new_vhost ${1}' >> ~/bin/new_vhost
chmod 755 ~/bin/new_vhost
fi
if [ ! -f ~/bin/rm_vhost ]; then
echo "* Installing rm_vhost script into ~/bin"
echo '#!/bin/bash
function rm_vhost() {
if [ -z ${1} ]; then
echo
echo "USAGE: rm_vhost <domain>"
exit
fi
domain="${1}"
user=$(whoami)
domain_root="/Users/${user}/Sites/${domain}"
echo "Removing httpd configuration file."
sudo rm -rf /etc/apache2/extra/vhosts.d/${domain}.conf
grep "${domain}" /etc/hosts >/dev/null
if [ $? -eq 0 ]; then
echo "Removing local domain entry in /etc/hosts"
sudo sed -i "" "/127.0.0.1 ${domain} www.${domain}$/d" /etc/hosts
fi
if [ -d "${domain_root}" ]; then
echo " Leaving data at ${domain_root}"
fi
echo "Done."
}
rm_vhost ${1}' >> ~/bin/rm_vhost
chmod 755 ~/bin/rm_vhost
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment