apache的介绍和安装详解
apache的介绍和安装详解apache介绍
介绍:
Apache:Apache HTTP Server是开源软件项目的杰出代表,基于标准的HTTP协议提供网页浏览服务。Apache可以运行在Windows,Linux,Unix等多种操作系统平台上
Apache主要特性
支持最新的HTTP/1.1通信协议
拥有简单而强有力的基于文件的配置过程
支持通用网关接口
支持基于IP和基于域名的虚拟主机
支持多种方式的HTTP认证
集成Perl处理模块
集成代理服务器模块
支持实时监视服务器状态和定制服务器日志
支持服务器端包含指令(SSI)
支持安全Socket层(SSL)
提供用户会话过程的跟踪
支持FastCGI
通过第三方模块可以支持Java Servlets
安装
apache默认在系统镜像里有,名为httpd包,可以用yum直接安装,也可以用源码包编译安装。源码包编译安装方式可以定制所需功能,加载特定的模块。所以生产环境下一般都是源代码编译安装
下面介绍怎么用源码报编译安装apache。版本为(httpd-2.4.4)
1)卸载系统中默认的httpd软件,若没有则忽略
2)检查apr版本。系统默认情况下为1.3,由于httpd-2.4.*所需版本为1.4.6,所以需要安装apr最新版,而httpd-2.2.*不需要
# rpm -qa |grepapr
apr-util-1.3.9-3.el6_0.1.x86_64
apr-1.3.9-5.el6_2.x86_64
安装apr-1.4.6
# cd /usr/src/
# wget https://archive.apache.org/dist/apr/apr-1.4.6.tar.bz2
# tar jxfapr-1.4.6.tar.bz2
# cd apr-1.4.6
#./configure --prefix=/usr/local/apr-1.4.6
#make&& make install
安装apr-util-1.4.1
# cd/usr/src/
#wget https://archive.apache.org/dist/apr/apr-util-1.4.1.tar.bz2
# tar jxfapr-util-1.4.1.tar.bz2
# cdapr-util-1.4.1
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr-1.4.6/
#make && make install
3)下载httpd-2.4.4,解压并编译安装
# cd /usr/src/
# wget https://archive.apache.org/dist/httpd/httpd-2.4.4.tar.gz
# tar zxfhttpd-2.4.4.tar.gz
# cd httpd-2.4.4
#./configure --prefix=/usr/local/httpd-2.4.4--enable-so --enable-rewrite --enable-ssl --enable-cgi --enable-cgid--enable-mods-shared=most --enable-mpms-shared=all--with-zlib --with-pcre--with-apr=/usr/local/apr-1.4.6/ --with-apr-util=/usr/local/apr-util-1.41
# make&& make install
参数解释:
--enable-so :支持动态共享模块如果没有这个模块PHP将无法与apache结合工作
--enable-ssl :启用支持ssl
--enable-cgi :支持cgi
--enable-rewrite :支持URL重写
--with-zlib :压缩库,在互联网上传播时可节约带宽
--with-apr=/usr/local/apr-1.4.6/ :指定apr路径
--with-apr-util=/usr/local/apr-util-1.41/:指定apr-util路径
--enable-mpms-shared=all:支持多道处理模块
--enable-mods-shared=most 动态加载大部分模块
在编译过程中可能会报两个错
报错1:
checking for pcre-config... false
configure: error: pcre-config forlibpcre not found. PCRE is required and available from http://pcre.org/
解决方法:这是缺少pcre的开发包导致的,安装pcre-devel即可
yum installpcre-devel –y
报错2:
checking whether to enable mod_ssl...configure: error: mod_ssl has been requested but can not be built due toprerequisite failures
解决方法:这是缺少openssl开发模块导致的,安装openssl-devel即可
yum install openssl-devel -y
补充:
构建MPM为静态模块
在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 httpd -l 来确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。
构建 MPM 为动态模块
在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。 构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM。
4)安装完成之后,优化apache,使更容易操作
4.1)优化执行路径
# cd/usr/local/
# ln -shttpd-2.4.4/ httpd
# ln -sf/usr/local/httpd-2.4.4/bin/* /usr/bin/
# httpd -v
Server version: Apache/2.4.4 (Unix)
Server built: Dec 16 2014 12:17:45
4.2)制作启动脚本,添加httpd为系统服务,并设置开机自启动
可以使用httpd自带的apachectl作为控制脚本,但是需要加执行权限,另外需要在文件中添加两行
#description:ApacheController Script
#chkconfig: - 85 15
操作步骤:
#cp/usr/local/httpd-2.4.4/bin/apachectl /etc/init.d/httpd
# sed -i '1a #description:Apache Controller Script'/etc/init.d/httpd
# sed -i '2a #chkconfig:- 85 15'/etc/init.d/httpd
# chmod a+x/etc/init.d/httpd
# head -3/etc/init.d/httpd
#!/bin/sh
#description:Apache Controller Script
#chkconfig:-85 15
# ll/etc/init.d/httpd
-rwxr-xr-x 1 root root 3512 12月16 12:38 /etc/init.d/httpd
4.4)启动Apache
# service httpdstart
# netstat -anput |grep httpd
tcp 0 0 :::80 :::* LISTEN 50106/httpd
# lsof -i :80
COMMANDPID USER FDTYPE DEVICE SIZE/OFF NODE NAME
httpd50106 root 4u IPv651433 0t0 TCP *:http (LISTEN)
httpd50107 daemon 4uIPv6 51433 0t0TCP *:http (LISTEN)
httpd50108 daemon 4uIPv6 51433 0t0TCP *:http (LISTEN)
httpd50109 daemon 4uIPv6 51433 0t0TCP *:http (LISTEN)
# chkconfig --addhttpd
# chkconfig httpdon
# chkconfig --list| grep httpd
httpd 0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭
提示:需要关闭防火墙iptables和禁用selinux,否则Apache可能会启动报错
客户端测试:
补充:使用apachectl作为启动脚本发现,启动Apache后并没有对结果进行显示,只能通过查看端口才能判断Apache是否启动成功,现在写一个脚本,可以出现像系统服务一样的带有结果的脚本
#!/bin/bash
#description: Apache Controller Script
# chkconfig: - 85 15
#config: /usr/local/httpd/conf/httpd.conf
#pidfile: /usr/local/httpd/logs/httpd.pid
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
./etc/sysconfig/httpd
fi
HTTPD_LANG=${HTTPD_LANG-"C"}
INITLOG_ARGS=""
apachectl=/usr/local/httpd/bin/apachectl
httpd=${HTTPD-/usr/local/httpd/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/httpd/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n$"Starting $prog: "
LANG=$HTTPD_LANGdaemon --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 $RETVA
apache配置文件详解
yum安装的apache配置文件位于/etc/httpd/conf/httpd.conf
源码报安装的apache的配置文件位于*/httpd/conf/httpd.conf(*表示指定的Apache安装路径)
全局配置项
KeepAlive On设置是否允许持续性的连接 最好设置为ON
ServerRoot设置httpd服务器的根目录
Listen 80 设置httpd服务器监听的端口号
User 设置运行httpd进程的用户身份,默认为daemon
Group 设置运行httpd进程的组身份,默认为daemon
ServerAdmin设置httpd服务器的管理员邮箱
ServerName设置Web站点的完成主机名
DocumentRoot 设置网站的根目录
DirectoryIndex 设置网站的默认主页,可以设置多个,需用空格隔开
ErrorLog 设置错误日志文件的路径,默认路径为logs/error_log
LogLevel 设置记录日志的级别,默认为Warn警告
CustomLog 设置访问日志文件的路径,日志类型,默认路径为logs/access_log
PidFile 设置用户保存httpd进程号的文件,默认在logs/httpd.pid
CharsetDefault 设置站点的网页默认使用的字符集编码,如UTF-8,gb2312
Include 包含另一个配置文件的内容
区域配置项
//定义’/’目录区域的开始
Options FollowSymlinks //控制选项,允许使用符号链接
AllowOverrideNone //不允许隐含控制文件中的覆盖配置
Order deny,allow //访问控制策略的应用顺序
Deny from all //禁止任何人访问此目录
附:
Options 后面常跟的选项:
[*] none:不支持任何选项
[*] Indexes:允许索引目录
[*] FollowSynLinks:允许访问符号链接指向的源文件
[*] Includes:允许执行服务端包含(SSI)
[*] ExecCGI:允许运行CGI脚本
[*] ALL:支持所有选项
AllowOverride 常见的参数
[*] ALL 全部的权限均可覆盖
[*] AuthConfig仅有网页认证可覆盖
[*] Indexes 仅允许Indexes方面覆盖
[*] Limits 允许用户利用Allow,Deny与Order管理可浏览的权限
[*] None 不可覆盖
配置文件具体详解请参考Apache配置文件详解.doc
页:
[1]