apache的介绍和安装详解
apache介绍介绍: Apache:Apache HTTP Server是开源软件项目的杰出代表,基于标准的HTTP协议提供网页浏览服务。Apache可以运行在Windows,Linux,Unix等多种操作系统平台上 安装apache默认在系统镜像里有,名为httpd包,可以用yum直接安装,也可以用源码包编译安装。源码包编译安装方式可以定制所需功能,加载特定的模块。所以生产环境下一般都是源代码编译安装 下面介绍怎么用源码报编译安装apache。版本为(httpd-2.4.4) 1)卸载系统中默认的httpd软件,若没有则忽略 2)检查apr版本。系统默认情况下为1.3,由于httpd-2.4.*所需版本为1.4.6,所以需要安装apr最新版,而httpd-2.2.*不需要 [iyunv@Apache_Server ~]# rpm -qa |grep apr apr-util-1.3.9-3.el6_0.1.x86_64 apr-1.3.9-5.el6_2.x86_64 安装apr-1.4.6 [iyunv@Apache_Server src]# cd /usr/src/ [iyunv@Apache_Server src]# wget https://archive.apache.org/dist/apr/apr-1.4.6.tar.bz2 [iyunv@Apache_Server src]# tar jxfapr-1.4.6.tar.bz2 [iyunv@Apache_Server src]# cd apr-1.4.6 [iyunv@Apache_Server apr-1.4.6]#./configure --prefix=/usr/local/apr-1.4.6 [iyunv@Apache_Server apr-1.4.6]#make&& make install 安装apr-util-1.4.1 [iyunv@Apache_Server apr-1.4.6]# cd/usr/src/ [iyunv@Apache_Server src]# wget https://archive.apache.org/dist/apr/apr-util-1.4.1.tar.bz2 [iyunv@Apache_Server src]# tar jxfapr-util-1.4.1.tar.bz2 [iyunv@Apache_Server src]# cdapr-util-1.4.1 [iyunv@Apache_Server apr-util-1.4.1]#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr-1.4.6/ [iyunv@Apache_Server apr-util-1.4.1]#make && make install
3)下载httpd-2.4.4,解压并编译安装 [iyunv@Apache_Server ~]# cd /usr/src/ [iyunv@Apache_Server src]# wget https://archive.apache.org/dist/httpd/httpd-2.4.4.tar.gz [iyunv@Apache_Server src]# tar zxfhttpd-2.4.4.tar.gz [iyunv@Apache_Server src]# cd httpd-2.4.4 [iyunv@Apache_Server httpd-2.4.4]# ./configure --prefix=/usr/local/httpd-2.4.4--enable-so --enable-rewrite --enable-ssl --enable-cgi --enable-cgid--enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all--with-mpm=event --with-zlib--with-pcre --with-apr=/usr/local/apr-1.4.6/ --with-apr-util=/usr/local/apr-util/ [iyunv@Apache_Server httpd-2.4.4]# 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/:指定apr-util路径 --enable-mpms-shared=all :支持多道处理模块 --with-mpm=event :设定默认的模块 在编译过程中可能会报两个错 报错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 install pcre-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)优化执行路径 [iyunv@Apache_Server httpd-2.4.4]# cd/usr/local/ [iyunv@Apache_Server local]# ln -shttpd-2.4.4/ httpd [iyunv@Apache_Server local]# ln -sf/usr/local/httpd-2.4.4/bin/* /usr/bin/ [iyunv@Apache_Server local]# 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 操作步骤:
[iyunv@Apache_Server ~]#cp/usr/local/httpd-2.4.4/bin/apachectl /etc/init.d/httpd [iyunv@Apache_Server ~]# sed -i '1a #description:Apache Controller Script'/etc/init.d/httpd [iyunv@Apache_Server ~]# sed -i '2a #chkconfig:- 85 15' /etc/init.d/httpd [iyunv@Apache_Server ~]# chmod a+x/etc/init.d/httpd [iyunv@Apache_Server ~]# head -3/etc/init.d/httpd #!/bin/sh #description:Apache Controller Script #chkconfig:-85 15 [iyunv@Apache_Server ~]# ll/etc/init.d/httpd -rwxr-xr-x 1 root root 3512 12月16 12:38 /etc/init.d/httpd
4.4)启动Apache [iyunv@Apache_Server ~]# service httpdstart [iyunv@Apache_Server ~]# netstat -anput |grep httpd tcp 0 0 :::80 :::* LISTEN 50106/httpd [iyunv@Apache_Server ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 50106 root 4u IPv6 51433 0t0 TCP *:http (LISTEN) httpd 50107 daemon 4u IPv6 51433 0t0 TCP *:http (LISTEN) httpd 50108 daemon 4u IPv6 51433 0t0 TCP *:http (LISTEN) httpd 50109 daemon 4u IPv6 51433 0t0 TCP *:http (LISTEN) [iyunv@Apache_Server ~]# chkconfig --addhttpd [iyunv@Apache_Server ~]# chkconfig httpdon [iyunv@Apache_Server ~]# chkconfig --list| grep httpd httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
提示:需要关闭防火墙iptables和禁用selinux,否则Apache可能会启动报错
客户端测试:
补充:使用apachectl作为启动脚本发现,启动Apache后并没有对结果进行显示,只能通过查看端口才能判断Apache是否启动成功,现在写一个脚本,可以出现像系统服务一样的带有结果的脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| #!/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 //控制选项,允许使用符号链接
AllowOverride None //不允许隐含控制文件中的覆盖配置
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 不可覆盖
|