Apache服务编译搭建

Apache工作模式

常用的网站服务有Apache、nginx、Tomcat。

Apache:时间较早,模块化设计,几乎可以运行在所有操作系统上,性能稳定;配置相对复杂,自身无法解析动态网页。

nginx:高性能、高并发的网站和反向代理服务器,也可做邮件代理,阿里再开发tengine应用于天猫和淘宝。

tomcat:java应用服务器,也是servlet容器,可以认为是Apache的扩展,可以独立运行,也可以和Apache合作。

Apache2.4版本

新特性:

1、MPM 支持在运行时装载;不过要开启这种特性,在编译安装要启用这三种功能;
–enable-mpms-shared=all --with-mpm=event

2、支持 event

3、支持异步读写

4、在每个模块及每个目录上指定日志级别

5、增强版的表达式分析器

6、每请求配置:,

7、毫秒级别的 keepalive timeout

8、基于 FQDN 的虚拟主机不再需要 NameVirtualHost 指令

9、支持使用自定义变量

Apache网站部署

注:本实验为单机部署,实验前确保网络能正常上网

配置文件及参数

/etc/httpd     服务目录

/etc/httpd/conf/httpd.conf     主配置文件

/var/www/htm     l网站数据目录

/var/log/httpd/access_log     访问日志

/var/log/httpd/error_log     错误日志

1.关闭selinux

输入getenforce 获取当前selinux状态

Enforcing为开启,Disabled为关闭

临时关闭 # sudo setenforce 0

永久关闭 # sudo vi /etc/sysconfig/selinux

改为

2.开放httpd的8080端口号

# `firewall-cmd --zone=public --add-port=8080/tcp --permanent`

重启防火墙

# `firewall-cmd --reload`

查看端口号是否开启

# firewall-cmd --query-port=8080/tcp

.3.检查是否已安装rpm包httpd

# rpm -q httpd \\查看是否安装

# rpm -e httpd --nodeps \\卸载已安装的程序

4.安装前提软件

把软件复制到虚拟机的/usr/src目录下或直接下载到虚拟机

如果编译安装无法执行,可能是开发软件工具没有安装,需要先安装开发软件:命令如下

# yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel expat-devel zlib-devel

5.下载http相关二进制软件包

# cd /usr/local/src/

# wget http://mirrors.gigenet.com/apache//apr/apr-1.6.5.tar.gz

# wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.6.1.tar.gz

# wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.gz

# yum -y install pcre-devel

解压安装包

# tar zxvf apr-1.6.5.tar.gz

# tar zxvf  apr-util-1.6.1.tar.gz

# tar zxvf  httpd-2.4.41.tar.gz

编译安装 apr-1.6.5

# ./configure --prefix=/usr/local/apr && make && make install

报错01:


```csharp
 ./configure --prefix=/usr/local/apr

checking build system type... x86_64-pc-linux-gnu

checking host system type... x86_64-pc-linux-gnu

checking target system type... x86_64-pc-linux-gnu

Configuring APR library

Platform: x86_64-pc-linux-gnu

checking for working mkdir -p... yes

APR Version: 1.6.3

checking for chosen layout... apr

checking for gcc... no

checking for cc... no

checking for cl.exe... no

configure: error: in `/usr/local/apr-1.6.3':

configure: error: no acceptable C compiler found in $PATH

See `config.log' for more details

[root@server-1 apr-1.6.3]# echo $?

1

 

解决办法:安装gcc

```csharp
yum install gcc -y

再次config编译成功

./configure --prefix=/usr/local/apr

make && make install

echo $?

0

编译安装 apr-util-1.6.1

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install

6.编译安装 httpd-2.4.41

# ./configure --prefix=/usr/local/httpd --enable-so --enable-cgi --enable-cgid --enable-ssl --with-ssl=/usr/local/openssl --enable-rewrite --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-fcgi --enable-expires --enable-deflate && make && make install

编译报错03:configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

解决办法:

yum install pcre-devel -y

再次执行./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most返回如下界面,表示configure编译成功

