< 返回技术文档列表

Apache入门篇(一)之安装部署apache

发布时间:2021-01-25 22:28:17⊙投诉举报

Apache入门篇(一)之安装部署apache

一、HTTPD特性

(1)高度模块化:core(核心) + modules(模块) = apache(2)动态模块加载DSO机制: Dynamic Shared Object(动态共享对象)(3)MPM:Multipath Processing Modules(多路解决模块)多种不同的IO解决模型
Aapche的三种工作模型:

prefork:2.2版本默认的模型,多进程模型,每个进程响应一个请求;进程之间彼此独立,每个子进程只有一个线程,最稳固。缺点:内存消耗较大一个主进程:负责生成n个子进程,子进程也称为工作进程,每个子进程解决一个客户请求;即使没有客户请求,也会预先生成多个空闲进程,随时等待请求到达;最大不会超过1024个;主进程监听套接字,在请求到来时fork出一个子进程去解决该请求。

worker:多线程模型,每个线程响应一个请求;
一个主进程:生成多个子进程,每个子进程负责生个多个线程,每个线程响应一个请求;m进程,n线程:m*n优点:线程之间内存共享,系统内存利用率较高,内存消耗比prefork小的多,适用于高流量的http服务器。缺点:一个进程崩溃整个进程,整个进程里面的线程一律一起挂掉。
event:事件驱动模型,每个线程响应n个请求;
一个主进程:生成m个子进程,每个进程直接n个请求;m*n在非常繁忙的服务器下,以上两种模型都不再适用,在work基础上,event把服务进程从连接中分离出来。服务器解决速度很快,同时具备非常高的点击率时,可用的线程数量就是关键的资源限制,此时Event MPM方式是最有效的在2.2版本的httpd中,三种模式的切换需要重新编译,2.4版本后,无需编译,直接修改配置导入module就可。

二、Centos 7.4编译安装部署Apache 2.2.31

①下载安装包[root@longlinux ]#  cd /usr/local/src            //切换到下载目录[root@longlinux src]# wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.31.tar.gz ②解压[root@longlinux src]# tar -jxvf httpd-2.2.31.tar.bz2    //解压 ③配置编译参数[root@longlinux src]# cd httpd-2.2.31 [root@longlinux httpd-2.2.31]# ./configure \ --prefix=/usr/local/apche2 \         //指定安装位置--with-included-apr \                   //httpd依赖的包apr,可支持httpd跨平台运作--enable-so \                               //表示启用DSO--enable-deflate=shared \           //表示动态共享的方式编译deflate,后面参数同理--enable-expires=shared \ --enable-rewrite=shared \ --with-pcre                                 //正则相关的库 配置时出现了如下错误:error: mod_deflate has been requested but can not be built due to prerequisite failures因为缺少了zlib-devel库,需要安装:[root@longlinux httpd-2.2.31]# yum install -y zlib-develTips:  DSO是Dynamic Shared Objects(动态共享目标)的缩写,它提供了一种在运行时将特殊格式的代码在程序运行需要时,将需要的部分从外存调入内存执行的方法。Apache 支持动态共享模块,也支持静态模块,静态的话,会把需要的目标直接编译进apache的可执行文件中,相比较动态,尽管省去了加载共享模块的步骤,但是也加大了二进制执行文件的空间,变得臃肿。④编译为了避免编译的时候出错,需事前安装好少量库文件[root@longlinux httpd-2.2.31]# yum install pcre pcre-devel apr apr-devel gcc gcc++ [root@longlinux httpd-2.2.31]# make ⑤安装[root@longlinux httpd-2.2.31]# make install ⑥启动服务 </pre>[root@longlinux httpd-2.2.31]# /usr/local/apche2/bin/apachectl -t   //检查语法配置文件的语法[root@longlinux httpd-2.2.31]# /usr/local/apche2/bin/apachectl start //启动httpd服务 启动的时候出现了如下错误,需要修改配置文件 /usr/local/apache2/conf/httpd.conf [root@longlinux httpd-2.2.31]# vim /usr/local/apache2/conf/httpd.conf 搜索‘#ServerName’ ,增加ServerName localhost:80*[root@longlinux httpd-2.2.31]# /usr/local/apache2/bin/apachectl restart* //再重新启动apche就可/usr/local/apache2/bin/apachectl -M 查看动态模块 /usr/local/apache2/bin/apachectl -l 查看静态模块 /usr/local/apache2/bin/apachectl -t 查看配置文件/usr/local/apache2/conf/httpd.conf能否存在错误语法

