当前位置: 首页 > news >正文

Linux基本服务——web服务解析

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 目录

    Web服务解析

    虚拟Web主机

    Web目录访问控制


    Web服务解析

  • 用途:基于 B/S 架构提供网页的服务端程序

    应用层协议:HTTP(TCP 80)

    软件包(S):httpd

    软件包(B):firefox、IE、curl

    检查配置语法:httpd -t

    如何访问一个网站(URL) —— Uniform Resource Locator,网址(统一资源定位器)

    http://www.baidu.com/

    http://music.baidu.com/mp3/huluwa.mp3

    协议名://服务器地址:端口/目录路径/文件

    服务器默认网页从哪来 ——

    网页根目录: /var/www/html/

    配置文件:/etc/httpd/conf/httpd.conf

    /etc/httpd/conf.d/*.conf

    默认首页:index.html

    httpd.conf基础设置 ——

    Listen  "监听地址:端口"

    DocumentRoot    "网页根目录的绝对路径"

    DirectoryIndex  "网站目录下默认提供的第一个网页(首页)的名称"

    ServerName  "网站的FQDN完整域名"

    ServerAlias "网站域名的别名"

    ServerAdmin "网站管理员的电子邮箱地址"

    为 server 快速部署Web服务

    1)装包

    [root@server ~]# dnf  -y  install  httpd

    2)配置

    [root@server ~]# vim /etc/httpd/conf/httpd.conf

    ServerAdmin  webmaster@example.com        //设置网站管理邮箱

    [root@server ~]# echo  'Hello Class!'  > /var/www/html/index.html    //创建默认首页

    3)起服务

    [root@server ~]# firewall-cmd --permanent  --add-service=http

    [root@server ~]# firewall-cmd --reload

    [root@server ~]# systemctl  enable  httpd  --now

    4)从浏览器访问网页

    [client]# curl  http://server.lab.example.com/

    Hello Class!

    访问Web站点时,服务器端给出的常见反馈:

    ++ HTTP OK(200),网页正常响应

    ++ Not Found(404),浏览器请求的网页没找到(真的没这个文件,或者URL网址错了)

    ++ Forbbiden(403),拒绝提供xx网页

    虚拟Web主机

    含义:在一台httpd服务器上提供多个不同的站点

    基于域名的虚拟主机:

    http://server.lab.example.com

    http://web1.lab.example.com

    一旦启用虚拟Web主机以后 ——

    1. 全局配置当中的DocumentRoot和ServerName会被忽略

    2. 如果客户机请求的URL不属于任何一个已知的虚拟站点,那么使用第一个虚拟站点做回应

    配置要点:

    <VirtualHost  *:80>

    ServerName   web1.example.com

    ServerAlias  example.com

    DocumentRoot  /var/www/web1

    CustomLog  "logs/web1-vhost.log"  combined

    ServerAdmin  web1-admin@example.com

    </VirtualHost>

    练习:在 server 上配置虚拟Web主机

    !!!! 当浏览器请求 http://web1.lab.example.com/时,页面显示 "web1"

    !!!! 当浏览器请求 http://server.lab.example.com/时,页面显示 "Coming Soon!"

    1)准备网页目录

    [root@server ~]# mkdir -p /srv/{default,web1}/www

    [root@server ~]# echo 'Coming Soon!' > /srv/default/www/index.html

    [root@server ~]# echo 'web1' > /srv/web1/www/index.html

    [root@server ~]# restorecon -Rv /srv/

    2)配置虚拟Web主机

    [root@server ~]# vim  /etc/httpd/conf.d/vhosts.conf

    <VirtualHost _default_:80>

    ServerName  server.lab.example.com

    DocumentRoot  /srv/default/www

    </VirtualHost>

    <VirtualHost *:80>

    ServerName  web1.lab.example.com

    ServerAlias  lab.example.com

    DocumentRoot  /srv/web1/www

    </VirtualHost>

    <Directory /srv/*/www>

    Require all granted

    </Directory>

    [root@server ~]# systemctl restart httpd

    3)从客户机访问虚拟Web主机

    [client]# vim  /etc/hosts

    192.168.4.98   server.lab.example.com  server  web1.lab.example.com  lab.example.com

    [client]# curl  http://web1.lab.example.com/

    web1

    [client]# curl  http://lab.example.com

    web1

    [client]# curl  http://server.lab.example.com/

    Coming Soon!

    [client]# curl  http://192.168.4.98/

    Coming Soon!

    Web目录访问控制

    Web目录访问的受控因素:

    ++ 基本权限rwx + ACL权限 + 运行用户(apache)

    ++ SELinux策略(目录角色 -t  httpd_sys_content_t、端口开放 http_port_t)

    # chcon  -R  -t  httpd_sys_content_t  /web1new/

    或者

    # semanage fcontext -a -t httpd_sys_content_t  '/web1new(/.*)?'

    # restorecon  -Rv  /web1new/

    ++ 服务配置策略(httpd.conf ==> <Directory  .....>)

    禁止任何人访问根目录(默认设置)

    <Directory />

    AllowOverride none

    Require all denied

    </Directory>

    允许任何人访问

    <Directory  目录路径>

    Require all granted

    </Directory>

    只允许个别网络或IP地址访问

    <Directory  目录路径>

    Require  ip  IP地址  网段IIP .. ..

    </Directory>

    获取httpd配置手册 ——

    # dnf  -y  install  httpd-manual

    # systemctl  restart  httpd

    # firefox  http://server.lab.example.com/manual/

    练习:调整 server 网站目录授权

    ++ 为站点 server.lab.example.com 的网页目录下创建子目录 private

    ++ 在 private 子目录下部署网页 index.html

    ++ 只允许从 server 本机或 serverb 的浏览器访问 private 目录及网页

    ++ 禁止其他任何主机访问 private 子目录

    关键配置:

    [root@server ~]# vim  /etc/httpd/conf.d/vhosts.conf

    <VirtualHost  *:80>

    ServerName  server.lab.example.com

    DocumentRoot  /srv/default/www

    </VirtualHost>

    <Directory /srv/*/www>

    Require all granted

    </Directory>

    <Directory  /srv/default/www/private>

    Require ip 127.0.0.1  192.168.4.98  192.168.4.101

    </Directory>

    [root@server ~]# systemctl  restart  httpd

http://www.dtcms.com/a/309595.html

相关文章:

  • 如何管理数据足迹,实现SAP S/4HANA的无缝迁移
  • Solana: 逐行解读 solana-test-validator 输出, 本地节点日志完全指南
  • oracle备库主机断电重启后IO异常报错
  • 【C#学习Day16笔记】XML文件、 事件Event 、Json数据
  • Sqlserver备份恢复指南-完整备份恢复
  • 从零到英雄:掌握神经网络的完整指南
  • Qt Quick 自定义组件开发
  • 江协科技STM32 11-4 SPI通信外设
  • Android SDK 版本差异与兼容方案:从适配到实践
  • gitlab 开发人员无法创建分支,管理员配置分支权限
  • flutter-boilerplate-project 学习笔记
  • 嵌入式学习笔记-MCU阶段--DAY09
  • STM32-ESP8266Wi-Fi模块使用USART实现通信/创建AP和STA模式配置教程(寄存器版)
  • 从0开始学习R语言--Day64--决策树回归
  • 流式编程的中间操作
  • 机器学习sklearn:随机森林的决策树
  • 低通滤波器的原理以及作用
  • C# 引用外部项目
  • 切比雪夫不等式
  • 网页从点击到显示:前端开发视角下的旅程
  • 在SQL SERVER 中如何用脚本实现每日自动调用存储过程
  • 大模型开发框架LangChain之构建知识库
  • 高速公路桥梁安全监测系统解决方案
  • 技术栈:基于Java语言的搭子_搭子社交_圈子_圈子社交_搭子小程序_搭子APP平台
  • 安全专家发现利用多层跳转技术窃取Microsoft 365登录凭证的新型钓鱼攻击
  • 【C#学习Day14笔记】泛型、集合(数组列表Arraylist、列表list)与字典
  • Python 中的可迭代、迭代器与生成器——从协议到实现再到最佳实践
  • 最新docker国内镜像源地址大全
  • AttributeError: ChatGLMTokenizer has no attribute vocab_size
  • 强反光干扰下识别率↑89%!陌讯多模态融合算法在烟草SKU识别的实战解析