http 协议介绍
http: Hyper Text Transfer Protocol 超文本传输协议,是互联网应用最为广泛的一种网络协议,主要用于 Web 服务。通过计算机处理文本信息,格式为 HTML(Hyper Text Mark Language)超文本标记语言来实现
http 协议的版本
http 0.9:仅于用户传输 html 文档
http 1.0
1.引入了 MIME(Multipurpose Internet Mail Extesions)机制:多用途互联网邮件扩展,引入这个技术之后, http 可以发送多媒体(比如视频、音频等)信息。此机制让 http不在单单只支持 html 格式,还可以支持其他格式来进行发送了。
2.引入了 keep-alive 机制,支持持久连接的功能(但这个 keep-alive 原理是在首部添加了某个字段而形成的,并非原生就支持此功能)
3.引入支持缓存功能
http 1.1
支持更多的请求方法,更加精细的缓存控制,原生直接支持持久连接功能(presistent)。
http 2.0 提供了 HTTP 语义优化的传输, spdy : google 引入了的一个技术,能够加速 http 数据交互,尤其是使用 ssl 加速机制,但是 spdy 现在用的还不多。
目前常用的版本就是 http 1.0 版本和 http 1.1 版本
Apache安装部署:
1、检查是否已安装rpm包httpd
[root@apache ~]# rpm -q httpd #查看是否安装
[root@apache ~]# rpm -e httpd --nodeps #卸载已安装的程序
2、安装前提软件
如果编译安装无法执行,可能是开发软件工具没有安装,需要先安装开发软件:
[root@apache ~]# yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel
把所需要用到的软件复制到虚拟机的/usr/src目录下或直接下载到虚拟机
编写脚本安装前提软件:
[root@apache ~]# mkdir /sh
[root@apache ~]# vim /sh/qianti.sh
#!/bin/bash
cd /usr/src
tar zxf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr && make && make install
cd ..
tar zxf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
cd ..
yum -y install zlib-*
tar zxf pcre-8.39.tar.gz
cd pcre-8.39
./configure --prefix=/usr/local/pcre && make && make install
cd ..
tar zxf openssl-1.0.1u.tar.gz
cd openssl-1.0.1u
./config -fPIC --prefix=/usr/local/openssl enable-shared && make && make install
[root@apache ~]# sh qianti.sh
3、安装Apache主程序
[root@apache ~]# vim /sh/httpd.sh
#!/bin/bash
cd /usr/src
tar zxf httpd-2.4.25.tar.gz
cd httpd-2.4.25
./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
[root@apache ~]# sh /sh/httpd.sh
4、优化链接
[root@apache ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin
添加系统服务
[root@apache ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@apache ~]# vim /etc/init.d/httpd
# 定位到第二行:修改为
# chkconfig: 35 85 15 #声明服务启动级别,开机启动顺序,关机关闭顺序
# description: apache 2.4.25 #服务声明,简要信息
[root@apache ~]# chkconfig --add httpd #添加httpd到系统服务
[root@apache ~]# chkconfig httpd on #设置服务开机自启(等同于:systemctl enable httpd)
[root@apache ~]# systemctl start httpd #开启服务(等同于:service httpd start)
5、查看httpd模块
[root@apache ~]# httpd -V #查看版本和已装模块
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::bbcd:c290:2463:4c51. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.25 (Unix)
Server built: Jun 16 2020 02:04:43
Server's Module Magic Number: 20120211:67
Server loaded: APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr/local/httpd"
-D SUEXEC_BIN="/usr/local/httpd/bin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
[root@apache ~]# httpd -l #只查看静态编译模块
Compiled in modules:
core.c
mod_so.c
http_core.c
[root@apache ~]# httpd -M #查看所有模块
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::bbcd:c290:2463:4c51. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
authn_file_module (shared)
authn_core_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
authz_core_module (shared)
access_compat_module (shared)
auth_basic_module (shared)
reqtimeout_module (shared)
filter_module (shared)
mime_module (shared)
log_config_module (shared)
env_module (shared)
headers_module (shared)
setenvif_module (shared)
version_module (shared)
mpm_event_module (shared)
unixd_module (shared)
status_module (shared)
autoindex_module (shared)
dir_module (shared)
alias_module (shared)
6、MPM(Multi Process Modules):多进程处理模块
负责实现网络监听、请求的处理等功能,目的为了在不同的平台上实现最优化的性能和稳定性
操作系统平台 | MPM |
---|---|
BeOS | beos |
NetWare | mpm_netware |
OS/2 | mpm_os2 |
linux | prefork、worker、event |
Windows | mpm_winnt |
7、apache的工作模式
prefork模式 | 非线程、预生成进程型MPM,一个子进程同一时间点仅能处理一个用户请求,根据并发请求数动态调整子进程 |
---|---|
worker模式 | 线程化、多进程型MPM,每个进程可生成多个线程,每个线程处理一个请求,缺点:长连接,资源容易被占用 |
event模式 | worker的改进版,使用监控线程处理长连接出现的资源占用问题 |
8、修改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 #允许同时的最大接入请求数量
MaxConnectionsPerChild 0 #每个子进程可处理的请求数
</IfModule>
9、使用ab命令进行压力测试
yum -y install httpd-tools
ab -c 160 -n 10000 http://192.168.1.102/index.html #使用ab压力测试命令进行160人并发访问,发出10000个请求