#目前自己学习linux一段时间,就自己摸索写一个脚本,按照apache软件。经过验证是可以的。
#apahche:安装路径/usr/local/apache-2.2.0
#安装日志在/tmp目录下
#脚本说明:apache_install.sh只是简单的安装软件,修改了默认用户及主站点目录/www
#涉及到站点部署需要另写脚本。
#以下就是我写的脚本内容,纯属新手,可以修改的地方大家一定要帮我指出来,不要吝啬!
---------------------------------低调的分割线----------------------------------------------
#!/bin/bash
# Author:liwentong
# V.1-0 13-09-14
# Function: auto install the apache
#
#-------Diteminer if have apache---#
/bin/netstat -tln | grep 80
[ $? -eq 0 ] && echo "Already have a httpd runing..." && exit 1
[ -d /usr/local/apache ] && echo "You have install httpd" && exit 1
echo "======`date +%F-%T`======" >> /tmp/apache_install.log
#判断系统默认是否已经有apache在运行。
#------Install the softward-------#
[ ! -d /root/tools ] && /bin/mkdir -p /root/tools
[ ! -f /root/tools/httpd-2.2.0.tar.bz2 ] && \
{
cd /root/tools/
wget http://down1.chinaunix.net/distfiles/httpd-2.2.0.tar.bz2 1>/dev/null 2>>/tmp/apache_install.log
#判断是否有软件了,没有就从网络上下载。
#echo "You have't soft httpd-2.2.0.tar.bz2" >>/tmp/apache_install.log
#exit 1
}
cd /root/tools
/bin/tar jxf httpd-2.2.0.tar.bz2
cd httpd-2.2.0
#编译参数你们可以自己进行修改。安装目录为/usr/local/apache-2.2.0
./configure \
--prefix=/usr/local/apache-2.2.0 \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-modules=most \
--enable-so \
--with-mpm=worker \
--enable-rewrite
[ $? -ne 0 ] && \
{
echo "Configure error" >> /tmp/apache_install.log
exit 1
}
make && make install
[ $? -ne 0 ] && \
{
echo "Make && make install error" >> /tmp/apache_install.log
exit 1
}
[ $? -eq 0 ] && echo "Install apache successful" >> /tmp/apache_install.log
/bin/ln -s /usr/local/apache-2.2.0 /usr/local/apache
#------Add user apache------#
/bin/grep apache: /etc/shadow
[ $? -ne 0 ] && \
{
/usr/sbin/groupadd apache
/usr/sbin/useradd -g apache -s /sbin/nologin apache
}
#增加apache的用户和用户组
#-----Config the http.conf----#
[ ! -d /www ] && /bin/mkdir /www && chown -R apache.apache /www
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd-default.conf
sed -i 's#User daemon#User apache#g' /usr/local/apache/conf/httpd.conf
sed -i 's#Group daemon#Group apache#g' /usr/local/apache/conf/httpd.conf
sed -i 's#/usr/local/apache-2.2.0/htdocs#/www/#g' /usr/local/apache/conf/httpd.conf
sed -i 's#\#ServerName www.example.com:80#ServerName 127.0.0.1:80#g' /usr/local/apache/conf/httpd.conf
#修改一些最简单的http.conf配置。
#-----Test the http server----#
[ ! -f /usr/lib/libiconv.so.2 ] && /bin/ln -sf /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2
#这个是因为我在一次安装中有出现错误,特意加上这句。
/usr/local/apache/bin/apachectl -t 1> /dev/null 2>>/tmp/apache_install.log
[ $? -ne 0 ] && echo "http.conf have error " >> /tmp/apache_install.log && exit 1
/usr/local/apache/bin/apachectl start
[ $? -ne 0 ] && echo "Can't start httpd ../bin/apachectl" >>/tmp/apache_install.log && exit 1
/bin/echo "/usr/local/apache/bin/apachectl start" >> /etc/rc.local
/bin/cp /usr/local/apache/bin/apachectl /usr/sbin/httpd
/bin/netstat -tln | grep 80
[ $? -eq 0 ] && \
/bin/echo `netstat -tln |grep 80 ` >> /www/index.html && /bin/echo "Http runing..." >>/tmp/apache_install.log
--------------------------------------低调的分割线---------------------------------------
#此时如果你的机器网络是连通的,并且知道IP地址,那么直接在浏览器输入IP地址就可以访问了。防火墙要关闭。
#此脚本应该是一个比较低级的脚本,毕竟刚学习能力有限,希望以后自己能力强了,再回来完善吧。
#很希望大家能够把好的脚本给我看看