apache2.4

介绍

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 80
        GET / HTTP/1.1
        Host: HOSTNAME or IP

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

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

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:18
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
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 for use 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/24
          Require all granted 
        </RequireAll>
      </Directory>
    </VirtualHost> 

lamp架构

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

 

 

测试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();
?>

验证:

 

 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();
?>

测试

 

 ok,没问题

 

 

 

posted @ 2019-05-26 20:20  力王7314  阅读(593)  评论(0编辑  收藏  举报