这是依据lnmp的vhost 改写而成。 1脚本适用于archlinux下通过yaourt安装的lnmp环境。 2修改一些逻辑顺序。 3调整每个vhost的日志到单独文件夹到 /var/log/nginx/xxx/access.xxx.log and /var/log/nginx/xxx/error.xxx.log. 4为每个vhost增加单独error log 5修改hosts,绑定新增的网站解析到本地

#!/bin/bash

# Check if user is root
if [ $(id -u) != “0” ]; then
echo “Error: You must be root to run this script, use sudo sh $0”
exit 1
fi

clear

if [ “$1” != “–help” ]; then

read -p "(Please input domain):" domain
while \[ "$domain" = ""  \]; do
    read -p "(MUST input a domain):" domain
done
echo "==========================="
if \[ ! -f "/etc/nginx/vhost/$domain.conf" \]; then
echo "==========================="
echo "domain=$domain"
echo "==========================="
else
echo "==========================="
echo "$domain is exist!"
echo "==========================="
fi

echo "Do you want to add more domain name? (y/n)"
read add\_more\_domainame

if \[ "$add\_more\_domainame" == 'y' \]; then

  echo "Type domainname,example(bbs.vpser.net forums.vpser.net luntan.vpser.net):"
  read moredomain
      echo "==========================="
      echo domain list="$moredomain"
      echo "==========================="
  moredomainame=" $moredomain"
fi

read -p "(Default directory:  /srv/http/$domain):" vhostdir
if \[ "$vhostdir" = "" \]; then
    vhostdir="/srv/http/$domain"
    while \[ -d $vhostdir \]; do
        echo "The \[$vhostdir\] is EXIST!!!"
        echo "Please input ANOTHER name of directory for the domain"
        read -p "(Default directory:  /srv/http/$domain):" vhostdir
    done
fi
echo "==========================="
echo Virtual Host Directory="$vhostdir"
echo "==========================="

echo "==========================="
echo "Allow Rewrite rule? (y/n)"
echo "==========================="
read allow_rewrite

if \[ "$allow_rewrite" == 'n' \]; then
    rewrite="none"
else
    rewrite="other"
    echo "Please input the rewrite of programme :"
    echo "wordpress,discuz,typecho,sablog,dabr rewrite was exist."
    read -p "(Default rewrite: other):" rewrite
    if \[ "$rewrite" = "" \]; then
        rewrite="other"
    fi
fi
echo "==========================="
echo You choose rewrite="$rewrite"
echo "==========================="

echo "==========================="
echo "Allow access_log? (y/n)"
echo "==========================="
read access_log

if \[ "$access_log" == 'n' \]; then
  al="access_log off;"
else
  echo "Type access_log name(Default access log file:$domain.log):"
  read al_name
  if \[ "$al_name" = "" \]; then
    al_name="access.$domain"
    el_name="error.$domain"
  fi
  alf="log\_format  $domain  '$remote\_addr - $remote\_user \[$time\_local\] "$request" '
         '$status $body\_bytes\_sent "$http_referer" '
         '"$http\_user\_agent" $http\_x\_forwarded_for';"
  al="access\_log  /var/log/nginx/$domain/$al\_name.log  $domain;"
  el="error\_log /var/log/nginx/$domain/$el\_name.log;"
echo "==========================="
echo You access log file="$al_name.log"
echo You error log file="$el_name.log"
echo "==========================="
fi

get_char()
{
SAVEDSTTY=\`stty -g\`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
echo ""
echo "Press any key to start create virtul host..."
char=\`get_char\`

if [ ! -d /etc/nginx/vhost ]; then
mkdir /etc/nginx/vhost
fi

echo “Create Virtul Host directory……”
mkdir -p $vhostdir
echo “Creat $domain’s log directory”
if [ ! -d /var/log/nginx/$domain ]; then
mkdir -p /var/log/nginx/$domain
fi
echo “Create access log file for $doamin”
touch /var/log/nginx/$domain/$al_name.log
echo “Create error log file for $doamin”
touch /var/log/nginx/$domain/$el_name.log
echo “set permissions of Virtual Host directory……”
chmod -R 755 $vhostdir
chown -R codewalker:root $vhostdir
echo “binding domain to localhost……”
echo “127.0.0.1 $domain” >> /etc/hosts
if [ ! -f /etc/nginx/$rewrite.conf ]; then
echo “Create Virtul Host ReWrite file……”
touch /etc/nginx/$rewrite.conf
echo “Create rewirte file successful,now you can add rewrite rule into /etc/nginx/$rewrite.conf.”
else
echo “You select the exist rewrite rule:/etc/nginx/$rewrite.conf”
fi

cat >/etc/nginx/vhost/$domain.conf<>/usr/local/php/etc/php.ini<