很多软件没有发布rpm包,而是直接发布了源码,好处是这样能兼容更多的平台,但是,用户需要对源码进行编译后安装。利用工具make将源码生成二进制文件。
本文主要讲解了对httpd进行编译安装。分别介绍centOS6和centOS7两个系统版本上httpd的安装
2 CentOS6编译安装httpd下载httpd-2.2.34.tar.bz2这个安装包到centOS6,一般建议把软件包放在路径/usr/local/src这个路径下。
解开该压缩文件 tar -xvf httpd-2.2.34.tar.bz2。
接下来,要用make来变成C程序的文件。解压后的目录下,默认还没有Makefile这个文件,该文件用来记录如何编译该软件,安装在哪个路径下,哪些软件功能启用,哪些功能不启用。这个就是体现了源码编译安装的灵活度,可以定制功能。Makefile是通过configure这个工具来生成的,由源码发布者提供。
Makefile 是configure工具借助Makefile.in这个模板来生成的文件
C语言源代码编译安装三步骤:
1、./configure
(1) 通过选项传递参数,指定启用特性、安装路径等;执行时会参考用户的指定以及Makefile.in文件生成Makefile
(2) 检查依赖到的外部环境,如依赖的软件包
2、make 根据Makefile文件,构建应用程序
3、make install 复制文件到相应路径
有些情况下,没有configure文件,可以通过autoconf生成configure脚本,automake这个工具用来生成Makefile.in这个文件,一般这两个文件由开发者生成发布。
configure脚本支持大量的选项,根据不同的选项来生成Makefile文件
安装开发包组,这个是编译开发软件要用到的工具。建议都要安装。这个包组安装完成后,可能在编译过程还需要用到其他工具,具体情况根据缺失提示信息再安装对应的开发包。
configure要指定安装目录,--prefix=PREFIX 指定软件包的安装路径,如果没有指定,则是按照默认的路径安装。
运行如下语句,生成Makefile文件
./configure --prefix=/sunny/httpd66 --sysconfdir=/etc/httpd66 --enable-ssl
运行语句,发现如下报错,一般这种是缺失相应的开发包,比如提示OpenSSL缺失,就另行安装openssl-devel这个包。一般的包就是提示缺失的名字后加devel这种包就是了。不同的软件缺失的插件不一样,根据实际情况安装软件包。
图一 configure报错
该步骤完成后,除了生成Makefile这个文件外,还会生成其他相关的文件。
执行make,这里根据软件的大小决定了编译时间的长短,电脑性能好的话,编译时间会明显缩短。编译完成后 ,会生成新的文件
make
执行make install 开始安装
make install
到这里,httpd软件安装成功。接下来需要对软件进行启动以及配置。
如果这里有指定软件的安装路径,则到对应路径的/bin目录下开启软件,如本实验中路径是/sunny/httpd66/bin/apachectl,但是这样的操作比较麻烦,建议直接在/etc/profile.d目录下生成文件,写入该路径,这里建议把路径放在前面,因为系统里有一个路径和这个服务一样,会先匹配执行前面的
echo 'export PATH=/sunny/httpd66/bin:$PATH' > /etc/profile.d/httpd66.sh . /etc/profile.d/httpd66.sh //运行该文件,使得路径生效
这里有个小细节,用单引号,不会展开PATH这个变量,比较好看,如果用双引号,则会展开变量,路径比较长,而且路径已经被固定了,但是,在这里两个写法都是正确的写法,结果如下图
图二 单引号和双引号的区别
启动apache服务
apachectl start
验证一下,ss -ntl 查看80端口是否打开,如果打开就可以去通过网页打开。
由于编译时指定了路径,这里如果要改变浏览器首页的信息,可以到路径/sunny/httpd66/htdocs/ 更改index.html这个文件。
如果要使用man帮助查看httpd的相关信息,需要把软件的man文档路径加入到man的配置文件里执行如下语句
vim /etc/man.config MANPATH /sunny/httpd66/man //添加文件安装指定路径下的man路径
到这里为止,httpd编译完成,服务可以正常启用
3 一键安装apache代码以下是在centOS6上一键编译安装apache的脚本
#!/bin/bash # ----------------+---------------------------------------+ # * Author : Sunny # * Create time : 2017-08-08 # * Last modified : 2017-08-08 # * Filename : httpd.sh # * Description : Auto install apache for centOS6 # * version : 1.0 # ----------------+---------------------------------------+ #定义变量 export httpdpkg=`find / -name "httpd-2.2.34.tar.bz2" | head -1` echo "httpdpkg is $httpdpkg" export pkgnu=`find / -name "httpd-2.2.34.tar.bz2" | wc -l` echo "pkgnu is $pkgnu" export httpdir="/usr/local/src/httpd-2.2.34" #解压httpd服务包 case $pkgnu in 0) cd /usr/local/src ; wget http://apache.fayea.com/httpd/httpd-2.2.34.tar.bz2; tar xvf httpd-2.2.34.tar.bz2; ;; *) tar xvf $httpdpkg -C /usr/local/src/ &>/dev/null ; ;; esac #安装编译 [ -d $httpdir ] && cd $httpdir || echo "httpdir is not exist.please check" echo "You are in `pwd` now" yum grouplist "development tools" &>/dev/null|| yum -y groupinstall "Development Tools" &>/dev/null || echo "Devel tools is install" rpm -q apr-devel &>/dev/null || yum -y install apr-devel &>/dev/null; rpm -q apr-util-devel &>/dev/null || yum -y install apr-util-devel &>/dev/null; rpm -q openssl-devel &>/dev/null || yum -y install openssl-devel &>/dev/null; rpm -q gcc &>/dev/null ||yum -y install gcc &>/dev/null; #因为编译过程可能会缺失各种工具包,所以这里不隐藏编译过程,如果有问题,可以及时发现解决 ./configure --prefix=/sunny66/httpd66 &>/dev/null; [ $? = 0 ] && make &>/dev/null|| echo "Please check configure" [ $? = 0 ] && make install &>/dev/null || echo "Please check make"; [ $? = 0 ] && echo " httpd is successful install" || echo "Warning,install is failure,Please check make install" #简单的初始化配置,注意是添加apache的bin路径到PATH echo 'export PATH=/sunny66/httpd66/bin:$PATH' > /etc/profile.d/httpd22.sh . /etc/profile.d/httpd22.sh && echo "Now PATH is $PATH" echo "MANPATH /sunny66/sunny66/man" >> /etc/man.config; echo "ServerName localhost:80" >>/sunny66/httpd66/conf/httpd.conf sed -i 's/It works!/Welcome to Sunny Home/g' /sunny66/httpd66/htdocs/index.html apachectl start; #关闭防火墙和selinux setenforce 0; iptables -F; #检查端口状态 ss -ntl | grep 80; [ $? = 0 ] && echo "httpd is success.you can open web to check" || echo "Apache do not start" #解除变量 unset httpdpkg; unset pkgnu; unset httpdir; echo "Congratulation"; exit 0;