Skip to content

Instantly share code, notes, and snippets.

@sdbondi
Created August 14, 2012 10:05
Show Gist options
  • Save sdbondi/3347952 to your computer and use it in GitHub Desktop.
Save sdbondi/3347952 to your computer and use it in GitHub Desktop.
Add new host in local apache server
#!/bin/bash
function usage {
echo "Creates a new local virutal host."
echo "usage: newhost [-s] sitename"
echo "Parameters:"
echo " s : Create HTTPS virtual host as well."
}
function safename {
echo $( echo -n "$1" | python -c "import re; import sys; sys.stdout.write(re.sub(r'[-]{2,}', '-', re.sub(r'[^a-z0-9]+', '-', sys.stdin.read().lower())))" )
}
if [ "$(id -u)" != "0" ]; then
echo "Sorry, you are not root."
exit 1
fi
if [ $# -eq 0 ];then
usage;exit 1;
fi
host="$1"
usehttps=0
if [ "$1" == "-s" ];then
useHttps=1
shift
if [ -z "$1" ];then
usage;exit 1;
fi
host="$1"
fi
host=$( safename "${host}" )
targetsite="${host}.local"
targetdir=/etc/apache2/sites-available
wwwdir=/var/www
if [ -f "${targetdir}/${targetsite}" ];then
echo "WARN: Virtual Host already exists! Overwriting..."
fi
echo "Creating virtual host ${targetsite} for directory ${wwwdir}/${host}"
# Create virtual host entry
cat >"${targetdir}/${targetsite}" <<EOF
<VirtualHost ${targetsite}:80>
ServerAdmin webmaster@localhost
ServerName ${targetsite}
DocumentRoot /var/www/${host}
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/${host}/>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog \${APACHE_LOG_DIR}/${targetsite}-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
if [ "$useHttps" == "1" ];then
certfile=/etc/apache2/certs/ssl.crt
certkey=/etc/apache2/certs/ssl.key
cat >>"${targetdir}/${targetsite}" <<EOF
<IfModule mod_ssl.c>
<VirtualHost ${targetsite}:443>
ServerAdmin webmaster@localhost
ServerName ${targetsite}
DocumentRoot /var/www/${host}
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/${host}/>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# SSL
SSLEngine on
SSLCertificateFile ${certfile}
SSLCertificateKeyFile ${certkey}
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
# MSIE 7 and newer should be able to use keepalive
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
ErrorLog \${APACHE_LOG_DIR}/${targetsite}-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog \${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>
EOF
fi
# Hostname
grep -e "127.0.0.1[ ]*${targetsite}" /etc/hosts > /dev/null
if [ $? -ne 0 ];then
echo "127.0.0.1 ${targetsite}" >> /etc/hosts
else
echo "Hosts entry already found."
fi
a2ensite "${targetsite}"
service apache2 reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment