Last Updated: February 25, 2016
·
18.31K
· nekwebdev

Bash Script to create new Apache2 virtual hosts.

Hello World!

Major edit:

You can find the script here.
Just run vhost -help to get the usage information.

I left the following for reference, but it is much easier and faster to use the new script.

If you are coming from my tip on how to setup a web development OS in VirtualBox, then the script will work as is (https://coderwall.com/p/sc6r_q)

This script will assume certain things:

  • That you plan on using the /home/username/www directory as your webroot.

  • That you have template and template-pub already setup in /etc/apache2/sites-available.

This is what my template virtual host file looks like, straight copy from the default one:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    ServerName template.local
    DocumentRoot /home/username/www/template
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /home/username/www/template/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from All
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride All
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now this is the template-pub virtual host file contents:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    ServerName template.local
    DocumentRoot /home/username/www/template/public
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /home/username/www/template/public/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from All
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride All
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

Here we only added /public to the DocumentRoot and Directory for frameworks that have a public folder and the applications one level higher. This is highly suggested as standard practice for a more secure web application, keep it one level above the public web root.

Now that this is taken care of here is the script in all it's glory. It will ask for the root password to be able to manage the virtual host files.

#!/bin/bash
# Duplicate the base vhost template, will need SUDO!!!
sudo -v

if [ "$2" == "pub" ]
then
    echo "Creating a vhost for $1 with a webroot in $HOME/www/$1/public/"
    sudo cp /etc/apache2/sites-available/template-pub /etc/apache2/sites-available/$1
else
    echo "Creating a vhost for $1 with a webroot in $HOME/www/$1/"
    sudo cp /etc/apache2/sites-available/template /etc/apache2/sites-available/$1
fi

sudo sed -i 's/template/'$1'/g' /etc/apache2/sites-available/$1
sudo sed -i '1s/^/127.0.0.1       '$1'.local\n/' /etc/hosts

sudo a2ensite $1
sudo service apache2 reload

echo "Done, please browse to http://$1.local to check!"

Copy it in your /home/username/bin folder as Mint 14.1 auto adds it to your $PATH (type echo $PATH to check it).
Make sure you chmod +x scriptname.sh

scriptname.sh vhostname

scriptname.sh vhostname pub

The second command will set the virtual host with it's web root inside the public folder of your project.

Hope it helps :)

1 Response
Add your response

I'm relatively new to linux. I've installed Ubuntu 13.10 and am having trouble with your new script. I saved it to ~/bin/vhosts.sh and chmod it. Made sure ~/bin was in my path as well. But when I run ./vhosts.sh -help it tries to create a help.local and ~/www/help dir instead of listing the commands. I know it's something super simple that I'm missing but I'm stuck and stupid. EDIT: Also of note I'm using ZSH.

over 1 year ago ·