Apache 创建虚拟主机脚本

depoly-site.sh

$sudo depoly-site.sh drupal
#!/bin/bash

if [[ $# -lt 1 || $1 == "--help" || $1 == "-h" ]]
then
  echo "Usage:"
  echo "  sudo `basename $0` SITENAME"
  echo "Examples:"
  echo "  sudo `basename $0` drupal-8"
  exit
fi

WORKDIR="/home/zhouitpro/www"
APACHEDIR="/etc/apache2/sites-available"
HOSTSFILE="/etc/hosts"

echo "
	ServerAlias $1.localhost
	DocumentRoot $WORKDIR/$1
	
		Options FollowSymLinks
		AllowOverride All
        
" > $APACHEDIR/$1
a2ensite $1
service apache2 restart
grep -q "127.0.0.1  $1.localhost" $HOSTSFILE
if [ $? -ne 0 ]; then
  echo "127.0.0.1  $1.localhost" >> $HOSTSFILE
fi