Centos7系统下httpd各种方式实现与配置

  • 1、 Centos7系统下实现httpd-2.2的安装,并分别实现prefork、worker、event等几种工作方式

  • Centos 7上若yum安装httpd程序,默认的是2.4的版本,因此无法用yum 直接安装,我这里采取源码安装httpd-2.2
    安装前准备

    [[email protected] ~]# systemctl stop firewalld.service
    [[email protected] ~]# vi /etc/sysconfig/selinux
    [[email protected] ~]# setenforce 0
    [[email protected] ~]# getenforce
    Permissive

    安装相应组件包

    [[email protected] ~]# yum groupinstall "Development Tools" "Serverplatform Development" -y
    [[email protected] ~]# wget http://archive.apache.org/dist/httpd/httpd-2.2.32.tar.gz

    编译安装

    [[email protected] ~]# tar -zxf httpd-2.2.32.tar.gz
    [[email protected] ~]# cd httpd-2.2.32
    [[email protected] httpd-2.2.32]# ./configure --prefix=/usr/local/apache2/ --sysconfdir=/etc/httpd2 --with-mpm=worker
    [[email protected] httpd-2.2.32]# make && make install

    设定环境变量与systemd

    [[email protected] httpd-2.2.32]# cat /etc/profile.d/httpd.sh
    export PATH=$PATH:/usr/local/apache2/bin
    [[email protected] httpd-2.2.32]# ln -sv /usr/local/apache2/include /usr/include//httpd
    ‘/usr/include//httpd’ -> ‘/usr/local/apache2/include’
    [[email protected] httpd-2.2.32]# cat /etc/man_config
    MANPATH /usr/local/apache2/man
    [[email protected] httpd-2.2.32]# cat /lib/systemd/system/httpd.service
    [Unit]
            Description=The httpd service
            After=network.target
            [Service]
            Type=forking
            ExecStart=/usr/local/apache2/bin/apachectl start
            ExecReload=/bin/kill -s HUP $MAINPID
            ExecStop=/usr/local/apache2/bin/apachectl stop
            Restart=/usr/local/apache2/bin/apachectl restart
            [Install]
            WantedBy=multi-user.target

    启动服务与验证

    [[email protected] httpd-2.2.32]# systemctl daemon-reload
    [[email protected] httpd-2.2.32]# systemctl start httpd.service
    [[email protected] httpd-2.2.32]# ss -tan
    State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port
    LISTEN      0      128                              *:22                                           *:*
    LISTEN      0      100                      127.0.0.1:25                                           *:*
    ESTAB       0      0                    192.168.10.10:22                                192.168.10.1:57790
    LISTEN      0      128                             :::80                                          :::*
    LISTEN      0      128                             :::22                                          :::*
    LISTEN      0      100                            ::1:25                                          :::*
    [[email protected] httpd-2.2.32]# httpd -l
    Compiled in modules:
      core.c
      mod_authn_file.c
      mod_authn_default.c
      mod_authz_host.c
      mod_authz_groupfile.c
      mod_authz_user.c
      mod_authz_default.c
      mod_auth_basic.c
      mod_include.c
      mod_filter.c
      mod_log_config.c
      mod_env.c
      mod_setenvif.c
      mod_version.c
      worker.c
      http_core.c
      mod_mime.c
      mod_status.c
      mod_autoindex.c
      mod_asis.c
      mod_cgid.c
      mod_negotiation.c
      mod_dir.c
      mod_actions.c
      mod_userdir.c
      mod_alias.c
      mod_so.c

    按照以上方式即可实现prefork和event两种方式(默认为prefork方式)

    ./configure --prefix=/usr/local/apache2/ --sysconfdir=/etc/httpd --with-mpm=prefork
    ./configure --prefix=/usr/local/apache2/ --sysconfdir=/etc/httpd --with-mpm=event

    prefork: 预先创建进程,两级进程模型,父进程负责创建子进程,每个子进程响应一个用户请求
    worker:父进程管理子进程,子进程通过线程响应用户请求,每线程处理一个用户请求
    event:两级模型,父进程管理子进程,子进程通过event-driver机制直接响应n个请求

    • 2、 简述request报文请求方法和状态响应码

    • 请求方法(method):
      GET:从服务器获取一个资源;
      HEAD:只从服务器获取文档的响应首部;
      POST:向服务器发送要处理的数据;
      PUT:将请求的主体部分存储在服务器上;
      DELETE:请求删除服务器上指定的文档;
      TRACE:追踪请求到达服务器中间经过的代理服务器;
      OPTIONS:请求服务器返回对指定资源支持使用的请求方法;
    • Status(状态码):
      1xx: 100-101,信息提示;
      2xx: 200-206,成功
      3xx: 300-305,重定向
      4xx: 400-415,错误类信息,客户端错误
      5xx: 500-505,服务器端错误
    • 常用的状态码:
      200: 成功,请求的所有数据通过响应报文的entity-body部分发送;OK
      301: 请求的URL指向的资源的已经被删除;但在响应报文中通过首部Location指明了资源现在所处的新位置;Moved Permanently
      302: 与301相似,但在响应报文中通过Location指明资源现在所处临时新位置;Found
      304: 客户端发出了条件式请求,但服务器上的资源未曾发生改变,则通过响应此响应状态码通知客户端;Not Modified
      401: 需要输入账号和密码认证方能访问资源;Unauthorzed
      403: 请求被禁止;Forbidden
      404: 服务器无法找到客户端请求的资源;Not Found
      500: 服务器内部错误;Internal Server Error
      502: 代理服务器从后端服务器收到了一条伪响应; Bad Gateway
    • 3、详细描述httpd虚拟主机、站点访问控制、基于用户的访问控制、持久链接等应用配置实例

    虚拟主机的实现方案:
    基于IP地址
    基于端口号(port)
    基于主机域名(FQDN)

    注意点:

    1. 一般虚拟主机不要与中心主机混用,要使用虚拟主机,得先禁用“main”主机,禁用方法:注释中心主机的DocumentRoot指令即可
    2. 配置VirtualHost,在httpd2.2中,NameVirtualHost这一项需启用2.2以上版本不需要
    • 基于IP地址

      [[email protected] ~]# yum -y install httpd
      [[email protected] ~]# ip addr add 192.168.10.30/24 dev ens32
      [[email protected] ~]# ip addr add 192.168.10.31/24 dev ens32
      [[email protected] ~]# mkdir -p /var/www/html/30
      [[email protected] ~]# mkdir -p /var/www/html/31
      [[email protected] ~]# echo "hello,ip address is "192.168.10.30"" > /var/www/html/30/index.html
      [[email protected] ~]# echo "hello,ip address is "192.168.10.31"" > /var/www/html/31/index.html
      [[email protected] ~]# vi /etc/httpd/conf.d/virtualhost.conf
      <VirtualHost 192.168.10.30:80>
      DocumentRoot "/var/www/html/30"
      ServerName www.magedu30.com
      <Directory "/var/www/html/30">
      AllowOverride None
      Require all granted
      </Directory>
      </VirtualHost>
      <VirtualHost 192.168.10.31:80>
      DocumentRoot "/var/www/html/31"
      ServerName www.magedu31.com
      <Directory "/var/www/html/31">
      AllowOverride None
      Require all granted
      </Directory>
      </VirtualHost>
      [[email protected] ~]# httpd -t
      Syntax OK
      [[email protected] ~]# systemctl restart httpd.service
      #最后测试结果
      [email protected] ~]# curl 192.168.10.30
      hello,ip address is 192.168.10.30
      [[email protected] ~]# curl 192.168.10.31
      hello,ip address is 192.168.10.31
    • 基于端口号:
      [[email protected] ~]# mkdir -p /var/www/html/80
      [[email protected] ~]# mkdir -p /var/www/html/10080
      [[email protected] ~]# echo "hi,the ip port is ‘80‘" >/var/www/html/80/index.html
      [[email protected] ~]# echo "hi,the ip port is ‘10080‘" >/var/www/html/10080/index.html
      [[email protected] ~]# vi /etc/httpd/conf.d/test1.conf
      <VirtualHost 192.168.10.10:80>
          ServerName www.magedu10.com
          DocumentRoot "/var/www/html/80"
          <Directory "/var/www/html/80">
                   Options None
                   AllowOverride None
                   Require all granted
           </Directory>
           CustomLog "logs/test1_access_log"  combined
      </VirtualHost>
      [[email protected] ~]# vi /etc/httpd/conf.d/test2.conf
      Listen 10080
      <VirtualHost 192.168.10.10:10080>
          ServerName www.test2.com
          DocumentRoot "/var/www/html/10080"
          <Directory "/var/www/html/10080">
                 Options None
                 AllowOverride None
                 Require all granted
          </Directory>
          CustomLog "Logs/test2_access_log"  combined
      </VirtualHost>
      [email protected] ~]# httpd -t
      Syntax OK
      [[email protected] ~]# systemctl restart httpd
      #最后验证结果:
      [[email protected] ~]# curl 192.168.10.10:80
      hi,the ip port is ‘80‘
      [[email protected] ~]# curl 192.168.10.10:8080
      hi,the ip port is ‘10080‘
    • 基于主机域名
      [[email protected] ~]# mkdir -p /var/www/html/ilinux
      [[email protected] ~]# mkdir -p /var/www/html/iunix
      [[email protected] ~]# echo "domain name is ‘www.ilinux.com‘" >/var/www/html/ilinux/index.html
      [[email protected] ~]# echo "domain name is ‘www.iunix.com‘" >/var/www/html/iunix/index.html
      [[email protected] ~]# vi /etc/httpd/conf.d/virtualhost.conf
      <VirtualHost 192.168.10.10:80>
      DocumentRoot "/var/www/html/ilinux"
      ServerName www.ilinux.com
      <Directory "</var/www/html/ilinux">
      AllowOverride None
      Require all granted
      </Directory>
      </VirtualHost>
      <VirtualHost 192.168.10.10:80>
      DocumentRoot "/var/www/html/iunix"
      ServerName www.iunix.com
      <Directory "/var/www/html/iunix">
      AllowOverride None
      Require all granted
      </Directory>
      </VirtualHost>
      [[email protected] ~]# cat /etc/hosts
      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
      ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
      192.168.10.10 www.ilinux.com www.iunix.com
      [[email protected] ~]# httpd -t
      Syntax OK
      [[email protected] ~]# systemctl restart httpd
      #最后测试结果:
      [[email protected] ~]# curl www.ilinux.com
      domain name is ‘www.ilinux.com‘
      [[email protected] ~]# curl www.iunix.com
      domain name is ‘www.iunix.com‘
    • 站点访问控制
      禁止192.168.10.20访问
      [[email protected] ~]# cat /etc/httpd/conf.d/deny.conf
      <VirtualHost 192.168.10.10:80>
          ServerName www.ilinux.com
          DocumentRoot "/var/www/html"
          <Directory "/var/www/html">
                 <Requireall>
                         Require all granted
                         Require not ip 192.168.10.20
                 </Requireall>
           </Directory>
      </VirtualHost>
    • 基于用户的访问控制
      [[email protected] ~]# htpasswd -c /tmp/test.users tom
      New password:
      Re-type new password:
      Adding password for user tom
      [[email protected] ~]# htpasswd -m /tmp/test.users jerry
      New password:
      Re-type new password:
      Adding password for user jerry
      [[email protected] ~]# htpasswd -m /tmp/test.users xiaochen
      New password:
      Re-type new password:
      Adding password for user xiaochen
      [[email protected] ~]# mv /tmp/test.users /etc/httpd/conf.d/.htpasswd
      [[email protected] ~]# mkdir -p /var/www/html/testusers
      [[email protected] ~]# echo "Testusers Area" > /var/www/html/testusers/index.html
      [[email protected] ~]# cat /etc/httpd/conf.d/testusers.conf
      <Directory "/var/www/html/testusers">
          Options None
          AllowOverride None
          AuthType basic
          AuthName "Test Area,pls enter your username and password"
          AuthUserFile "/etc/httpd/conf.d/.htpasswd"
          Require user tom jerry obama
      </Directory>
      [[email protected] ~]# cat /etc/httpd/conf.d/virtualhost.conf
      <VirtualHost 192.168.10.10:80>
      DocumentRoot "/var/www/html/testusers"
      ServerName www.ilinux.com
      <Directory "</var/www/html/testusers">
      AllowOverride None
      Require all granted
      </Directory>
      </VirtualHost>
      <VirtualHost 192.168.10.10:80>
      DocumentRoot "/var/www/html/testusers"
      ServerName www.iunix.com
      <Directory "/var/www/html/testusers">
      AllowOverride None
      Require all granted
      </Directory>
      </VirtualHost>
      [[email protected] ~]# httpd -t
      Syntax OK
      [[email protected] ~]# systemctl restart httpd

      最后测试:

    • 持久链接
      持久链接是建立链接后持续获取资源不断开,一直响应到把需要获取的资源都成功获取了以后才终止链接
      #创建模块化文件
      [[email protected] ~]# cat /etc/httpd/conf.d/keepalive.conf
      KeepAlive On
      KeepAliveTimeout 35
      MaxKeepAliveRequests 100
      [[email protected] ~]# httpd -t
      Syntax OK
      [[email protected] ~]# systemctl restart httpd

    原文地址:http://blog.51cto.com/13929964/2318367

    时间: 2024-10-08 17:22:12

    Centos7系统下httpd各种方式实现与配置的相关文章

    CentOS7系统下YUM安装安装Mongodb 3.4

    CentOS7系统下YUM安装安装Mongodb 3.4 第一步 查看是否存在Mongodb配置yum源 切换到yum目录 cd /etc/yum.repos.d/ 查看文件 ls 第二部 不存在添加yum 源 创建文件 touch mongodb-3.4.repo 编辑该文件 vi mongodb-3.4.repo 内容 : cat /etc/yum.repos.d/mongodb-3.4.repos [mongodb-org-3.4] name=MongoDB Repository base

    centos7系统下nginx安装并配置开机自启动操作

    准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcre-devel zlib-devel ##创建工作目录并进入工作目录 mkdir -p /z/nginx && cd /z/nginx ##获取nginx最新的安装包 wget http://nginx.org/download/nginx-1.11.10.tar.gz ##解压缩 tar z

    LINUX系统下Java和Scala的环境配置

    LINUX系统下Java和Scala的环境配置 最近,笔者在研究一个有关“自然语言处理”的项目,在这个项目中,需要我们用Spark进行编程.而Spark内核是由Scala语言开发的,所以在使用Spark之前,我们必须配置好Scala,而Scala又是运行在JVM上的,所以在配置Scala之前,先要配置好JDK.下面是我个人的一些总结. 我是在Win7系统下开的虚拟机,虚拟机的系统是CentOS7,在创建虚拟机完成后,它会自带一个OpenJDK,我的版本是这样的: 但是因为最开始不了解这个情况,所

    计算机图形学-mac系统下Xcode中OpenGL开发环境配置。

    mac系统下Xcode中OpenGL开发环境配置. 这学期有计算机图形学的课程,需要用到OpenGL,最近着手开始配置开发环境了,老师上课给的安装包都是基于windows系统的.网上也是windows上配置的教程比较多,Mac版的比较少.我综合了几个教程并自己总结,实践成功.特来分享配置过程.希望能帮到大家! 介绍 OpenGL(Open Graphics Library)是定义了一个跨编程语言,跨平台的编程接口规格的专业的图形程序接口.它用于三维图像(二维亦可),是一个功能强大,与硬件无关,调

    虚拟机centos7系统下安装hadoop ha和yarn ha(详细)

    一:基础环境准备 (一):虚拟机新建五个centos7系统(复制文件夹的方式) (二):角色分配 (三)按照角色分配表配置 (1)更改主机ip(自行查找),如果只是个人搭建玩一玩,可选择安装centos7图形界面操作比较简单 (2)更改主机名方法, vim /etc/hostname文件,修改成对应的主机名,保存(比如在192.168.254.2这台主机上,就修改成master).再执行hostname 主机名,进行设置. (3)在五个虚拟机系统中修改/etc/hosts文件,添加如下内容 19

    Centos7系统下修改主机名操作笔记

    习惯了在Centos6系统下修改主机名的操作,但是Centos7下修改主机名的操作却大不相同!操作笔记如下: 在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient),和灵活的(pretty): * "静态"主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名. * "瞬态"主机名是在系统运行时临时分配的主机名,例如,通过DHCP或mDNS服务器分配.静态主机名和瞬态主机名都遵从作为互联网域名同样的字

    在虚拟机环境(CentOS7系统)下将kubernetes中部署服务成功,但在虚拟机外部无法访问到服务

    在CentOS7环境下,kubernetes单机版环境,成功部署一个服务,在虚拟机中访问服务没问题,下面这样: curl http://172.27.73.26:8888/eureka-server/default/master {"name":"eureka-server","profiles":["default"],"label":"master","version&qu

    redis在windows和Linux系统下的下载、安装、配置

    1.下载redis安装包 在redis的官网只有Linux系统下的安装包,微软的GitHub上有提供windows版本的redis安装包 redis中文网:http://www.redis.cn/ 微软redis GitHub网址:https://github.com/MSOpenTech/redis/releases 链接:https://pan.baidu.com/s/1d3fCshRXT0UBYiNejbehMA 提取码:t4l72.Windows安装 将ZIP包解压到安装路径的redis

    windows系统下简单nodejs安装及环境配置

    相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼,这里不想谈太多的nodejs的相关信息.只说一下,windows系统下简单nodejs环境配置 相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼.有关nodejs的相关资料网上已经铺天盖地.由于它的高并发特性,造就了其特殊的应用地位. 国内目前关注最高,维护最好的一个关于nodejs的网站应该是http://www.cnodejs.org/  这里不想谈太多的nodejs