三、Centos7.4安装部署Apache 2.4.33

(1)Yum方式安装

[root@localhost ~]# yum install -y httpd[root@localhost ~]# systemctl enable httpd[root@localhost ~]# systemctl start httpd</pre>

(2)源码方式安装

①下载httpd-2.4.33源码包

[root@localhost ~]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.33.tar.gz[root@localhost ~]# tar -zxvf httpd-2.4.33.tar.gz

②编译安装

[root@localhost httpd-2.4.33]# ./configure --prefix=/usr/local/apache-2.4.33 \   #指定安装位置 --with-included-apr \  #httpd依赖的包apr,可支持httpd跨平台运作 --enable-so \    #启用DSO --enable-deflate=shared \   #动态共享的方式编译deflate,后面的参数同理 --enable-mods-shared=most \ --enable-rewrite=shared \--enable-mpms-shared \  #2.4版本使用动态更换mpm模式必需使用该参数 --with-pcre  #正则相关的库[root@localhost httpd-2.4.33]# make && make install

③编译过程报错处理

apache 2.4版本后,需要提前安装apr和apr-util的包以处理依赖关系,以及处理正则库依赖,不然会有以下报错:错误1:error: mod_deflate has been requested but can not be built due to prerequisite failures处理:yum install -y zlib-devel错误2:configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr andapr-util packages to ./srclib/.处理:[root@localhost ~]# wget http://archive.apache.org/dist/apr/apr-util-1.5.1.tar.gz[root@localhost ~]# wget http://archive.apache.org/dist/apr/apr-1.5.1.tar.gz[root@localhost ~]# cp -rf apr-util-1.5.1 httpd-2.4.33/srclib/apr-util[root@localhost ~]# cp -rf apr-1.1.1 httpd-2.4.33/srclib/apr错误3:/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_GetErrorCode' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetEntityDeclHandler' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ParserCreate' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetCharacterDataHandler' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ParserFree' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetUserData' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_StopParser' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_Parse' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ErrorString' /usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetElementHandler' collect2: error: ld returned 1 exit status make[2]: *** [htpasswd] Error 1make[2]: Leaving directory `/usr/local/httpd-2.4.26/support' make[1]: *** [all-recursive] Error 1make[1]: Leaving directory `/usr/local/httpd-2.4.26/support' make: *** [all-recursive] Error 1 处理:更换apr,apr-util的版本,前期用了1.6版本报错,更换了1.5版本后可正常编译

④Apache启动

[root@localhost httpd-2.4.33]# /usr/local/apache-2.4.33/bin/apachectl -t   #apache配置文件语法检测Syntax OK[root@localhost httpd-2.4.33]# /usr/local/apache-2.4.33/bin/apachectl start   #apache启动[root@localhost httpd-2.4.33]# ps -ef |grep httpd    #查看apache进程

root 32457 1 0 12:34 ? 00:00:00 /usr/local/apache-2.4.33/bin/httpd -k start
daemon 32458 32457 0 12:34 ? 00:00:00 /usr/local/apache-2.4.33/bin/httpd -k start
daemon 32459 32457 0 12:34 ? 00:00:00 /usr/local/apache-2.4.33/bin/httpd -k start
daemon 32460 32457 0 12:34 ? 00:00:00 /usr/local/apache-2.4.33/bin/httpd -k start
root 32567 34939 0 12:36 pts/1 00:00:00 grep --color=auto httpd
root 34201 34186 0 03:35 ? 00:00:03 httpd -DFOREGROUND
bin 34242 34201 0 03:35 ? 00:00:00 httpd -DFOREGROUND
bin 34243 34201 0 03:35 ? 00:00:00 httpd -DFOREGROUND
bin 34244 34201 0 03:35 ? 00:00:00 httpd -DFOREGROUND

[root@localhost httpd-2.4.33]# netstat -tulnp |grep 80   #查看apache能否监听80端口 tcp6 0      0 :::80                   :::*                    LISTEN      32457/httpd   
[root@localhost httpd-2.4.33]# curl localhost    #访问apache

<html><body><h1>It works!</h1></body></html>


/template/Home/Zkeys/PC/Static