Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Created May 7, 2012 19:24
Show Gist options
  • Save lavoiesl/2629830 to your computer and use it in GitHub Desktop.
Save lavoiesl/2629830 to your computer and use it in GitHub Desktop.
Apache VirtualHost Template with variable replacement
<VirtualHost *:80>
ServerAdmin {USER}@cslavoie.com
ServerName {DOMAIN}
ServerAlias www.{DOMAIN}
ServerAlias {USER}.localhost
ServerAlias {USER}.static.cslavoie.com
DocumentRoot {DOC_ROOT}
<Directory {DOC_ROOT}>
AllowOverride All
Order allow,deny
Allow From All
</Directory>
AddHandler php-script .php
Action php-script /php5.fastcgi virtual
Alias /php5.fastcgi /var/www/fastcgi/php5.fastcgi
FastCGIExternalServer /var/www/fastcgi/php5.fastcgi -socket /var/run/php-fpm/{USER}.sock
LogLevel warn
CustomLog /var/log/apache2/{USER}.access.log combined
ErrorLog /var/log/apache2/{USER}.error.log
</VirtualHost>
#!/bin/bash
# Apache VirtualHost Template with variable replacement
if [ $# -lt 2 ]; then
echo "Usage: $(basename $0) user domain" >&2
exit 1
fi
user="$1"
domain="$2"
path="/home/$user/www"
template="/etc/apache2/sites-available/apache-template"
if [ ! -d "$path" ]; then
echo "Web directory $path doesn’t exists" >&2
exit 2
fi
if [ ! -f "$template" ]; then
echo "Template $template doesn’t exists" >&2
exit 2
fi
# Escape slashes
doc_root=$(echo "$path" | sed 's/\//\\\//g');
sed -e "s/{USER}/$user/g" -e "s/{DOC_ROOT}/$doc_root/g" -e "s/{DOMAIN}/$domain/g" $template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment