BASH SCRIPT TO SETUP MULTIPLE SITES ON OS X MOUNTAIN LION

after the mac os mountain lion update, my local web server stopped working. boo hiss. i had to google my way to fixing it. it took some time, but all is now well.

all that effort made me want to streamline the process for adding and setting up additional sites. since i don't know much about writing bash scripts it would be a learning experience too.

i needed the script to take in a server name and a directory path. using this information it would add an entry to the mac hosts file and a virtualhost block to the apache config file. then it would restart apache.

An example usage would look like so:

makesite -s somesite.localhost -d pathtodocumentroot

first off, some bash basics. how to create and run a script.

ok next hurdle, how to parse arguments. my script would have 3 options, -h for help, -s for the server name and -d for the directory that the site will reside in.

Once the arguments were sorted, the script would have to write to the mac host file.

echo 127.0.0.1 somesite.local >> '/private/etc/hosts';

...then the virtual host block via a heredoc

cat >> "/private/etc/apache2/extra/httpd-vhosts.conf" << EOD
<VirtualHost *:80>
	DocumentRoot "pathtodocumentroot"
	ServerName somesite.local
	<Directory "pathtodocumentroot">
		AllowOverride All
		Options All
	</Directory>
</VirtualHost>
EOD

...but only on the conditional that the user has specified values for -s and -d

if [[ -z $SERVERNAME ]] || [[ -z $DIRNAME ]]
then
	usage
	exit 1
else
	do the good stuff...
fi

...and it would be nice if the script wrote to the files only if the user was root

if [[ -z $SERVERNAME ]] || [[ -z $DIRNAME ]]
then
	usage
	exit 1
else
	if [ "(id -u)" != "0" ]
	then
		echo "This script requires root."
		exit 1
	else
		do the good stuff...
	fi

fi

and then restart apache.

apachectl restart;

Finally to finish things off, the script was moved so it would be available as a systemwide executable.

So that was the journey for my very first bash script.

 
MAGENTO, PHP, HTML5, XHTML, CSS3, JQUERY, JOOMLA, WORDPRESS, CODEIGNITER, MODX, PLESK, XML . . . . . . .
 
 

LIKE IT, +1 IT, PIN IT

FOLLOW ME

  • rss feed
  • facebook
  • flickr
  • LinkedIn
  • twitter
  • google+
  • pinterest
 

NEWSFEED

 
 

google trusted merchant

Thu 21 Mar 2013
 
 

circle css3 hovers

css3 circle transitions

Sat 22 Sep 2012
 
 

wa gwan

so whats happening?

Sat 01 Sep 2012
 
 

mobile site with separate urls

building a mobile site with separate urls as per google guidelines

Fri 31 Aug 2012
 
 

bash script to setup multiple sites on os x mountain lion

my first bash script

Sat 18 Aug 2012
 
 

please forgive my heart by bobby womack

bobby womack - please forgive my heart

Sun 11 Mar 2012
 
 

lmfao @ barrowlands, glasgow 2012

im in miami bitch

Sun 11 Mar 2012
 
 

free web hosting? i dont believe it!

Server hosting for free? It cannae be...Its a trick surely.

Sat 28 Jan 2012
 
 

iphone4 mac sync issue

Why is my iphone4 not showing up as a device on my mac?

Sun 11 Dec 2011
 
 

walking through doorways makes you forget...um

I wasn't meant to do some thing here....

Sat 19 Nov 2011
 
 

hong kong 2011

After 10+ years, Im back where it all began. Hong Kong!

Sat 19 Nov 2011
 
 

hi. hello. how you doing

Thu 01 Sep 2011
 
 

magento https css woes

hundred miles an hour, switchin lanes like WOE

Sun 24 Jul 2011
 
 

beladrum!

Got tickets for Beladrum 2011. Yas!

Sat 28 May 2011
 
 

modx

the more I use modx, the more I like it.

Wed 17 Mar 2010
 
 

dot net nuke

dot net cms. + limited dot net experience = lost time

Sat 20 Feb 2010
 
 

utilizing web services

hmm. soap. wsdl. web services. its all new stuff..

Sat 20 Feb 2010