[脚本解读]第二期:军哥的lnmp 添加绑定域名脚本(1)

#!/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
echo “=========================================================================”
echo “Add Virtual Host for LNMP V0.9  ,  Written by Licess ”
echo “=========================================================================”
echo “LNMP is a tool to auto-compile & install Nginx+MySQL+PHP on Linux ”
echo “This script is a tool to add virtual host for nginx ”
echo “For more information please visit http://www.lnmp.org/”
echo “”
echo “=========================================================================”

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

domain=”www.lnmp.org”
echo “Please input domain:”
read -p “(Default domain: www.lnmp.org):” domain
if [ “$domain” = “” ]; then
domain=”www.lnmp.org”
fi
if [ ! -f “/usr/local/nginx/conf/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

vhostdir=”/home/wwwroot/$domain”
echo “Please input the directory for the domain:$domain :”
read -p “(Default directory: /home/wwwroot/$domain):” vhostdir
if [ “$vhostdir” = “” ]; then
vhostdir=”/home/wwwroot/$domain”
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=”$domain”
fi
alf=”log_format  $al_name  ‘\$remote_addr – \$remote_user [\$time_local] \”\$request\” ‘
‘\$status \$body_bytes_sent \”\$http_referer\” ‘
‘\”\$http_user_agent\” \$http_x_forwarded_for’;”
al=”access_log  /home/wwwlogs/$al_name.log  $al_name;”
echo “===========================”
echo You access log file=”$al_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 /usr/local/nginx/conf/vhost ]; then
mkdir /usr/local/nginx/conf/vhost
fi

echo “Create Virtul Host directory……”
mkdir -p $vhostdir
touch /home/wwwlogs/$al_name.log
echo “set permissions of Virtual Host directory……”
chmod -R 755 $vhostdir
chown -R www:www $vhostdir

if [ ! -f /usr/local/nginx/conf/$rewrite.conf ]; then
echo “Create Virtul Host ReWrite file……”
touch /usr/local/nginx/conf/$rewrite.conf
echo “Create rewirte file successful,now you can add rewrite rule into /usr/local/nginx/conf/$rewrite.conf.”
else
echo “You select the exist rewrite rule:/usr/local/nginx/conf/$rewrite.conf”
fi

cat >/usr/local/nginx/conf/vhost/$domain.conf<<eof
$alf
server
{
listen       80;
server_name $domain$moredomainame;
index index.html index.htm index.php default.html default.htm default.php;
root  $vhostdir;

include $rewrite.conf;
location ~ .*\.(php|php5)?$
{
try_files \$uri =404;
fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
}

location ~ .*\.(js|css)?$
{
expires      12h;
}

$al
}
eof

echo “Test Nginx configure file……”
/usr/local/nginx/sbin/nginx -t
echo “”
echo “Restart Nginx……”
/usr/local/nginx/sbin/nginx -s reload

echo “=========================================================================”
echo “Add Virtual Host for LNMP V0.9  ,  Written by Licess ”
echo “=========================================================================”
echo “For more information please visit http://www.lnmp.org/”
echo “”
echo “Your domain:$domain”
echo “Directory of $domain:$vhostdir”
echo “”
echo “=========================================================================”
fi

时隔那么长时间,大家伙是不是以为阿福又跑路去BBQ了?然也,阿福虽然和不是BBQ总级别的,但是。实在是生活太累了。就连最好的同学的电话都没时间打。好吧,开玩笑。

这个似乎有点长,那么让我们慢慢来看。

首先是最先的一段。

#!/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

这一段,最前面的东东,相比大家通过第一篇也有一定的了解把,那就是声明,这段程序是bash。

下面一句带#的就是解释了,check if user is root 的意思就是检查用户是否是root。嗯,阿福觉得,谷歌翻译一定适合学习脚本的童鞋。

下面就是一个if结构了。

if [$ (id-u)!=”0″]; then

echo”Error:You must be root to run this scripts,use sudu sh $0”

exit 1

fi

其中$这个符号依旧是代表着一个变量。而id则是你的用户id号id -u 如果是root的话,那么id-u就是0如果不是,那么就显示echo后面的双引号里面的东西。echo大家去谷歌翻译一下把,大概就是显示的意思。然后就是退出exit 1了。然后就是结束if。

clear
echo “=========================================================================”
echo “Add Virtual Host for LNMP V0.9  ,  Written by Licess ”
echo “=========================================================================”
echo “LNMP is a tool to auto-compile & install Nginx+MySQL+PHP on Linux ”
echo “This script is a tool to add virtual host for nginx ”
echo “For more information please visit http://www.lnmp.org/”
echo “”
echo “=========================================================================”

这一段最简单 clear就是清屏,记住就OK了。

echo就是在屏幕上打印。。大家去运行脚本的时候看看具体是什么样子的就知道这个echo的是怎么运行的了。总之上面的这玩意就是一行一行的显示出来而已。

嗯。下面就是大if结构了。。。所以阿福还是下次讲为秒。

 

[脚本解读]第一期:简单而又不简单的502 自动重启脚本

CheckURL=”http://www.vpser.net”

STATUS_CODE=`curl -o /dev/null -m 10 –connect-timeout 10 -s -w %{http_code} $CheckURL`
#echo “$CheckURL Status Code:\t$STATUS_CODE”
if [ “$STATUS_CODE” = “502” ]; then
/etc/init.d/php-fpm restart

fi

这个就是军哥的lnmp1.0安装包中自带的502重启脚本。不过今天咱们不来谈用法。而谈谈他是怎么工作的。当然免不了,一句一句的解释意思。

首先是第一句:

CheckURL=”http://www.vpser.net”

这句里面的网址,阿福想,大家都晓得是什么了把,这个就是要检测的目标网址。第一句的意思就是给CheckURL这个变量赋值,学过编程的童鞋一般都晓得赋值是什么意思,这里阿福就给大家科普一下,赋值就是相当于给一个空碗盛饭,这个网址就相当于是饭,而这个变量CheckURL就相当于是饭碗。只有盛满了,才能给人家上饭么~

STATUS_CODE=`curl -o /dev/null -m 10 –connect-timeout 10 -s -w %{http_code} $CheckURL`

这里一定大家还是一头雾水。没关系,先从前面来,看到“=”了没这货在,就是赋值,所以前面的STATUS_CODE就是一个变量。也就是一个饭碗。后面的括号中的内容才是重点。-o 参数,是把下载的所有内容都重定向到/dev/null,-s命令,是屏蔽了curl本身的输出,而-w参数,是根据我们自己的需要,自定义了curl的输出格式。 这里还有一个要注意的就是一个变量当我们要试用的时候,要在变量前面加”$”符号,就如我们吃饭前必须用工具,如筷子,刀叉等吃一样。另外,两边的引号不要搞错了。。。不是单引号哦,是反引号哦~需要你在数字键做左边的“~” 键的英文状态输入哦~

 

然后就是进入高潮了,

if [ “$STATUS_CODE” = “502” ]; then
/etc/init.d/php-fpm restart

fi

这个高潮是以If语句来搞定的,如果STATUS_CODE 的值=于502,那么咱们就重启php-fpm 然后就是结束了,末尾的fi相当于c里面的break之类的。

至此,脚本解读第一期已经完毕,有任何不完善的地方,请不吝指出,感谢支持!