当这两个版本的源代码下载好后,我们先将这两个源代码包放在一个文件夹里,这个文件夹取名为httpd。
#install development tools
#安装开发工具组包,其中-y表示当需要输入y/n时,直接选择y
1 yum groupinstall -y "development tools" &> /dev/null
#这段代码是获取系统的主要版本号是centos6还是centos7,然后将结果放在releasever这个变量里
2 releasever=`cat /etc/centos-release | egrep -o " [0-9]" | cut -d" " -f2`
#在编译安装过程中会提示我们有些工具包没有安装,我们这里提前先安装
3 yum -y install apr-devel.x86_64 &> /dev/null
4 yum -y install apr-util-devel.x86_64 &> /dev/null
#pcre这个包是我在centos7上测试安装时提示我缺少的包,所以先判断是不是centos7版本,是就安装上
5 [ $releasever -eq 7 ] yum -y install pcre-devel.x86_64 &> /dev/null
开始编译:
#先切换目录到httpd下
18 cd httpd
#判断系统主版本号,如果是7则解压2.4版本的httpd源代码,如果是6则解压2.2版本的httpd源代码
#然后切换目录到对应的解压后的文件夹里
#执行configure文件,--prefix=/app/httpd22或者24是指,编译安装的后的文件放在哪,我们这里将所有的文件都放在了/app/httpd22里
#如果需要将不同的文件放在不同的目录下,则需要自己添加相关参数。
19 [ $releasever -eq 7 ] && tar -xf httpd-2.4.27.tar.bz2 && cd httpd-2.4.27 && ./configure --prefix=/app/httpd24 && make && make install
20 [ $releasever -eq 6 ] && tar -xf httpd-2.2.34.tar.bz2 && cd httpd-2.2.34 && ./configure --prefix=/app/httpd22 && make && make install
21 echo "#####httpd install finished####"
22 echo "#####start config the PATH ####"
#在/etc/profile.d/目录下写配置文件,将httpd的启动过路径放在PATH变量里
23 [ $releasever -eq 7 ] && echo 'export PATH=/app/httpd24/bin:$PATH' > /etc/profile.d/httpd.sh
24 [ $releasever -eq 6 ] && echo 'export PATH=/app/httpd22/bin:$PATH' > /etc/profile.d/httpd.sh
#让配置文件生效
25 source /etc/profile || echo "httpd.sh start failed"
#启动httpd服务,如果失败则输出开启失败
26 apachectl start || echo "start httpd failed"
27 echo "#####start httpd finished"
#注销变量
28 unset releasever
完整代码:
1 #!/bin/bash
2 #install development tools
3 yum groupinstall -y "development tools" &> /dev/null
4 releasever=`cat /etc/centos-release | egrep -o " [0-9]" | cut -d" " -f2`
5 yum -y install apr-devel.x86_64 &> /dev/null
6 yum -y install apr-util-devel.x86_64 &> /dev/null
7 [ $releasever -eq 7 ] yum -y install pcre-devel.x86_64 &> /dev/null
8 cd httpd 9 [ $releasever -eq 7 ] && tar -xf httpd-2.4.27.tar.bz2 && cd httpd-2.4.27 && ./configure --prefix=/app/ht tpd24 && make && make install 10 [ $releasever -eq 6 ] && tar -xf httpd-2.2.34.tar.bz2 && cd httpd-2.2.34 && ./configure --prefix=/app/ht tpd22 && make && make install
11 echo "#####httpd install finished####"
12 echo "#####start config the PATH ####"
13 [ $releasever -eq 7 ] && echo 'export PATH=/app/httpd24/bin:$PATH' > /etc/profile.d/httpd.sh
14 [ $releasever -eq 6 ] && echo 'export PATH=/app/httpd22/bin:$PATH' > /etc/profile.d/httpd.sh
15 source /etc/profile || echo "httpd.sh start failed" 16 apachectl start || echo "start httpd failed"
17 echo "#####start httpd finished" 18 unset releasever
18 unset releasever
安装成功界面: