Apache介绍
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。
静态资源与动态资源
静态资源:静态内容,客户端从服务器获得的资源的表现形式与原文件相同,格式为.html,.css,.jgp等
动态资源:通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端, .php .jsp等
长连接与短连接
长链接:连接建立,每个资源获取完成后不会断开连接,而是继续等待其他请求的完成
优点:只需要建立一次连接
缺点:如果请求资源的时间少于长连接的阈值,那么多余的时间会被浪费
如果请求资源的时间大于长连接的阈值,那么有些资源将访问不到
短链接:客户端和服务器每进行一次HTTP操作,就建立一次连接,任务结束就中断连接。
优点:当请求资源时,请求完毕立即断开
缺点:当请求资源较多时,那么需要不断的建立连接,断开连接,占用资源
优化方案:给长连接设置阈值,设置时间较短的长链接
http/1.0默认是使用短链接
http/1.1默认是使用长连接
HTTP状态码
HTTP状态码(英语:HTTP Status Code)是用以表示网页服务器超文本传输协议响应状态的3位数字代码。它由 RFC 2616 规范定义的,并得到 RFC 2518、RFC 2817、RFC 2295、RFC 2774 与 RFC 4918 等规范扩展。所有状态码的第一个数字代表了响应的五种状态之一。所示的消息短语是典型的,但是可以提供任何可读取的替代方案。 除非另有说明,状态码是HTTP / 1.1标准(RFC 7231)的一部分。
常见的状态码
200:请求已成功,请求所希望的响应头或数据体将随此响应返回。 301:请求的URL指向的资源已经被删除,但在响应报文中通过Location指明了资源现在所处的新位置 302:与301相似,但在响应报文中通过Location指明资源现在所处临时新位置 304:如果客户端发送了一个带条件的 GET 请求且该请求已被允许,而文档的内容(自上次访问以来或者根据请求的条件)并没有改变,则服务器应当返回这个状态码 401:需要输入账号密码访问资源 403:请求被禁止 404:服务器无法找到客户端请求的资源 500:服务器内部错误 502:代理服务器从后端服务器收到一条伪响应
安装apache服务
yum install httpd -y
apache配置文件路径
/etc/httpd/conf/httpd.conf
apache访问页面文件路径
/var/www/html/index.html
apache日志
apache日志一般分为两类,1.错误日志,2.访问日志
错误日志:一般存放apache所生成的错误信息
错误日志默认存放路径
/etc/httpd/logs/error_log
访问日志:记录着访问本网站的客户端信息,例如ip等
访问日志默认存放路径
/etc/httpd/logs/access_log
访问日志类型
combined和common:复合型和普通型
访问日志格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common
含义:
%h:远端主机 %I:远端登录名 %u:远程用户名 %t:时间 %r:请求第一行 %>s:状态 %b:传送字节 %{Referer}i\:请求来源 \"%{User-Agent}i\:客户端浏览器提供的浏览器识别信息
MPM模块
Multipath Process Module:多路处理模块,Apache 一共有3种稳定的 MPM 模式(多进程处理模块),它们分别是 prefork、worker、event。2.4版本的httpd默认是prefork工作模式。
而由于event不支持https,因此,企业里面很少使用event
-
prefork模式 (apache默认工作模式)
工作特点:
使用多个进程,每个进程只有一个线程,每个进程在某个确定的时间只能维持一个链接,优点是稳定,但内存开销较高
-
worker模式
工作特点:
使用多个进程,每个进程包含多个线程,每个线程在某个确定的时间只能维持一个链接,内存占用比较小,适合大并发,高流量的web服务器worker缺点是一个线程崩溃,整个进程就会连同其任何线程一起挂掉
-
event模式
不支持https
修改MPM模块来修改apache工作模式
查看apache当前工作模式 httpd -V
[root@localhost ~]# httpd -V AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this message Server version: Apache/2.4.6 (CentOS) #apache当前版本 Server built: Nov 16 2020 16:18:20 Server's Module Magic Number: 20120211:24 Server loaded: APR 1.4.8, APR-UTIL 1.5.2 Compiled using: APR 1.4.8, APR-UTIL 1.5.2 Architecture: 64-bit Server MPM: prefork #当前工作模式 threaded: no 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="/etc/httpd" -D SUEXEC_BIN="/usr/sbin/suexec" -D DEFAULT_PIDLOG="/run/httpd/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@localhost ~]#
修改MPM模块工作模式 vim /etc/httpd/conf.modules.d/00-mpm.conf
[root@localhost ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf # Select the MPM module which should be used by uncommenting exactly # one of the following LoadModule lines: # prefork MPM: Implements a non-threaded, pre-forking web server # See: http://httpd.apache.org/docs/2.4/mod/prefork.html LoadModule mpm_prefork_module modules/mod_mpm_prefork.so #当前配置文件只有这行没有注释,说明这行是生效的 # worker MPM: Multi-Processing Module implementing a hybrid # multi-threaded multi-process web server # See: http://httpd.apache.org/docs/2.4/mod/worker.html # #LoadModule mpm_worker_module modules/mod_mpm_worker.so #如果切换为worker 模式,将这行注释打开,把prefork那行注释掉 # event MPM: A variant of the worker MPM with the goal of consuming # threads only for connections with active processing # See: http://httpd.apache.org/docs/2.4/mod/event.html # #LoadModule mpm_event_module modules/mod_mpm_event.so
将配置文件修改后再httpd -V 查看工作模式
[root@localhost ~]# httpd -V
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.6 (CentOS)
Server built: Nov 16 2020 16:18:20
Server's Module Magic Number: 20120211:24
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: worker #模式已更改为worker
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="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="/run/httpd/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"
apache虚拟主机
当一台服务器上只运行一个apache时,当用户访问量不大时,会造成资源浪费,为了节省资源可以创建虚拟主机来运行多个apache的web服务,运行多个虚拟主机有三种实现方式
1、基于ip :为每个虚拟主机至少准备一个IP
2、基于端口号:为每个主机至少准备一个端口号
3、基于主机名:为每个主机至少准备一个主机名
创建虚拟主机可以混合使用上述三种任意方式
注意:一般创建虚拟主机时,不能和中心主机混用,使用虚拟主机时禁用中心主机。
禁用中心主机:注释DocumentRoot
每个虚拟主机都有专门的配置: vim /etc/httpd/conf/httpd.conf
<VirtualHost "IP:PORT"> ServerName: DocumentRoot </VirtualHost>
举例:vim /etc/httpd/conf/httpd.conf ,在文件最后追加
<VirtualHost 192.168.41.136:80> #基于IP创建虚拟主机 ServerName www.web1.com #虚拟主机名称 DocumentRoot "/var/www/web1" #网站主站点目录 ErrorLog logs/web1-error_log #错误日志路径 CustomLog logs/web1-access_log combiend #访问日志路径 <Directory "/var/www/html/web1"> #针对哪个目录做访问控制 Options None AllowOverride None <RequireAll> #访问控制权限设置
Require all granted #允许所有人访问 Require not ip 192.168.1.0/24 #不允许访问的IP段 </RequireAll> </Directory> </VirtualHost>
Listen 8080
<VirtualHost *:8080> #基于端口号创建虚拟主机 ServerName www.web2.com #虚拟主机名称 DocumentRoot "/var/www/web2" #网站主站点目录 ErrorLog logs/web2-error_log #错误日志路径 CustomLog logs/web2-access_log combiend #访问日志路径 <Directory "/var/www/html/web2"> #针对哪个目录做访问控制 Options None AllowOverride None <RequireAll> #访问控制权限设置
Require all granted #允许所有人访问 Require not ip 192.168.1.0/24 #不允许访问的IP段 </RequireAll> </Directory> </VirtualHost>
apache访问控制
apache有四种访问控制模式
1、基于站点访问控制
可基于两种类型的路径指明对哪些资源进行访问控制 文件系统控制: <Directory ""> </Directory> <File ""> </File> <FileMatch ""> </FileMatch> URL路径: <location ""> </location> ...
2、基于用户访问控制
基于用户访问控制 <Directory "/var/test"> AllowOverride None Options None AuthType Basic AuthName "this is admin page" AuthUserFile "/etc/httpd/conf.d/.htpasswd" Require user admin1 admin2 admin3 </Directory> 创建访问授权账号 -c:自动创建htpasswd文件,因此,仅应该在添加第一个用户时使用 -m:md5加密 -D:删除用户 htpasswd -c -m /etc/httpd/conf.d/.htpasswd admin1 htpasswd -m /etc/httpd/conf.d/.htpasswd admin1
3、基于来源地址访问控制
Directory中"基于来源地址"实现访问控制 (1)Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews None All Indexes: 如果没有默认主页面也找不到自定义页面会显示索引页面 (2)基于来源地址的访问控制机制 Require all granted Require all denied 来源地址: Require ip IPADDR Require not ip IPADDR IPADDR: 192.168 192.168.0.0 192.168.0.0/24 192.168.0.0/255.255.255.0 示例: <Directory "/var/www/admin"> AllowOverride None Options None #Require all granted Require not ip 192.168.254.0/24 </Directory>
4、基于组访问控制
基于组访问控制 <Directory "/var/test"> AllowOverride None Options None AuthType Basic AuthName "this is admin page" AuthUserFile "/etc/httpd/conf.d/.htpasswd" AuthGroupFile "/etc/httpd/conf.d/.htgroup" Require group webadmins </Directory>