Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐: 强烈推荐:Ylmf OS 3.0 正式版发布附迅雷高 XP系统下硬盘安装Fedora 14图文教程
搜索:
您的位置: Linux安全网 > Linux入门 > » 正文

shell脚本添加和删除apahce虚拟主机并判断是否正常

来源: 游造 分享至:

1 安装sendEmail

tar xvf sendEmail-v1.56.tar.gz 
mv sendEmail-v1.56 sendEmail

2 添加apache虚拟主机shell脚本:

vi addVirtual.sh
#!/bin/bash
#cat <<EOF>> /etc/httpd/conf/httpd.conf
#<VirtualHost *:80>
#    ServerAdmin admin@$1
#    DocumentRoot /$1
#    ServerName $1
#    ErrorLog logs/$1-error_log
#    CustomLog logs/$1-access_log common
#</VirtualHost>
#EOF

#mkdir /$1
#chmod 755 /$1
#service httpd restart

port=`stat.html' target='_blank'>netstat -anl|grep 80 |sed -n '1p' |awk '{print $4}'|awk -F: '{ print $2}'`
ip=`ifconfig eth0|sed -n 2p|awk  '{ print $2 }'|awk -F : '{ print $2 }'`
if [ "$port" != "80" ]
   then
   /home/shell/sendEmail/sendEmail -f xxx@xxx -t xxx@xxx -s smtp.xxx -u "apache启动有问题!!!" -xu USER -xp PASSWD -m "${ip} apache shutdown,Please contact the administrator"
fi

执行脚本添加虚拟主机:


NameVirtualHost *:80 ####必须要开户###

[root@3 shell]# sh addVirtual.sh 2.test.com###2.test.com就是用户自己输入的域名####

测试:

在本地hosts文件添加:

192.168.2.5    2.test.com

192.168.2.5   3.test.com

访问2.test.com    ####在生成的/2.test.com文件下面创建个主页####

 

 

####说明添加虚拟主机正常#####

 添加3.test.com虚拟主机

[root@3 shell]# sh addVirtual.sh 3.test.com

测试:

访问3.test.com    ####在生成的/3.test.com文件下面创建个主页####

 

 

 

3 删除虚拟主机脚本:

 vi  delvirtual.sh
#!/bin/bash
i=$1
sed ':n;/ServerName '$i'[^\n]*$/{$!N;$!N;$!N;d};/\n.*\n.*\n/!{N;bn};P;D' /etc/httpd/conf/httpd.conf >httpd.conf.bak

>/etc/httpd/conf/httpd.conf
cat httpd.conf.bak>/etc/httpd/conf/httpd.conf

service httpd restart

port=`netstat -anl|grep 80 |sed -n '1p' |awk '{print $4}'|awk -F: '{ print $2}'` ###获取端口###
ip=`ifconfig eth0|sed -n 2p|awk  '{ print $2 }'|awk -F : '{ print $2 }'`
if [ "$port" != "80" ]    ####判断是否等于80,不等于就发送邮件####
   then
   /home/shell/sendEmail/sendEmail -f xxx@xxx -t xxx@xxx -s smtp.zhnews.net -u "apache启动有问题!!!" -xu USER -xp PASSWD -m "${ip} apache shutdown,Please contact the administrator"
fi

 

执行删除虚拟主机脚本:

[root@3 shell]# sh delvirtual.sh 3.test.com

再次访问3.test.com看是否正常?

 

 

OK!!访问到2.test.com那边去了,因为我默认的主页是2.test.com,自动跳转。

 

测试下当apache没有启动的报警(脚本只为测试,没有直接加上面的测试):

vi  port.sh
#!/bin/bash

service httpd stop

port=`netstat -anl|grep 80 |sed -n '1p' |awk '{print $4}'|awk -F: '{ print $2}'`
ip=`ifconfig eth0|sed -n 2p|awk  '{ print $2 }'|awk -F : '{ print $2 }'`
if [ "$port" != "80" ]
   then
   /home/shell/sendEmail/sendEmail -f xxx@xxx -t xxx@xxx -s smtp.xxx -u "apache启动有问题!!!" -xu USER -xp PASSWD -m "${ip} apache shutdown,Please contact the administrator"
fi

测试:

[root@3 shell]# sh port.sh
停止 httpd:[确定]
Dec 13 18:17:41 3 sendEmail[3788]: Email was sent successfully!

查看邮件:

 

#########使用简单脚本配置多虚拟主机会快速,以上脚本可以配合php/' target='_blank'>PHP使用,从PHP页面上配置虚拟主机,方便。。。。。


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史