Shell script to append new lines to etc/hosts and Apache httpd-vhosts.conf in Mac OSX 10.6
Asked Answered
M

2

5

I am using Mac OSX 10.6 and doing web development on it. I know a small amount about writing shell scripts, but I am not really versed in them as of yet.

What I would like to do is to write a shell script that will simply ask for a local site alias and the document directory and it will then append the new alias onto hosts with something like "127.0.0.1 mysite.local" on a new line at the bottom of etc/hosts.

Then the script would append Apache's httpd-vhosts.conf file with something like this:

<VirtualHost *:80>
    DocumentRoot "/Repositories/myproject/mysite.com/trunk/htdocs"
    ServerName mysite.local
    ServerAlias mysite.localhost
</VirtualHost>

Then it would finally run the command to restart my Apache server. Now I know the terminal command to restart Apache, that is simple enough. I also know how to read in the site name and path from the user running the script. Such as below:

#!/bin/bash
read -p "New local site name: " site
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " sitepath

What I don't know how to do is to append text to a file from terminal.

Any thoughts or helpful ideas?

Thanks, Patrick

Munition answered 6/5, 2011 at 18:27 Comment(0)
A
10

Untested, but it should work:

#!/bin/bash
read -p "New local site name: " SITE
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " SITEPATH

#/etc/hosts
cp /etc/hosts /etc/hosts.original
echo -e "127.0.0.1\t${SITE}.local" >> /etc/hosts

#httpd-vhosts.conf
VHOSTSFILE="/etc/apache2/httpd-vhosts.conf"
cp $VHOSTSFILE ${VHOSTSFILE}.original
echo "<VirtualHost *:80>" >> $VHOSTSFILE
echo -e "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo -e "\tServerName ${SITE}.local" >> $VHOSTSFILE
echo -e "\tServerAlias ${SITE}.localhost" >> $VHOSTSFILE
echo '</VirtualHost>' >> $VHOSTSFILE

#restart apache

>> redirects the output to the given file, appending the contents to the file. I’m also using -e to allow \t to be expanded to a tab character.

Note that you need to run this script with sudo. I've also included commands to backup the original files before modifying them, just in case.

Astilbe answered 6/5, 2011 at 22:45 Comment(1)
Thanks man, that totally works. I really appreciate the help. The only changes I had to make was the filepath for the httpd-vhosts.conf file and adding "zendctl.sh restart-apache" to the end of it. Easy! Dude, you rock.Munition
T
0

I made some tweaks and did some extra stuff in the above answer, because this didn't work for me but it helped me to come up with another solution. This answer is only for Mac users.

First go in your /etc/apache2/httpd.conf and uncomment virtual host reference, which is this line Include /private/etc/apache2/extra/httpd-vhosts.conf

Now create a bash file I did that in my user’s home named as imran, you need to replace it with your username.

I placed it inside /Users/imran named as create_new_site.sh. I gave it executeable permissions, so it can be easily executed using chmod +x create_new_site.sh

Code for the script is as below:

#!/bin/bash
read -p "New local site name (prefix to .local): " SITE
SITEPATH=$SITE

sudo chmod -R a+w /Users/imran/Sites/web

SITEPATH="/Users/imran/Sites/web/${SITEPATH}"

mkdir -p $SITEPATH

#/etc/hosts
cp /etc/hosts /etc/hosts.original
echo -e "127.0.0.1\t${SITE}.local" >> /etc/hosts

#httpd-vhosts.conf
VHOSTSFILE="/etc/apache2/extra/httpd-vhosts.conf"
cp $VHOSTSFILE ${VHOSTSFILE}.original
echo "<VirtualHost *:80>" >> $VHOSTSFILE
echo -e "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo -e "\tServerName ${SITE}.local" >> $VHOSTSFILE
echo '</VirtualHost>' >> $VHOSTSFILE

echo '<?php phpinfo();' > "${SITEPATH}/phpinfo.php"

sudo chmod -R a+w $SITEPATH

#restart apache
sudo apachectl restart

echo "All done! visit, let's visit http://${SITE}.local/phpinfo.php"

Once that is done you can start creating new sites using sudo ./create_new_site.sh. Remember that you need to be in your home directory, which you can go via cd ~ command. Now let's suppose you created a site with name test. You should be able to visit http://test.local/phpinfo.php to see your vhost working.

Triumphant answered 20/12, 2019 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.