编译安装httpd-2.4.9及新特性详解

前言

前面我们讲解了httpd在CentOS6上(httpd-2.2)的相关功能配置,而CentOS7上采用了httpd-2.4的版本,那么httpd-2.4增加了哪些特性呢?接下来让我们在CentOS6.6上手动编译安装一下httpd-2.4.9,看一下和httpd-2.2有什么不同,顺便补充说一下httpd的其它功能。

环境及新特性介绍

环境介绍

系统环境:CentOS6.6

所需软件包:apr-1.5.0.tar.bz2、apr-util-1.5.3.tar.bz2、httpd-2.4.9.tar.bz2

注意:httpd2.4需要依赖apr和arp-util 1.4以上版本

httpd-2.4新特性

①MPM支持在运行时装载

②支持event MPM

③异步读写

④在每模块及每目录分别使用不同的日志级别

⑤每请求的配置;<If>,<Elseif>

⑥增强版的表达式分析器

⑦毫秒级的keep alive的timeout

⑧基于FQDN的虚拟主机不再需要NameVirtualHost指令

⑨支持用户使用自定义变量

新增模块:

mod_proxy_fcgi

mode_ratelimit

mod_request

修改的配置机制:

不再支持使用order, allow, deny定义基于ip的访问控制,改为require

编译安装

编译安装之前请确保开发包组已安装(Development tools、Server Platform Development)

yum groupinstall Development tools Server Platform Development -y

编译安装apr和apr-util

[root@scholar ~]# tar xf apr-1.5.0.tar.bz2 
[root@scholar ~]# cd apr-1.5.0
[root@scholar apr-1.5.0]# ./configure --prefix=/usr/local/apr
[root@scholar apr-1.5.0]# make && make install
[root@scholar apr-1.5.0]# cd ..
[root@scholar ~]# tar xf apr-util-1.5.3.tar.bz2 
[root@scholar ~]# cd apr-util-1.5.3
[root@scholar apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util
 --with-apr=/usr/local/apr/
 [root@scholar apr-util-1.5.3]# make && make install

编译安装httpd

[root@scholar apr-util-1.5.3]# cd ..
[root@scholar ~]# tar xf httpd-2.4.9.tar.bz2 
[root@scholar ~]# cd httpd-2.4.9
[root@scholar httpd-2.4.9]# ./configure --prefix=/usr/local/apache 
--sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi 
--enable-rewrite --enable-deflate --with-zlib --with-pcre 
--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ 
--enable-mpms-shared=all --with-mpm=event --enable-modules=most
[root@scholar httpd-2.4.9]# make && make install
编译参数详解:
--prefix:#安装路径
--sysconfdir:#指定配置文件路径
--enable-so:#DSO兼容,DSO=Dynamic Shared Object,动态共享对象,可实现模块动态生效
--enable-ssl:#支持SSL/TLS,可实现https访问 需已安装openssl-devel
--enable-cgi:#支持CGI脚本(默认对非线程的MPM模式开启)
--enable-rewrite:#启用Rewrite功能,URL重写
--enable-deflate:#支持压缩功能
--with-zlib:#使用指定的zlib库,不指定路径会自动寻找
--with-pcre:#使用指定的PCRE库,不指定路径会自动寻找 需已安装pcre-devel
--with-apr:#指定apr安装路径
--with-apr-util:#指定apr-util安装路径
--enable-mpms-shared:#支持动态加载的MPM模块,可选参数:all
--with-mpm:#设置默认启用的MPM模式,{prefork|worker|event}
--enable-modules:#支持动态启用的模块,可选参数:all,most,few,reallyall
#编译之前可使用./configure --help查看各项参数

添加环境变量

为了方便命令使用需要添加环境变量

[root@scholar ~]# vim /etc/profile.d/httpd24.sh  #名字自定义

export PATH=/usr/local/apache/bin:$PATH

[root@scholar ~]# source /etc/profile.d/httpd24.sh  #重读环境变量

启动服务,查看80端口是否被监听

1.jpg

访问测试,是否工作

2.jpg

编译安装默认首页就是“It works!”,默认文件在安装目录的htdocs目录下

如果二次开发就需要导出头文件

[root@scholar ~]# ln -sv /usr/local/apache/include/ /usr/include/httpd24

导出man手册

[root@scholar ~]# vim /etc/man.config 

MANPATH /usr/local/apache/man   #添加编译安装的man位置

提供脚本

#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#              server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#              server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server 
#  implementing the current HTTP standards.
### END INIT INFO

# 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-/usr/local/apache/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $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=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} $httpd
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  condrestart|try-restart)
        if status -p ${pidfile} $httpd >&/dev/null; then
                stop
                start
        fi
        ;;
  force-reload|reload)
        reload
        ;;
  graceful|help|configtest|fullstatus)
        $apachectl $@
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|
        status|fullstatus|graceful|help|configtest}"
        RETVAL=2
esac

exit $RETVAL

加入服务列表,测试脚本

3.jpg

OK,脚本可用,至此编译安装工作算是完成了

httpd-2.4配置讲解

配置文件

#/etc/httpd24为编译安装时定义的目录
/etc/httpd24/httpd.conf # 主配置文件
/etc/httpd24/extra/httpd-default.conf # 默认配置文件(keepalive、AccessFileName等设置)
/etc/httpd24/extra/httpd-userdir.conf # 用户目录配置文件
/etc/httpd24/extra/httpd-mpm.conf # MPM配置文件
/etc/httpd24/extra/httpd-ssl.conf # SSL配置文件
/etc/httpd24/extra/httpd-vhosts.conf # 虚拟主机配置文件
/etc/httpd24/extra/httpd-info.conf # server-status页面配置文件

通过以上配置文件可以看出编译安装的httpd-2.4与rpm方式安装的httpd-2.2除了配置文件位置不一样,其它配置如:虚拟主机,基于用户的访问控制,https等配置都和上文一样的,除了基于IP的访问控制与上文中的不太一样,我们在开篇的时候也说了,httpd-2.4不再支持使用order, allow, deny这些机制,而是统一使用require进行访问控制。

例如:
<Directory "/usr/local/apache/htdocs">
    AllowOverride None
    Require ip 172.16.0.0/16
    Require not ip 192.168.1.0/24
</Directory>
Require ip IPADDR    #控制某特定主机的访问
Require not ip IPADDR
Require all granted  #允许所有主机访问
Require all deny     #拒绝所有主机访问
Require host HOSTNAME
Require not host HOSTNAME
HOSTNAME:
 FQDN:具体的主机
 DOMAIN: 域内的所有主机

补充

最后补充一点没讲到的功能,通用功能非httpd-2.4特有

CGI功能

CGI(Common Gateway Interface),通用网关接口,被用来解释处理来自表单的输入信息,并在服务器产生相应的处理,或将相应的信息反馈给浏览器,CGI程序使网页具有交互功能。

CGI工作原理

CGI工作原理.jpg

配置CGI

我们直接用编译安装的httpd-2.4.9配置

启用cgi模块

[root@scholar ~]# vim /etc/httpd24/httpd.conf 

LoadModule cgid_module modules/mod_cgid.so
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"  #这里我们就用默认位置

创建CGI脚本

[root@scholar ~]# vim /usr/local/apache/cgi-bin/cgi.sh #名字自定义

#!/bin/bash

cat << EOF
Content-Type: text/html

<pre>
The requested resource is $(basename $0).
The datetime is $(date).
</pre>

EOF

修改权限,重启服务

4.jpg

测试CGI

5.jpg

测试成功,CGI配置完成

压缩功能

压缩,顾名思义就是将比较大的文件压缩为较小的文件,节约空间。应用于httpd就可以减小传输字节,节省流量,加快网页加载速度,从而提高用户体验。

Web服务器处理HTTP压缩的过程:

① Web服务器接收到浏览器的HTTP请求后,检查浏览器是否支持HTTP压缩(Accept-Encoding 信息);

② 如果浏览器支持HTTP压缩,Web服务器检查请求文件的后缀名;

③ 如果请求文件是HTML、CSS等静态文件,Web服务器到压缩缓冲目录中检查是否已经存在请求文件的最新压缩文件;

④ 如果请求文件的压缩文件不存在,Web服务器向浏览器返回未压缩的请求文件,并在压缩缓冲目录中存放请求文件的压缩文件;

⑤ 如果请求文件的最新压缩文件已经存在,则直接返回请求文件的压缩文件;

配置压缩功能

为了演示效果,我们人为制造一个较大的网页

[root@scholar ~]# cp /var/log/messages /usr/local/apache/htdocs/test.html
[root@scholar ~]# chmod +r /usr/local/apache/htdocs/test.html

测试未启用压缩功能时的传输字节

6.jpg

启用模块

[root@scholar ~]# vim /etc/httpd24/httpd.conf 

LoadModule deflate_module modules/mod_deflate.so

定义压缩类型,压缩等级及其它

SetOutputFilter DEFLATE

# mod_deflate configuration
 
#指定对某些MIME的格式文件进行压缩  
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css

#配置压缩等级(1-9)  
# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9

#对某些不支持压缩的浏览器部分压缩或不压缩  
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html
  
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
  
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

检查语法,重启服务

7.jpg

测试启用压缩功能后的传输字节

8.jpg

压缩成功,压缩功能配置完成

status页面

httpd内嵌有handler,其中有一个handler用于输出当前httpd服务相关状态信息

handler: server-status

配置status

启用模块及配置

[root@scholar ~]# vim /etc/httpd24/httpd.conf 

LoadModule status_module modules/mod_status.so

Include /etc/httpd24/extra/httpd-info.conf
[root@scholar ~]# vim /etc/httpd24/extra/httpd-info.conf

<Location /server-status>
    SetHandler server-status
    #Require host .example.com
    Require ip 172.16.10.8
</Location>

检查语法,重启服务

9.jpg

测试status页面

10.jpg

显示成功,status功能实现

The end

好了,httpd服务就暂时讲解到这里了,希望本系列可以对你学习httpd有所帮助,如有更新,请关注后续文章。以上仅为个人学习整理,如有错漏,大神勿喷~~~

原创文章,作者:书生,如若转载,请注明出处:http://www.178linux.com/3219

(0)
上一篇 2015-04-15 18:42
下一篇 2015-04-15 20:27

相关推荐

  • 【LINUX编译安装程序】Centos7.4编译安装httpd 2.4.29

    举例:Centos7.4编译安装httpd 2.4.29

    Linux干货 2018-03-17
  • 第五周

      第五周 1 显示/boot/grub/grub.conf中以至少一个空白字符开头的行;      grep "^[[:space:]]\+" /boot/grub/grub.conf 2 显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至…

    Linux干货 2017-01-02
  • bash基础特性(一)之命令历史,命令补存,路劲补存,命令行展开,命令执行状态结果和引用

    bash是Unix shell的一种,在1987年由布莱恩·福克斯为了GNU计划而编写。1989年发布第一个正式版本,原先是计划用在GNU操作系统上,但能运行于大多数类Unix系统的操作系统之上,包括Linux与Mac OS X v10.4都将它作为默认shell。 Bourne shell是一个早期的重要shell,由史蒂夫·伯恩在1978年前后编写,并同…

    2017-09-20
  • linux-系统自动化安装

    一、安装程序及配置 CentOS 系统安装 系统启动流程: bootloader–>kernel(initramfs)–>rootfs–>/sbin/init anaconda: 系统安装程序 tui:  基于图形库curses 的文本 窗口 gui :图形窗口 安装程序启动过程 MB…

    Linux干货 2016-09-26
  • bash 基础特性1

    bash 基础特性 命令模式 systemctl set-default multi-user.target 图形模式 systemctl set-default graphical.target   # rmdir [OPTION]…DIRECTORY…-p:删除某录后,如果其父目录为空,则一并删除之-v:显示过程# mk…

    Linux干货 2017-06-01
  • man命令的用法

    man 命令的用法    在linux运维工作中,经常会有一些命令我们不是很清楚它们的准确用法,这时候我们就需要去查询这些命令的用法,linux中就有很多文档帮助我们去了解这些命令的用法,其中就有一条命令man,下面我就总结一下它的用法。     man 是 manual 的简写,提供命令的帮助文件。   &…

    Linux干货 2017-02-17