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

apache .htaccess配置密码访问 .htpasswd

在/home/zhouitpro/www目录生成密码文件, 用户名为 zhouitpro,回车后会自动叫你输密码

#htpasswd -c .htpasswd  zhouitpro

配置.htaccess


AuthType Basic
AuthName "Restricted Files"
# (Following line optional)
AuthBasicProvider file
AuthUserFile /home/zhouitpro/www/.htpasswd 
Require valid-user