Skip to content

Instantly share code, notes, and snippets.

@drogers98
Created June 16, 2015 15:35
Show Gist options
  • Save drogers98/c1db9147bd30556278d4 to your computer and use it in GitHub Desktop.
Save drogers98/c1db9147bd30556278d4 to your computer and use it in GitHub Desktop.
New vhost script
#!/bin/bash
# Config vars
sites_path="$(eval echo ~${SUDO_USER})/Sites"
conf_file="${sites_path}/_conf/httpd-vhosts.conf"
hosts_path="/etc/hosts"
# Permissions
if [ "$(whoami)" != "root" ]; then
echo "Root privileges are required to run this, try running with sudo..."
exit 2
fi
# Get user input for domain
echo "Domain name (e.g. mysite.dev):"
read domain_name
# Check domain is not empty
if [ -z "$domain_name" ]; then
echo "Domain name cannot be empty"
exit 2
fi
# Get user input for domain
echo "Folder name (leave blank for same as domain):"
read folder_name
# Check domain is not empty
if [ -z "$folder_name" ]; then
folder_name="$domain_name"
fi
# Get user input for database name
echo "Database name (leave blank for none):"
read database_name
# Add virtual hosts to config file
echo "Writing new virtual host..."
echo -e "\n<VirtualHost *:80>\n ServerName ${domain_name}\n DocumentRoot ${sites_path}/${folder_name}\n SetEnv DB1_NAME \"${database_name}\"\n</VirtualHost>" >> $conf_file
echo "Success"
# update hosts file
echo "Updating hosts file..."
echo -e "127.0.0.1\t${domain_name}" >> $hosts_path
echo "Success"
# Restart apache
echo "Restarting apache..."
sudo apachectl graceful
echo "Success"
echo "Your new site is now available at ${domain_name}"
# Exit
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment