项目背景:在局域网内,取某一台主机当作服务器,并且开启ftp服务,提供apr、apr-utils、pcre、httpd软件下载
运行环境:redhat 6.4
#!/bin/bash
#自动化安装apache
yum list &>/dev/null
if [[ "$?" != "0" ]];then
if [ -e /dev/sr0 ];then
mount /dev/sr0 /mnt
elif [ -e /dev/cdrom ];then
mount /dev/cdrom /mnt
else
echo "未检测到光盘,请插入光盘"
exit 10
fi
#搭建yum源
cd /etc/yum.repos.d
sed -i "s/^/#/g" *.repo
cat << EOF >>*.repo
[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0
EOF
yum list &>/dev/null
if [ $? -eq 0 ];then
echo "yum源搭建成功"
else
echo "yum源搭建失败,系统退出"
exit 20
fi
fi
ruanjian=(gcc gcc-c++ lynx)
echo "${ruanjian[@]}"
for i in ${ruanjian[@]}
do
yum remove $i -y &>/dev/null
if [ $? -eq 0 ];then
yum install $i -y &>/dev/null
if [ $? -eq 0 ];then
continue
else
echo "$i install error,program exit"
exit 30
fi
else
echo "$i remove error"
exit 40
fi
done
#下载源码包
yum install expect -y &>/dev/null
ping -c 3 10.0.0.254 &>/dev/null
if [ $? -eq 0 ];then
yum install lftp -y &>/dev/null
mkdir /root/soft
cd /root/soft
/usr/bin/expect <<EOF
set timeout 20
spawn lftp 10.0.0.254
expect {
"*" { send "mget soft/lamp/httpd-2.4.20.tar.bz2 soft/lamp/apr-1.5.2.tar.gz soft/lamp/apr-util-1.5.4.tar.gz soft/lamp/pcre-8.36.tar.gz\n"}
}
expect {
"*" { send "exit\n"}
}
expect eof
EOF
if [ $? -eq 0 ];then
echo "源码包下载成功"
else
echo "源码包下载失败,程序退出"
exit 50
fi
else
echo "与服务器网络不通,程序退出"
exit 60
fi
cd /root/soft
tar -xf httpd-2.4.20.tar.bz2
cp -ar httpd-2.4.20 httpd
tar -xf apr-1.5.2.tar.gz
cp -ar apr-1.5.2 apr
tar -xf apr-util-1.5.4.tar.gz
cp -ar apr-util-1.5.4 apr-util
tar -xf pcre-8.36.tar.gz
cp -ar pcre-8.36 pcre
cd ./pcre
pwd
./configure --prefix=/usr/local/pcre --enable-utf8 --enable-jit &>./pcre.txt
if [ $? -eq 0 ];then
yum install make -y &>/dev/null
make &>./pcre.txt && make install &>./pcre.txt
if [ $? -eq 0 ];then
echo "pcre安装成功"
else
echo "pcre安装失败,程序退出"
exit 70
fi
else
echo "pcre配置失败,程序退出"
exit 80
fi
cd ..
cd ./httpd/srclib
pwd
cp -r ../../apr .
cp -r ../../apr-util .
cd ..
./configure --prefix=/usr/local/httpd --enable-cache --enable-cache-disk --enable-cache-socache --enable-so --enable-modules=all --enable-mods-shared=all --enable-echo --enable-data --enable-include --enable-sed --enable-proxy --enable-proxy-connect --enable-proxy-http --enable-proxy-fcgi --enable-proxy-scgi --enable-proxy-ajp --enable-proxy-balancer --enable-info --enable-rewrite --with-pcre=/usr/local/pcre --enable-mpms-shared=all --enable-userdir &>./httpd.txt
if [ $? -eq 0 ];then
make &>./httpd.txt && make install &>./httpd.txt
if [ $? -eq 0 ];then
echo "apache安装成功"
else
echo "apache安装失败,程序退出"
exit 80
fi
else
echo "apache配置失败,程序退出"
exit 90
fi
#编辑配置文件,开启apache服务
cd /usr/local/httpd/conf
pwd
ip="`ifconfig eth0|grep "inet addr:"|awk -F ":" '{print $2}'|awk '{print $1}' `"
sed -i "/ServerName www.example.com:80/a ServerName $ip" httpd.conf
sed -i "/^#Include conf\/extra\/httpd-mpm.conf/a Include conf/extra/httpd-mpm.conf" httpd.conf
sed -i "/^#Include conf\/extra\/httpd-default.conf/a Include conf/extra/httpd-default.conf" httpd.conf
#验证apache服务是哦否开启成功
#cd /usr/local/httpd/bin
#./apachectl start
#lsof -i:80 -n
#if [ $? -eq 0 ];then
# echo "apache服务开启成功"
#else
# echo "apache服务开启失败,开始查看错误日志"
# tail ../logs/error_log
#fi
#将httpd服务加入到service管理
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
sed -i "9a # chkconfig: - 85 15" /etc/init.d/httpd
chkconfig --add httpd
service httpd start
lsof -i:80 -n &>/dev/null
if [ $? -eq 0 ];then
echo "apache服务开启成功"
else
echo "apache服务开启失败,开始查看错误日志"
tail ../logs/error_log
fi
我的测试结果会报错,说mod_slotmem_shm这个模块没有开启,可以编辑httpd.conf文件,开启这个模块,可以成功开启apache服务
具体错误如下:
[Sun Aug 28 13:40:01.931826 2016] [proxy_balancer:emerg] [pid 74484:tid 140584019928832] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Sun Aug 28 13:40:01.932096 2016] [:emerg] [pid 74484:tid 140584019928832] AH00020: Configuration Failed, exiting