注:如果在编译过程中出现,缺少某个依赖包,解决思路

yum list |grep 包

然后安装devel关键字的软件包

yum install -y 软件包

make && make install 编译安装报错

查看apache服务状态,执行/usr/local/apache2.4/bin/apachectl命令后返回如下提示

[root@localhost apache2.4]# /usr/local/apache2.4/bin/apachectl

 

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

解决办法:
编译/usr/local/apache2.4/conf/httpd.conf文件,把参数 ServerName改为

ServerName localhost

注意:这里的localhost表示本机主机名,主机名可以自定义为其他
然后重启apache服务

[root@localhost src]# /usr/local/apache2.4/bin/apachectl restart

[root@localhost src]# /usr/local/apache2.4/bin/apachectl

httpd (pid 71429) already running

7.启动apache服务

# /usr/local/apache2.4/bin/apachectl start

查看apache服务进程

# ps aux |grep httpd

root       1463  3.0  0.2 253600  8828 ?        Ss   04:19   0:00 /usr/local/apache2.4/bin/httpd -k start

daemon     1464  2.0  0.2 540428  8916 ?        Sl   04:19   0:00 /usr/local/apache2.4/bin/httpd -k start

daemon     1465  2.0  0.2 540428  8916 ?        Sl   04:19   0:00 /usr/local/apache2.4/bin/httpd -k start

daemon     1470  1.0  0.2 540428  8912 ?        Sl   04:19   0:00 /usr/local/apache2.4/bin/httpd -k start

root       1549  0.0  0.0 112704   972 pts/1    R+   04:19   0:00 grep --color=auto httpd

8.优化链接

ln -s /usr/local/httpd/bin/* /usr/local/bin

添加系统服务

cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd

vim /etc/init.d/httpd

定位到第二行:修改为

# chkconfig: 35 85 15     \\声明服务启动级别,开机启动顺序,关机关闭顺序

# description: apache 2.4.25 \\服务声明,简要信息

 

sed -i '2i# chkconfig: 35 85 15' /etc/init.d/httpd

sed -i '3i# description: apache 2.4.25' /etc/init.d/httpd

chkconfig --add httpd \添加httpd到系统服务

chkconfig httpd on \设置服务开机自启(等同于:systemctl enable httpd)

systemctl start httpd \开启服务(等同于:service httpd start)

9.查看httpd模块

httpd -V \查看版本和已装模块

httpd -l \只查看静态编译模块

httpd -M \查看所有模块

10.MPM与prefork

MPM(Multi Process Modules):多进程处理模块

负责实现网络监听、请求的处理等功能,目的为了在不同的平台上实现最优化的性能和稳定性。

操作系统平台 MPM

BeOS beos

NetWare mpm_netware

OS/2 mpm_os2

linux prefork、worker、event

Windows mpm_winnt

prefork模式:

非线程、预生成进程型MPM,一个子进程同一时间点仅能处理一个用户请求,根据并发请求数动态调整子进程

worker模式:

线程化、多进程型MPM,每个进程可生成多个线程,每个线程处理一个请求,缺点:长连接,资源容易被占用

event模式:

worker的改进版,使用监控线程处理长连接出现的资源占用问题

11.修改mpm配置文件

vim /usr/local/httpd/conf/extra/httpd-mpm.conf

 

<IfModule mpm_event_module>

 

StartServers 3 #apache 启动时候默认开始的子进程数

 

MinSpareThreads 75 #最小空闲数量的工作线程

 

MaxSpareThreads 250 #最大空闲数量的工作线程

 

ThreadsPerChild 25 #每个子进程产生的线程数量

 

MaxRequestWorkers 400 #与 prefork 模式相同

 

MaxConnectionsPerChild 0 #与 prefork 模式相同

 

</IfModule>

 

12.使用ab命令进行压力测试

ab -c 160 -n 10000 http://192.168.1.102/index.html   

使用ab压力测试命令进行160人并发访问,发出10000个请求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北京Play

如有更多需要请多多关照

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值