一、编译安装的好处
在文章开头,我想安利给大家这种安装方式(编译安装),传统的yum安装包安装会将软件的各部分文件分散安装在默认位置,在后期进行系统维护的时候十分不方便,比如使用yum安装apache,那么安装后可执行文件默认放在/usr /local/bin, 库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,这样无形中就为维护工作添加了难度。
源码编译安装一般都是由三个部分组成:
1.配置(configure):依赖于gcc编译器,binutils,glibc。此步骤会配置软件特性,检查编译环境,生成Makefile文件
2.编译(make)
3.安装(make install)
编译安装可以自定义软件功能,优化编译参数,提高性能,解决不必要的软件间依赖,并且十分方便清理或数据迁移
编译安装后如果想卸载掉,直接遍历删掉安装的目录就好,或者在安装的目录make uninstall,如果想迁移数据甚至迁移软件,可以直接复制目录到其他机器(相同操作系统)
二、编译安装Apache2.4
1.依赖关系
httpd依赖于apr, apr-util
apr全称为apache portable runtime,能实现httpd跨平台运行
httpd-2.4 依賴于1.4+及以上版本的apr
这里我们下载的包为:
apr-1.6.5.tar.gz
apr-util-1.6.1.tar.gz
httpd-2.4.37.tar.gz
#解决依赖软件
yum install gcc
yum install pcre-devel
yum install binutils
yum install glibc
yum install openssl-devel
#获取软件包
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.5.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.37.tar.gz
#解压
tar -zxvf apr-1.6.5.tar.gz
tar -zxvf apr-util-1.6.1.tar.gz
tar -zxvf httpd-2.4.37.tar.gz
#编译安装
cd apr-1.6.5.tar.gz
./configure --prefix=/usr/local/apr #配置apr安装目录
make && make install
cd ../apr-util-1.6.1
./configure \
--prefix=/usr/local/apr-util \ #指定apr-util安装目录
--with-apr=/usr/local/apr #指定依赖的apr目录
make && make install
#这时候可能报错 fatal error : expat.h: No such file or directory 那么我们需要解决依赖
yum install expat-devel #之后重新编译安装
cd ../httpd-2.4.37
./configure \
--with-apr=/usr/local/apr \ #指定依赖的apr目录
--with-apr-util=/usr/local/apr-util \ #指定依赖的apr-util目录
--prefix=/usr/local/apache \ #指定安装目录
--sysconfdir=/etc/httpd24 \ #指定配置文件路径
--enable-so \ #启动模块动态装卸载
--enable-ssl \ #编译ssl模块
--enable-cgi \ #支持cgi机制
--enable-rewrite \ #支持url重写
--with-zlib \ #支持数据包压缩
--with-pcre \ #支持正则表达式
--with-mpm=prefork \ #指定httpd的工作方式为prefork
--enable-modules=most \ #启用的模块
--enable-mpms-shared=all #支持三种工作模式,关于工作模式,请查看我另一篇介绍apache工作模式的文章
make && make install
三、配置apache
1.将ServerName改为:localhost:80
vim /etc/httpd24/httpd.conf
找到ServerName将注释去掉然后更改保存退出
打开apache服务后访问IP查看配置是否正确:
/usr/local/apache/bin/apachectl start
2.添加apache环境变量:
cat >> /etc/profile << END
export PATH=$PATH:/usr/local/httpd/bin/
END
source /etc/profile
3.配置apche开机启动
cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
vim /etc/init.d/httpd
#在!/bin/bash后添加:
#chkconfig: 345 61 61 "#"不是本文注释是要添加在配置文件中注释掉的
#description:Apache httpd
#保存退出
#增加开机启动服务
chkconfig --add httpd
chkconfig --level 2345 httpd on
四、启动|停止|重启Apache
启动|停止|重启 /etc/init.d/httpd start|stop|restart