apache2.4

摘要:
所有状态码的第一个数字代表了响应的五种状态之一。常见的状态码:200:请求已成功,请求所希望的响应头或数据体将随此响应返回。

介绍

Apache HTTP server是Apache软件基金会的一个开源的网页服务器,可以运行在几乎所有广泛使用的计算机平台上,由于跨平台和安全性被广泛使用,是目前最流行的web服务器软件之一,目前主流的web服务器软件包括Apache、Nginx、Lighttpd、IIS、Resin、Tomcat、Weblogic、jetty等

Apache服务器的特点是使用简单,速度快,性能稳定,可以作为负载均衡及代理服务器来使用

静态资源和动态资源

静态资源:静态内容,客户端从服务器获得的资源的表现形式与原文件相同

动态资源:通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端

状态码

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:代理服务器从后端服务器收到一条伪响应

持久连接和非持久连接

2.持久连接
        Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其他请求的完成
            如何断开?
                数量限制:100时间限制:可配置(http2.4可以配置毫秒)
            副作用:对并发访问量较大的服务器,持久连接功能会使有些请求得不到响应
            折中:较短的持久连接时间
    3.非持久连接
        http1.0默认是非持久连接
        http1.1默认是持久连接
        
        KeepAlive On|Off
        MaxKeepAlivedRequests #
        KeepAliveTimeout #
    4.测试语法
        telnet 192.168.254.12 80GET / HTTP/1.1Host: HOSTNAME or IP

长连接,一次连接好可以请求多个资源,直到等到设置的超时时间超时

apache2.4第1张

短连接,在请求一个资源后立刻关闭连接

apache2.4第2张

MPM

Multipath Process Module:多路处理模块,Apache一共有3种稳定的MPM模式(多进程处理模块),它们分别是prefork、worker、event。2.4版本的httpd默认是prefork工作模式。
而由于event不支持https,因此,企业里面很少使用event
  • prefork模式

工作特点:

使用多个进程,每个进程只有一个线程,每个进程在某个确定的时间只能维持一个链接,优点是稳定,但内存开销较高

  • worker模式

工作特点:

使用多个进程,每个进程包含多个线程,每个线程在某个确定的时间只能维持一个链接,内存占用比较小,适合大并发,高流量的web服务器worker缺点是一个线程崩溃,整个进程就会连同其任何线程一起挂掉

  • event模式

不支持https

修改MPM模块

[root@localhost modules]# vim /etc/httpd/conf.modules.d/00-mpm.conf
解开注释:LoadModule mpm_worker_module modules/mod_mpm_worker.so
然后重启服务:
[root@localhost modules]# systemctl restart httpd
查看:
[root@localhost modules]# httpd -V
AH00112: Warning: DocumentRoot [/www/web1/] does not exist
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
Server version: Apache/2.4.6(CentOS)
Server built: Aug 8 2019 11:41:18Server's Module Magic Number: 20120211:24
Server loaded: APR 1.4.8, APR-UTIL 1.5.2Compiled using: APR 1.4.8, APR-UTIL 1.5.2Architecture: 64-bit
Server MPM: worker
threaded: yes (fixed thread count)
forked: yes (variable process count)

安装apache2.4

[root@localhost modules]# yum install httpd -y
[root@localhost modules]# systemctl restart httpd

修改apache端口号

1.Listen:[ip]:PORT
    省略ip表示监听本机所有ip;Listen可以出现多次

定义默认主页面

DirectoryIndex index.html index.html.var

日志

apache日志一般分为两类,1.错误日志,2.访问日志

错误日志:一般存放apache所生成的错误信息

