Skip to content

Instantly share code, notes, and snippets.

@itsmikita
Last active January 19, 2016 22:10
Show Gist options
  • Save itsmikita/6483fdace4fb0f774d05 to your computer and use it in GitHub Desktop.
Save itsmikita/6483fdace4fb0f774d05 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Local virtual host manager script for Mac (nginx edit)
#
# How to install:
# 1. Save this script somewhere on your disk (/var/www)
# 2. Set $user, $servers and $document_root
# 3. Create symbolic link: ln -s /path/to/vh.sh /usr/local/bin/vh
# 4. Run 'vh -h' for instructions
#
# Your UNIX username
user=""
# Virtual hosts configuration folder (/usr/local/etc/nginx/servers)
servers=""
# Document root folder (/var/www or /Users/$user/Sites)
document_root=""
# Name of this script
me=`basename $0`
# Add virtual host
if [ "$1" = "-a" ]; then
if [ "$2" = "" ]; then
echo "\n Missing server_name, run '$me -h' for help\n"
exit
elif [ `whoami` != 'root' ]; then
echo "\n Run this command as root like 'sudo $me ...'\n"
exit
fi
# Add nginx virtualhost file
server="$servers/$2.conf"
# Update these lines to fit your configuration
echo "server {" > $server
echo " listen 80;" >> $server
echo " server_name $2;" >> $server
echo " root $document_root/$2;" >> $server
echo " " >> $server
echo " include wordpress.conf;" >> $server
echo " include php-fpm.conf;" >> $server
echo "}" >> $server
echo ""
echo " Created new virtual host file '$server'"
# Add hostname to hosts
echo "127.0.0.1 $2" >> /etc/hosts
echo " Added '$2' to '/etc/hosts'"
# Create root directory if doesn't exists
if [ ! -d "$document_root/$2" ]; then
mkdir "$document_root/$2"
chown "$user:staff" "$document_root/$2"
echo " New folder created '$document_root/$2'"
fi
# Restart nginx
nginx -s stop
nginx
echo " Restarted nginx"
echo ""
# Remove virtual host
elif [ "$1" = "-r" ]; then
if [ "$2" = "" ]; then
echo "\n Missing server_name, run '$me -h' for help\n"
exit
elif [ `whoami` != 'root' ]; then
echo "\n Run this command as root like 'sudo $me ...'\n"
exit
fi
# Remove virtual host configuration file
rm "$servers/$2.conf"
echo "\n Removed '$servers/$2.conf'"
# Remove virtual hosts from /etc/hosts
grep -v $2 /etc/hosts > /etc/hosts.2
cp /etc/hosts.2 /etc/hosts
rm /etc/hosts.2
echo " Removed '$2' from '/etc/hosts'"
# Restart nginx
nginx -s stop
nginx
echo " Restarted nginx"
echo ""
# Print existing hosts
elif [ "$1" == "-l" ]; then
echo "\n Existing virtual hosts:\n"
for server in "$servers"/*; do
echo " $( basename "$server" )"
done
echo ""
# Print help
elif [ "$1" = "-h" ]; then
echo ""
echo " Basic usage:"
echo ""
echo " $me -a example.dev"
echo " Add virtual host 'example.dev' with root folder set to '$document_root/example.dev'"
echo ""
echo " $me -r example.dev"
echo " Remove virtual host example.dev"
echo ""
echo " $me -l"
echo " List existing virtual hosts"
echo ""
echo " $me -h"
echo " Prints this help instructions"
echo ""
# Unknown option
else
echo "\n Unknown option '$1', run '$me -h' for help\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment