linux下apache的编译安装

注:安装环境:redhat linux 5.8
httpd版本:2.4.3
 

一、下载所需源码包及依赖包
(1)下载所需源码包及解决依赖关系,httpd的安装依赖于apr,apr-util两个包,要安装高版本的httpd,也要同时升级其所依赖包,这里我们直接登录ftp://172.16.0.1/puv/Sources/new_lamp中下载更高版本的两个依赖包:apr-1.4.6.tar.bz2和apr-util-1.4.1.tar.ba2;
yum源等已配置好,这里不再进行操作。
#lftp 172.16.0.1
#cd pub/Sources/new_lamp
#get apr-1.4.6.tar.bz2 apr-util-1.4.1.tar.bz2 httpd-2.4.3.tar.bz2

***httpd安装过程还要依赖于pcre-devel包,yum安装此包即可***
#yum -y install pcre-devel

(2)下载过后先来解压安装两个依赖包,指定安装到/usr/local目录下
#tar xf apr-1.4.6.tar.bz2 ***解压到当前目录即可***
#cd apr-1.4.6.tar.bz2
#./configure --prefix=/usr/local/apr ***./configure是用于检查系统是否有编译时所需的库,以及库的版本是否满足编译的需要等安装所需要的系统信息,--prefix指定安装目录为/usr/local/apr***
#make && make install ***检查过后,开始进行编译和安装***

#cd
#tar xf apr-util-1.4.1.tar.bz2 ***开始安装第二个依赖包***
#cd apr-util-1.4.1
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr ***这个包的安装又要依赖于前一个安装的包,--with-apr指定依赖的包的目录***
#make && make install
好了依赖关系解决了,准备安装httpd
 

二、编译安装httpd.2.4.3;
(1)已经下载过了,切到所下载的目录中。另外./configure选项很重要,这里需要用到很多选项,会有另外一篇博文来讲它的选项的解释
#cd
#tar xf httpd.2.4.3.tar.bz2
#cd httpd.2.4.3
#./configure --prefix=/usr/local/apache --enable-so ooenable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-mpms-shared=all --with-mpms=event --sysconfdir=/etc./httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr
***指定apache的安装目录,指定默认安装模块和支持所有模块的切换,***

#make && make install
(2)编译安装完后开始配置httpd的配置文件,指令路径及服务脚本,man文档等
#vim /etc/profile.d/httpd.sh
***编辑添加httpd的指令到默认路径中,添加行:export PATH=$PATH:/usr/local/apache/bin;***

#vim /etc/man.config ***添加行:PATH = /usr/local/apache/man ;添加httpd的man帮助文档到默认目录中***
#setenforce 0 ***将selinux中的enforce更改为permissive。若要永久生效可在/etc/selinux/config中更改配置文件***

#cd /etc/httpd
#cp httpd.conf httpd.conf.bak ***在对httpd.conf配置前最好先将其备份***
#vim httpd.conf
***添加行:PidFile "/var/run/httpd.pid";将httpd的进程配置文件加入默认安装时的默认目录中***
相应的/etc/httpd/extra/httpd-mpm.conf中PidFile的路径也要更改

#vim /etc/httpd/extra/httpd-mpm.conf
***将PidFile的路径更改为:/var/run/httpd.pid;***

基本配置完成了,编译安装后apache会提供apachectl的指令,用来控制apache的启动停止的。
#apachectl start
#netstat -ntlp
***启动apache服务,查看端口信息,出现80端口,说明已成功完成编译安装***

(3)查看web服务器的状态
#telnet localhost 80
***键入此命令,查看80端口的web服务器的状态,这是的服务并不支持长连接,若要支持长连接,进行如下操作***

#vim /etc/httpd/httpd.conf
***添加如下行:KeepAlive on
KeepAliveTimeOut 5
MaxKeepAliveRequest 60 ***定义最大长连接的请求数为60***

重启服务,便可支持长连接服务了。。

(4)为本次编译安装的apache提供sysv服务
将如下脚本添加至/etc/rc.d/init.d/下,命名为httpd;
******************************************************************************************************************************
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac

exit $RETVAL

**********************************************************************************************************************************************************
添加完成后重新登录系统,执行httpd命令
#httpd -t
#service httpd restart
***这样,编译安装的apache就更加完善了,无需使用apachectl来控制web服务器了***

(4)关于编译安装的虚拟机web服务配置
#vim /etc/httpd/httpd.conf
***进入此配置文件,找到:Include /etc/httpd/extra/httpd-vhosts.conf此行,将注释去掉,同时找到:DocumentRoot "/usr/local/apache/htdocs";这是主机网页文件的存放目录,将此行注释掉***

***虚拟机的配置文件在/etc/httpd/extra/httpd-vhosts.conf中,编辑此文件***
#vim /etc/httpd/extra/httpd.conf
***可以看到编译安装时默认产生的虚拟机配置文件,将这些删掉,我们自己来创建配置文件***

***添加如下行*******************
<VirtualHost *:80>
DocumentRoot "/web/magedu"
ServerName www.magedu.com
<Directory "/web/magedu">
AllowOverride none
Option none
Require all granted
</Directory>
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "/web/example"
ServerName www.example.com
<Directory "/web/example">
AllowOverride none
Option none
Require all granted
</Directory>
</VirtualHost>
********************************
这里设置两台虚拟主机,编辑主机的hosts文件和windows的hosts文件,均添加如下行:
172.16.12.1 www.magedu.com
172.16.12.1 www.example.com
#vim /etc/hosts ***这是虚拟机的hosts配置文件
***添加过上述行,这两个主机名在本地便可以解析到了***


说明:一切都只是在笔者本机上进行测试的,故我们自己来建立web网页文件的存放目录;
在根目录在依次创建目录:web/{magedu,example};
#mkdir -pv /web/{magedu,example}
#vim /web/magedu/index.html
***为magedu这台虚拟机创建网页文件index.html;添加如下行:
<h1>welcome to magedu's page</h1>;***
#vim /web/example/index.html
***为example创建网页文件index.html;添加如下行:
<h1>welcome to example's page</h1>;***

#service httpd restart
***重启httpd服务***

在windows上浏览器键入上述两个定义的虚拟主机名,便可浏览所配置网页内容了。。。。。