前言
前面我们讲解了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
[[email protected] ~]# tar xf apr-1.5.0.tar.bz2 [[email protected] ~]# cd apr-1.5.0 [[email protected] apr-1.5.0]# ./configure --prefix=/usr/local/apr [[email protected] apr-1.5.0]# make && make install [[email protected] apr-1.5.0]# cd .. [[email protected] ~]# tar xf apr-util-1.5.3.tar.bz2 [[email protected] ~]# cd apr-util-1.5.3 [[email protected] apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ [[email protected] apr-util-1.5.3]# make && make install
编译安装httpd
[[email protected] apr-util-1.5.3]# cd .. [[email protected] ~]# tar xf httpd-2.4.9.tar.bz2 [[email protected] ~]# cd httpd-2.4.9 [[email protected] 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 [[email protected] 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查看各项参数
添加环境变量
为了方便命令使用需要添加环境变量
[[email protected] ~]# vim /etc/profile.d/httpd24.sh #名字自定义 export PATH=/usr/local/apache/bin:$PATH [[email protected] ~]# source /etc/profile.d/httpd24.sh #重读环境变量
启动服务,查看80端口是否被监听
访问测试,是否工作
编译安装默认首页就是“It works!”,默认文件在安装目录的htdocs目录下
如果二次开发就需要导出头文件
[[email protected] ~]# ln -sv /usr/local/apache/include/ /usr/include/httpd24
导出man手册
[[email protected] ~]# 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 [email protected] RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart| force-reload|reload|status|fullstatus|graceful|help|configtest}" RETVAL=2 esac exit $RETVAL
加入服务列表,测试脚本
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
我们直接用编译安装的httpd-2.4.9配置
启用cgi模块
[[email protected] ~]# 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脚本
[[email protected] ~]# 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
修改权限,重启服务
测试CGI
测试成功,CGI配置完成
压缩功能
压缩,顾名思义就是将比较大的文件压缩为较小的文件,节约空间。应用于httpd就可以减小传输字节,节省流量,加快网页加载速度,从而提高用户体验。
Web服务器处理HTTP压缩的过程:
① Web服务器接收到浏览器的HTTP请求后,检查浏览器是否支持HTTP压缩(Accept-Encoding 信息);
② 如果浏览器支持HTTP压缩,Web服务器检查请求文件的后缀名;
③ 如果请求文件是HTML、CSS等静态文件,Web服务器到压缩缓冲目录中检查是否已经存在请求文件的最新压缩文件;
④ 如果请求文件的压缩文件不存在,Web服务器向浏览器返回未压缩的请求文件,并在压缩缓冲目录中存放请求文件的压缩文件;
⑤ 如果请求文件的最新压缩文件已经存在,则直接返回请求文件的压缩文件;
配置压缩功能
为了演示效果,我们人为制造一个较大的网页
[[email protected] ~]# cp /var/log/messages /usr/local/apache/htdocs/test.html [[email protected] ~]# chmod +r /usr/local/apache/htdocs/test.html
测试未启用压缩功能时的传输字节
启用模块
[[email protected] ~]# 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
检查语法,重启服务
测试启用压缩功能后的传输字节
压缩成功,压缩功能配置完成
status页面
httpd内嵌有handler,其中有一个handler用于输出当前httpd服务相关状态信息
handler: server-status
配置status
启用模块及配置
[[email protected] ~]# vim /etc/httpd24/httpd.conf LoadModule status_module modules/mod_status.so Include /etc/httpd24/extra/httpd-info.conf
[[email protected] ~]# vim /etc/httpd24/extra/httpd-info.conf <Location /server-status> SetHandler server-status #Require host .example.com Require ip 172.16.10.8 </Location>
检查语法,重启服务
测试status页面
显示成功,status功能实现
The end
好了,httpd服务就暂时讲解到这里了,希望本系列可以对你学习httpd有所帮助,如有更新,请关注后续文章。以上仅为个人学习整理,如有错漏,大神勿喷~~~