Simple script to add a new Virtual Host on Mac OSX


Ideally when you develop a lot of websites on a local machine on OSX you have to create a virtual host for each website.
Below we will describe a really simple script to do this boring operation automatically.

Firstly a simple control to remember you have to choose a host name:

#!/bin/sh
if [ $# -lt 1 ]
then
echo
echo "You have to choose a host name!"
exit 0
fi

Then append a new host to the hosts file and verify it’s all right with a cat:

echo "\n" >> /etc/hosts
echo "127.0.0.1 $1" >> /etc/hosts
cat /etc/hosts

Now you can append the new Virtual Host to the apache configuration file httpd-vhosts.conf:

echo "\n" >> /etc/apache2/extra/httpd-vhosts.conf
echo "<VirtualHost *:80>" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerAdmin yourmail@email.com" >> /etc/apache2/extra/httpd-vhosts.conf
echo " DocumentRoot /Library/WebServer/Documents/$1/" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerName $1" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ErrorLog \"/private/var/log/apache2/$1-error_log\"" >> /etc/apache2/extra/httpd-vhosts.conf
echo " CustomLog \"/private/var/log/apache2/$1-access_log\" common" >> /etc/apache2/extra/httpd-vhosts.conf
echo " <Directory \"/Library/WebServer/Documents/$1/\">" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Options All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " AllowOverride All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Order allow,deny" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Allow from all" >> /etc/apache2/extra/httpd-vhosts.conf
echo " </Directory>" >> /etc/apache2/extra/httpd-vhosts.conf
echo "</VirtualHost>" >> /etc/apache2/extra/httpd-vhosts.conf
cat /etc/apache2/extra/httpd-vhosts.conf

Finally restart apache:

apachectl restart

This is the final script:

#!/bin/sh
if [ $# -lt 1 ]
then
echo
echo "You have to choose a host name!"
exit 0
fi
echo "\n" >> /etc/hosts
echo "127.0.0.1 " >> /etc/hosts
cat /etc/hosts
echo "\n" >> /etc/apache2/extra/httpd-vhosts.conf
echo "<VirtualHost *:80>" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerAdmin yourmail@email.com" >> /etc/apache2/extra/httpd-vhosts.conf
echo " DocumentRoot /Library/WebServer/Documents//" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerName " >> /etc/apache2/extra/httpd-vhosts.conf
echo " ErrorLog \"/private/var/log/apache2/-error_log\"" >> /etc/apache2/extra/httpd-vhosts.conf
echo " CustomLog \"/private/var/log/apache2/-access_log\" common" >> /etc/apache2/extra/httpd-vhosts.conf
echo " <Directory \"/Library/WebServer/Documents//\">" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Options All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " AllowOverride All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Order allow,deny" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Allow from all" >> /etc/apache2/extra/httpd-vhosts.conf
echo " </Directory>" >> /etc/apache2/extra/httpd-vhosts.conf
echo "</VirtualHost>" >> /etc/apache2/extra/httpd-vhosts.conf
cat /etc/apache2/extra/httpd-vhosts.conf
apachectl restart

download it here.
Once unzipped remember to edit the email on the script, and to make it executable with:

chmod +x newHost.sh

To execute the command you have to sudo (apache and the 2 files are root property):

sudo ./newHost chosenHostName

, , , , ,

  1. No comments yet.
(will not be published)