[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep "^Error"ErrorLog "logs/error_log"

访问日志:记录着访问本网站的客户端信息,例如ip等

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 
<IfModule log_config_module>#
    # The following directives define some format nicknames foruse with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""combined
    LogFormat "%h %l %u %t "%r" %>s %b"common

    <IfModule logio_module># You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O"combinedio
    </IfModule>
    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a <VirtualHost># container, they will be logged here.  Contrariwise, if you *do*# define per-<VirtualHost>access logfiles, transactions will be
    # logged therein and *not* in this file.
    #
    #CustomLog "logs/access_log"common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    CustomLog "logs/access_log"combined
</IfModule>

访问日志格式

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:客户端浏览器提供的浏览器识别信息

站点访问控制

站点访问控制
    可基于两种类型的路径指明对哪些资源进行访问控制
        文件系统控制:
            <Directory ""> </Directory>
            <File ""> </File>
            <FileMatch ""> </FileMatch>URL路径:
            <location ""> </location>...

基于"来源地址"的访问控制

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>

基于"用户"的访问控制

基于用户访问控制
        <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

基于"组"的访问控制

12.基于组访问控制
        <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>

虚拟主机

有三种实现方案:
            基于IP:
                为每个虚拟主机准备至少一个ip
            基于port:
                为每个虚拟主机准备至少一个port
            基于hostname:
                为每个虚拟主机准备至少一个hostname
            可以混合使用上述三种方式中任意方式
        note:一般虚拟主机不要和中心主机混用,所以要使用虚拟主机,请先禁用虚拟主机
            禁用中心主机:注释DocumentRoot
        每个虚拟主机都有专门配置:
            <VirtualHost "IP:PORT">ServerName:
                DocumentRoot
            </VirtualHost>
示例:
    <VirtualHost 192.168.254.12:80>ServerName www2.cl7.com
      DocumentRoot "/var/www/www2"ErrorLog logs/www2-error_log
      CustomLog logs/www2-access_log combiend
      <Directory "/var/www/html1">Options None
        AllowOverride None
        <RequireAll>Require not ip 192.168.1.0/24Require all granted 
        </RequireAll>
      </Directory>
    </VirtualHost> 

lamp架构

lamp架构是linux,apache,mysql,php的缩写,以下是架构图

apache2.4第3张

测试apache和php的连接

安装php和mysql的驱动程序

[root@localhost ~]# yum install php php-mysql

在/var/www/html下(如果没有修改路径的话)创建index.php,添加如下内容

[root@localhost ~]# vim /var/www/html/index.php
<?php
    phpinfo();
?>

验证:

apache2.4第4张

ok,成功

测试php和mysql(mariadb)的连接

在/var/www/html下(如果没有修改路径的话)创建index.php,添加如下内容:

[root@localhost ~]# vim /var/www/html/index.php
<?php
    $link=mysql_connect('127.0.0.1','root','root');
    if($link)
        echo "success!!!";
    else
        echo "failed!!!";
    mysql_close();
?>

测试

apache2.4第5张

ok,没问题

免责声明:文章转载自《apache2.4》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇第十二单元 硬盘分区、格式化及文件系统的管理一ColorBox常见问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

owaspbwa筆記

2.1.1 HTTP 基础知识(Http Basics) .............................................9 HTTP 是如何工作的呢?所有的 HTTP 传输都要遵循同样的通用格式(需要使用 IEWatch 或 WebScarab 类插件协助进行学习)。每个客户端的请求和服务端的响应都有三个部分:请 求或响应行、一...

Apache Shiro快速如门教程

第一部分 什么是Apache Shiro    1、什么是 apache shiro :   Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 如同 Spring security 一样都是是一个权限安全框架,但是与Spring Security相比,在于他使用了和比较简洁易懂的认证和授权方式。  ...

国内开源镜像站点汇总

国内开源镜像站点汇总 本项目迁移至新地址:https://gitee.com/gsls200808/chinese-opensource-mirror-site 一、站点版 (一)、企业站 网易:http://mirrors.163.com/ 搜狐:http://mirrors.sohu.com/(Ubuntu旧发行版同步被冻结,不同步Ubuntu新发...

linux下搭建lamp环境以及安装swoole扩展

一、CentOS 6.5/CentOS 6.9使用yum快速搭建LAMP环境 准备工作:先更新一下yum源  我安装的环境是:apache2.2.15+mysql5.5.60+php5.6.36 输入下面命令: yum -y update 1、安装Apache [root@localhost ~]# yum -y install httpd # 开机自启动...

Zk学习笔记——权限控制

参考:从Paxos到Zookeeper分布式一致性原理和实践 使用的zk依赖是cdh5.16.2的3.4.5 <!-- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zo...

Flume内存溢出错误

java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2367) at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.ja...