terminal

Creating a new Apache virtual host is something that’s done for every web project. It’s not a complicated process, but it can be simplified further by creating a bash script to automate the process. My script will update the Apache vhosts, add the new site to the hosts file, create the site directory if it doesn’t exist, and restart Apache. This script could be greatly expanded or changed depending on your needs. The script needs to be run as sudo since it updates the hosts file.

 

Usage

To use the script simply use the following command

 

The Vhost Bash Script File

Create a script file named addvhost.sh

 

Require Sudo

The first part of the script requires that the command be run as sudo with root privileges, and exits if not. This is required to update the hosts file.

 

Configuration

The next section is all configuration

 

User Input

I have allowed the user to pass in the site url and doc root using command options or via prompts if the options are missing. To allow options I’ve done the following.

To prompt the user if the options were not passed I used the read command.

 

Create the Doc Root

The script will create the doc root directory if it doesn’t exist. If it does exist it will bypass this step.

 

Update Vhosts

The next step is to update the vhost by adding/replacing the vhost file in the sites-available directory. The first part opens the skeleton file and replaces some placeholders with the site url and absolute path to the doc root. The second part adds/replaces the the vhosts file with the new content.

 

Update Hosts File

The hosts file also needs to be updated, the next part of the script does this.

 

Enable the Site and Restart Apache

To complete the process the new site needs to be enabled and Apache needs to be restarted.

 

Vhost Skeleton File

This script makes use of the following vhost skeleton file, this can be updated to suit your needs.

 

Make the File Executable

Finally, make the file executable using the following command.

One comment on “Bash Script to Create Apache Virtual Hosts

  1. Mirko says:

    I’ve added the possibility to choose the skeleton filename as parameter

    while getopts “:u:d:s:” o; do
    case “${o}” in
    u)
    site_url=${OPTARG}
    ;;
    d)
    relative_doc_root=${OPTARG}
    ;;
    s)
    vhost_skeleton_path=”$current_directory/${OPTARG}.skeleton.conf”
    ;;
    esac
    done

    and made a skeleton for symfony with the new apache 2.4 syntax

    # @site_url@

    ServerName @site_url@
    DocumentRoot “@site_docroot@/web”

    AllowOverride All
    Require all granted

    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeScript assets

    Options FollowSymlinks

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Leave a Reply

Your email address will not be published. Required fields are marked *