CentOS7 配置 httpd2.4 服务
1. httpd2.4 介绍
新特性
- (1)
MPM
支持运行DSO
机制,以模块形式按需加载 - (2) 正式支持
event MPM
机制 - (3) 支持异步读写
- (4) 支持每模块及每个目录分别使用各自的日志级别
- (5) 每请求配置,
<If>
如果某个模块存在、某个文件存在、请求某个文件等触发配置 - (6) 增强版的表达式分析器, 提高正则表达式效率
- (7) 支持毫秒级的
keepalive timeout
- (8) 基于主机名(
FQDN
)的虚拟主机不再需要NameVirtualHost
指令 - (9) 支持用户自定义变量
新模块
- (1)
mod_proxy_fcgi
:提供fastCGI
支持 - (2)
mod_ratelimit
:提供速率限制 - (3)
mod_remoteip
:提供远端IP
控制
注意事项
- 不再支持使用
Order
,Deny
,Allow
来做基于 IP 的访问控制,使用Require
的方式进行访问控制
2. httpd2.4 安装
CentOS6
一般通过编译安装httpd2.4
版本
httpd2.4
依赖
httpd2.4
依赖于apr-1.4+
、apr-util-1.4+
以上的版本,apr-icon
可选apr
(apache portable runtime apache
)表示运行时环境,用于兼容多平台
CentOS6
特性
- 默认的依赖包为
apr-1.3.9
、apr-util-1.3.9
不符合httpd2.4
的要求 - 如果冒然升级
CentOS6
系统自带的apr
会导致服务不可用,所以建议保留老版本并且安装新版本的包
安装编译步骤
# (1) 安装开发环境,安装pcre-devel
yum groupinstall "Development Tools" "Server Platform Development"
yum install pcre-devel
# (2) 下载1.4+版的apr
tar -xjvf
./configure --prefix=/usr/local/apr # 目录不一样是因为方式和系统的apr冲突
make && make install
# (3) 下载1.4+版的apr-util
# apr-util是apr的扩展,需要针对于apr安装,所以使用with进行依赖安装
apr-util
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
# (4) 创建用户和属组
groupadd -r apache
useradd -r -g apache apahce
yum install pcre-devel # centos6默认不会安装,提供--with-pcre功能
# (5) 编译配置文件
./configure --prefix=/usr/local/apache --sysconf=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
make && make install
# (6) 启动服务
# 首先关闭系统的httpd服务,否则会占用80端口
/usr/local/apache/bin/apachectl # 不要使用rpm包的apachectl命令,应该使用全路径
# 配置之后可以使用不指定全路径
vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin/apachectl:$PATH
. /etc/profile.d/httpd.sh
# 自己编译的httpd没有server服务脚本,可以使用apachectl进行启动此外
# 服务启动脚本,可以借鉴系统rpm包中的httpd启动脚本
apachectl
3. 配置文件
这里主要讲的是
CentOS7
上编译安装的httpd
服务配置,如果使用yum
进行安装,则和CentOS6
配置几乎一致。
- 推荐使用
yum
进行安装,不然自行编译很难进行大规模部署- 如果想使用
httpd2.4
就直接升级系统到centos7
,不建议自行编译安装,除非需要使用新特性
配置文件
- 主配置文件
/etc/httpd/conf/httpd.conf
- 会加载
conf.modules.d
下的模块
- 模块配置文件
/etc/httpd/conf.modules.d/*.conf
- 动态模块的存放位置,编号是为了排序
- 辅助配置文件
/etc/httpd/conf.d/*.conf
mpm
模式
- 在
httpd2.4
以 DSO 机制提供 - 配置文件
/etc/httpd/conf.modules.d/00-mpm.conf
服务控制
systemctl {start|stop|restart|status|reload} httpd.service
4. 服务配置
这里主要讲的是和
httpd2.2
服务配置不同的地方
3.1 切换使用 MPM
# (1)切换使用MPM 00-mpm.conf
LoadModule mpm_NAME_module modules/mod_mpm_NAME.so
NAME: prefork, event, worker
LoadModule 模块名 模块路径(相对于/usr/lib64/)
httpd -M查看当前MPM模式,使用-l已经没用了
3.2 基于IP
的访问控制
# (2) 基于IP的访问控制法则
httpd2.4中默认没有文件授权,所以不会显示文件列表,拒绝访问
需要删除或者重命名welcome.conf文件 需要做显示授权才能访问
允许所有主机访问:Require all granted
拒绝所有主机访问:Require all deny
控制特定IP访问:
需要在<RequireAll>中封装起来
Require ip IPADDR:授权指定来源地址的主机访问
Require not ip IPADDR:拒绝指定来源地址的主机访问
IPADDR:
IP: 172.16.100.2
Network/mask: 172.16.0.0/255.255.0.0
Network/Length: 172.16.0.0/16
Net: 172.16
控制特定主机(HOSTNAME)访问
Require host HOSTNAME
Require not host HOSTNAME
HOSTNAME:
FQDN: 特定主机
DOMAIN:指定域内的所有主机
举例说明: 需要使用Directory/RequireAll进行封装才能使用生效
<RequireAll>
Require all granted
Require not ip 10.252.46.165
</RequireAll>
4.3 虚拟主机
# (3) 虚拟主机
模块化配置可以方便自动化运维
注释主配置main且在conf.d目录中可以配置vhosts.conf或者配置在httpd.conf
对于centos6编译安装的httpd2.4来说(/etc/httpd24)和centos7 rpm包安装的配置文件布局不同
httpd.conf是主配置文件,而模块化在extra目录中进行配置
基于IP、Port和FQDN都支持;
基于FQDN的不再需要NameVirtualHost指令;
4.4 服务脚本
CentOS 7
的服务脚本和CentOS 6
不同
# (4) CentOS 6 服务脚本
cd /etc/rc.d/init.d/
cp httpd httpd24
修改配置文件,可以在改进下
chkconfig --add httpd24
chkconfig --list httpd24
chkconfig httpd24 on
service httpd start
ss -tnl
- 借鉴系统
rpm
包中的httpd
启动脚本
#!/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=/usr/local/apache/bin/httpd
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd24.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
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
}
# See how we were called.
case "$1" in
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