设为首页 收藏本站
查看: 1022|回复: 0

[经验分享] apache的介绍和安装详解

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-18 08:27:11 | 显示全部楼层 |阅读模式
apache的介绍和安装详解
apache介绍

介绍:

Apache:Apache HTTP Server是开源软件项目的杰出代表,基于标准的HTTP协议提供网页浏览服务。Apache可以运行在Windows,Linux,Unix等多种操作系统平台上

安装

apache默认在系统镜像里有,名为httpd包,可以用yum直接安装,也可以用源码包编译安装。源码包编译安装方式可以定制所需功能,加载特定的模块。所以生产环境下一般都是源代码编译安装

下面介绍怎么用源码报编译安装apache。版本为(httpd-2.4.4)

1)卸载系统中默认的httpd软件,若没有则忽略
                              wKiom1SRR9TCDSyVAAB6u22yeis458.jpg
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可能会启动报错


客户端测试:
wKioL1SRSDKTrfkTAACYRqaFNSw342.jpg
补充:使用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 不可覆盖



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-38198-1-1.html 上篇帖子: Apache配置禁止站点目录浏览 下篇帖子: apache虚拟主机的搭建
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表