Skip to content

Instantly share code, notes, and snippets.

@guiliredu
Forked from lavoiesl/apache-template
Created September 25, 2019 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guiliredu/7fd6102661ac9711fad65ef8bfef1876 to your computer and use it in GitHub Desktop.
Save guiliredu/7fd6102661ac9711fad65ef8bfef1876 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