Centos6.9 编译安装Apache2.4

1 配置安装环境:

yum -y install gcc gcc++ zlib zlib-devel 

卸载原有的Apache

rpm -qa|grep httpd

删除已经安装的所有httpd包

rpm -e --nodeps httpd-xxx-xxx.el6.xxx.x86_64

2 下载对应版本的包

wget http://mirrors.shuosc.org/apache//httpd/httpd-2.4.38.tar.gz

1)去http://httpd.apache.org/download 下载apache源码包
2)解压到/data,在下载目录执行tar -zxvf httpd-2.4.38.tar.gz -C /data (我用的是httpd-2.4.38.tar.gz)
3)进入目录/data/httpd-2.4.38,

# ./configure --prefix=/data/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl

4)出现configure: error: APR not found。解决办法:
  4.1)去apr下载地址 http://apr.apache.org/ 下载源码

wget http://mirrors.shuosc.org/apache//apr/apr-1.5.2.tar.gz

http://archive.apache.org/dist/apr/apr-1.5.2.tar.gz
  4.2)解压到/data,在下载目录执行tar -zxvf apr-1.5.2.tar.gz -C /data (我用的是apr- 1.5.2.tar.gz)
  4.3)进入目录/data/apr-1.5.2,执行./configure --prefix=/data/apr;make;make install
5)再次进入目录/data/httpd-2.4.38,增加参数–with-apr=/data/apr/
重新执行:./configure --prefix=/data/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl --with-apr=/data/apr/
6)出现configure: error: APR-util not found .解决办法
  6.1)去apr-util下载地址 http://apr.apache.org/ 下载源码

wget http://mirrors.shuosc.org/apache//apr/apr-util-1.5.2.tar.gz

http://archive.apache.org/dist/apr/apr-util-1.5.2.tar.gz
  6.2)解压到/data,在下载目录执行tar -zxvf apr-util-1.5.2.tar.gz -C /data (我用的是apr-util-1.5.2.tar.gz)
  6.3)进入目录/data/apr-util-1.5.2,执行./configure --prefix=/data/apr-util --with-apr=/data/apr/;make;make install
  6.4)make的时候出现错误

xml/apr_xml.c:35:19: error: expat.h: No such file or directory
xml/apr_xml.c:66: error: expected specifier-qualifier-list before ‘XML_Parser’
xml/apr_xml.c: In function ‘cleanup_parser’:
xml/apr_xml.c:364: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:365: error: ‘apr_xml_parser’ has no member named ‘xp’

解决办法:可能是缺少expat库
    6.4.1)yum install expat-devel 安装之后再make;make install
7)再次进入目录/data/httpd-2.4.38,增加参数–with-apr-util=/data/apr-util/,重新执行:./configure --prefix=/data/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl --with-apr=/data/apr/ --with-apr-util=/data/apr-util/
8)出现configure: error: pcre-config for libpcre not found.解决办法:
  8.1)去pcre下载地址 http://pcre.org/ 下载源码(注意:下载的时候一定不要下错了,不是pcre2 )

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42tar.gz

8.2)解压到/data,在下载目录执行tar -zxvf pcre-8.42.tar.gz -C /data(我用的是pcre-8.42.tar.gz)
  8.3)进入目录/data/pcre-8.42,执行./configure --prefix=/data/pcre;make;make install
9)再次进入目录/data/httpd-2.4.38,增加参数–with-pcre=/data/pcre,重新执行:./configure --prefix=/data/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl --with-apr=/data/apr/ --with-apr-util=/data/apr-util/ --with-pcre=/data/pcre
10)出现

configure: WARNING: OpenSSL version is too old;checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures;

解决办法:
  10.1)openssl源码下载地址http://www.openssl.org/source/ 下载源码

wget https://www.openssl.org/source/openssl-1.0.2n.tar.gz

10.2)解压到/data,在下载目录执行tar -zxvf openssl-1.0.2n.tar.gz -C /data(我用的是openssl-1.0.2n.tar.gz)

10.3)进入目录/data/openssl-1.0.1c,执行./config --prefix=/data/openssl shared;make;make install
注意此处的shared选项,否则在httpd make的时候会报mod_ssl相关错误
11)再次进入目录/data/httpd-2.4.38,增加参数–with-ssl=/data/openssl,重新执行:./configure --prefix=/data/apache2 --with-mpm=worker --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-nonportable-atomics --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl --with-apr=/data/apr/ --with-apr-util=/data/apr-util/ --with-pcre=/data/pcre --with-ssl=/data/openssl ;预编译没有问题
make如果出现以下错误:

/data/apr-util/lib/libaprutil-1.so: undefined reference to `XML_GetErrorCode'
/data/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetEntityDeclHandler
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory `/usr/local/httpd-2.4.26/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/httpd-2.4.26/support'
make: *** [all-recursive] Error 1

解决办法发:centos6.9的系统使用1.6的apr 和apr-util版本太高了,需要降低版本,将apr和apr-util降为1.5的版本,在重新编译安装即可。下载地址如下:
http://archive.apache.org/dist/apr/apr-util-1.5.2.tar.gz
http://archive.apache.org/dist/apr/apr-1.5.2.tar.gz
12)设置apache开机自动启动
首先拷贝apachectl到目录/etc/init.d/,执行命令

sudo cp apachectl /etc/init.d/httpd 

vim /etc/init.d/httpd 

脚本首行添加以下两行

# chkconfig: 2345 71 71 
# description: Apache2.4 is a world wide web server

注意:2345 是指改服务可以随系统的2345启动级别启动和停止,71 71 两个数字分别指启动顺序和停止顺序。
13)将httpd添加为开机启动

chkconfig --add httpd

这行命令的意思是在/etc/rc.d/rc*/ 添加/etc/init.d/httpd这个文件

chkconfig --list |grep httpd 

查看是否将httpd添加为系统服务

chkconfig httpd on 

开启开机启动httpd服务
14)去浏览器输入127.0.0.1,可以显示apache自带的网页It works。该网页的默认目录是在/data/apache2/htdocs/。apache到此就安装OK了。

你可能感兴趣的:(Apache)