• 独立Web站点的快速部署


                                                                  独立Web站点的快速部署

     

    1案例1:独立Web站点的快速部署

    1.1问题

    本例要求为http://server0.example.com配置Web站点,要求如下:

    http://classroom/pub/materials/station.html下载一个主页文件,将其重命名为index.html

    将此文件拷贝到站点的DocumentRoot目录下,不要对文件index.html的内容作任何修改

    使用elinksfirefox浏览上述Web站点

    1.2方案

    Web网站服务端:软件包httpd、系统服务httpd

    Web网站浏览器:软件包elinksfireox

    传输协议及端口:TCP 80

    Web网站服务端配置文件:

    /etc/httpd/conf/httpd.conf

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

    默认首页文件:index.html

    httpd网站文档的默认根目录:/var/www/html

    URLUniform Resource Locator,统一资源定位器)网址的基本组成:

    http://服务器地址[:端口号]/目录/文件名

    对于需要验证的FTP资源,还需要指定用户名密码信息:

    ftp://用户名:密码@服务器地址[:端口号]/目录/文件名

    1.3步骤

    实现此案例需要按照如下步骤进行。

    步骤一:构建及部署网站服务器

    1)安装软件包httpd

    [root@server0~]#yum -y install httpd

    ....

    2)部署网页

    [root@server0~]#cd /var/www/html///进入网页目录

    [root@server0 html]#wget http://classroom/pub/materials/station.html-O index.html//下载网页

    ....

    2016-11-26 19:33:49(1.36 MB/s)-index.htmlsaved[14/14]

    [root@server0 html]#cat index.html//检查网页文件

    Default Site.

    3)启动系统服务httpd,并设置开机自启

    [root@server0 html]#systemctl restart httpd

    [root@server0 html]#systemctl enable httpd

    ln-s'/usr/lib/systemd/system/httpd.service''/etc/systemd/system/multi-user.target.wants/httpd.service'

    步骤二:访问网站服务器

    1)使用elinks浏览器查看

    Elinks浏览器可以在命令行模式显示出网页文本,经常用来测试网站的可用性。

    [root@desktop0~]#yum -y install elinks//安装elinks

    ....

    [root@desktop0~]#elinks -dump http://server0.example.com///访问指定网址

    Default Site.

    2)使用firefox浏览器查看

    Firefox浏览器支持更多网页特性,是访问复杂网页、网址的优秀工具。

    在桌面终端直接运行firefox http://server0.examle.com/”,或者通过菜单快捷方式打开Firefox浏览器再输入对应网址,都可以看到目标网页(如图-1所示)。

    -1


     案例2:虚拟Web主机的部署

    2.1问题

    本例要求为server0扩展Web站点,新建虚拟主机http://www0.example.com,具体要求如下:

    设置DocumentRoot/var/www/virtual

    http://classroom/pub/materials/www.html下载主页文件,并重命名为index.html

    不要对文件index.html的内容作任何修改,将其放到此虚拟主机的DocumentRoot目录下

    确保fleyd用户能在/var/www/virtual目录建文件

    确保站点http://server0.example.com仍然可用

    2.2方案

    单一网站平台(比如172.25.0.11):

    多个域名--->相同的网页内容

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

    网页目录定义:DocumentRoot/var/www/html

    虚拟主机平台(比如172.25.0.11):

    在同一套httpd平台上跑很多个网站

    多个域名--->不同的网页内容

    网页目录由<VirtualHost...>区段配置定义

    多个虚拟主机站点的典型设置(/etc/httpd/conf.d/*.conf):

    <VirtualHost*:80>

    ServerName网站1FQDN

    DocumentRoot网站1的网页根目录

    </VirtualHost>

    <VirtualHost*:80>

    ServerName网站2FQDN

    DocumentRoot网站2的网页根目录

    </VirtualHost>

    ....

    2.3步骤

    实现此案例需要按照如下步骤进行。

    步骤一:部署网页文档

    1)建立网页目录

    [root@server0~]#mkdir /var/www/virtual

    [root@server0~]#useradd fleyd

    [root@server0~]#setfacl -m u:fleyd:rwx /var/www/virtual/

    2)部署网页文件

    [root@server0~]#cd /var/www/virtual/

    [root@server0 virtual]#wget http://classroom/pub/materials/www.html-O index.html

    ....

    100%[=====================>]14--.-K/s in 0s

    2016-11-26 20:01:14(826 KB/s)-index.htmlsaved[14/14]

    [root@server0 virtual]#cat index.html//检查网页文件

    Virtual Site.

    步骤二:配置虚拟主机http://www0.example.com/

    1)为新站点创建独立的配置文件

    [root@server0 virtual]#vim /etc/httpd/conf.d/01-www0.conf

    <VirtualHost*:80>

    ServerName www0.example.com

    DocumentRoot/var/www/virtual

    </VirtualHost>

    [root@server0 virtual]#httpd -t //确保语法检查OK

    Syntax OK

    2)重启系统服务httpd

    [root@server0 virtual]#systemctl restart httpd

    步骤三:访问虚拟主机http://www0.example.com/

    访问此虚拟站点,可以看到预期的网页内容:

    [root@desktop0~]#elinks -dump http://www0.example.com/

    Virtual Site.

    步骤四:完善原始站点http://server0.example.com/

    需要注意的是,原始的独立站点可能出现异常,访问时并不是原始的网页:

    [root@desktop0~]#elinks -dump http://server0.example.com/

    Virtual Site.

    原因是一旦启用虚拟站点机制以后:

    外部的DocumentRootServerName会被忽略

    1个虚拟站点被视为默认站点,若客户机请求的URL不属于任何已知站点,则由第1个站点响应

    若要解决此异常,需要将原始站点转换为第一个虚拟主机,启用顺序的设置可以通过文件名开头的数字来实现。

    1)为原始站点建立虚拟主机配置

    [root@server0~]#vim /etc/httpd/conf.d/00-default.conf

    <VirtualHost*:80>

    ServerName server0.example.com

    DocumentRoot/var/www/html

    </VirtualHost>

    2)重启系统服务httpd

    [root@server0 virtual]#systemctl restart httpd

    3)访问两个虚拟站点,确保各自的网页内容正确

    [root@desktop0~]#elinks -dump http://server0.example.com/

    Default Site.

    [root@desktop0~]#elinks -dump http://www0.example.com/

    Virtual Site.


     案例3:配置网页内容访问

    3.1问题

    本例要求在Web网站http://server0.example.comDocumentRoot目录下创建一个名为private的子目录,要求如下:

    http://classroom/pub/materials/private.html下载一个文件副本到这个目录,重命名为index.html

    不要对文件index.html的内容作任何修改

    server0上,任何人都可以浏览private的内容,但是从其他系统不能访问这个目录的内容

    3.2方案

    配置Web内容的访问控制需要添加Directory区段,主要形式可参考

    <Directory"父目录路径">

    Require all denied//上层目录拒绝任何访问

    </Directory>

    <Directory"子目录1路径">

    Require all granted//子目录1允许任何访问

    </Directory>

    <Directory"子目录2路径">

    Require ip IP或网段地址....//子目录2允许少数客户机

    </Directory>

    3.3步骤

    实现此案例需要按照如下步骤进行。

    步骤一:部署网页子目录及文档

    1)建立子目录

    [root@server0~]#mkdir /var/www/html/private

    2)部署网页

    [root@server0~]#cd /var/www/html/private

    [root@server0private]#wget http://classroom/pub/materials/private.html-O index.html

    ....

    2016-11-26 20:30:28(1.90 MB/s)-index.htmlsaved[14/14]

    [root@server0 private]#cat index.html//检查网页文件

    Private Site.

    步骤二:为指定的网页子目录限制访问

    httpd服务的标准配置中,根目录/默认拒绝任何访问,但网页目录/var/www/默认允许任何访问。因此,只需要为个别子目录增加访问控制即可。

    1)调整虚拟站点server0.example.com的配置文件

    [root@server0~]#vim /etc/httpd/conf.d/00-default.conf

    ....

    <Directory"/var/www/html/private">

    Require ip 127.0.0.1::1 172.25.0.11

    </Directory>

    2)重启系统服务httpd

    [root@server0~]#systemctl restart httpd

    步骤三:测试目录访问限制

    1)从desktop0上访问http://server0.example.com/private/被拒绝

    [root@desktop0~]#elinks -dump http://server0.example.com/private/

    Forbidden

    You don't have permission to access/private/on this server.

    2)从desktop0上访问http://server0.example.com/仍然是正常的

    [root@desktop0~]#elinks-dump http://server0.example.com/

    Default Site.

    3)server0本机上访问http://server0.example.com/private/

      /也不受 限制

    [root@server0~]#elinks -dump http://server0.example.com/private/

    Private Site.


     案例4:使用自定Web根目录

    4.1问题

    本例要求调整Web站点http://server0.example.com的网页目录,要求如下:

    新建目录/webroot,作为此站点新的网页目录

    http://classroom/pub/materials/station.html下载一个文件副本到这个目录,重命名为index.html

    不要对文件index.html的内容作任何修改

    确保站点http://server0.example.com仍然可访问

    4.2方案

    SELinux强制启用模式下,增加新的合规网页目录的方法:

    1)参照标准目录,重设新目录的属性

    chcon[-R]--reference=模板目录新目录

    或者

    2)将新目录增加到预设的标准Web目录范围

    semanage fcontext-a-t httpd_sys_content_t'新目录(/.*)?'

    4.3步骤

    实现此案例需要按照如下步骤进行。

    步骤一:部署网页目录及文档

    1)建立网页目录

    [root@server0~]#mkdir /webroot

    2)部署网页文件

    [root@server0~]#cd /webroot/

    [root@server0webroot]#wget http://classroom/pub/materials/station.html-O index.html

    ....

    2016-11-26 20:01:14(826 KB/s)-index.htmlsaved[14/14]

    [root@server0 webroot]#cat index.html//检查网页文件

    Default Site.

    步骤二:调整虚拟站点http://server0.example.com/的配置

    1)修改配置文件

    [root@server0~]#vim /etc/httpd/conf.d/00-default.conf

    <VirtualHost*:80>

    ServerName server0.example.com

    DocumentRoot/webroot

    </VirtualHost>

    ....

    2)重启系统服务httpd

    [root@server0~]#systemctl restart httpd

    步骤三:确保虚拟站点http://server0.example.com/仍然可以访问

    1)未调整网页目录SELinux上下文件的情况

    为虚拟站点http://server0.example.com/更换了新的网页目录以后,从浏览器访问将会失败,只能看到红帽测试页。

    [root@desktop0~]#elinks -dump http://server0.example.com/

    Red Hat Enterprise Linux Test Page

    This page is used to test the proper operation of the Apache HTTP server

    after it has been installed.If you can read this page,it means that the

    Apache HTTP server installed at this site is working properly.

    ....

    针对此问题,可以参考目录/var/www的属性为网页目录/webroot设置SELinux安全上下文

    [root@server0~]#chcon -R --reference=/var/www/webroot/

    [root@server0~]#ls -Z /webroot/index.html//确认结果

    -rw-r--r--.root root system_u:object_r:httpd_sys_content_t:s0/webroot/index.html

    2)未配置目录内容访问的情况

    尽管已经调整过/webrootSELinux安全上下文,但是从浏览器访问此虚拟站点时仍然会被拒绝,还是只能看到红帽测试页。

    还需要修改对应的配置文件,添加内容访问控制:

    [root@server0~]#vim /etc/httpd/conf.d/00-default.conf

    <VirtualHost*:80>

    ServerName server0.example.com

    DocumentRoot/webroot

    </VirtualHost>

    <Directory"/webroot">

    Require all granted

    </Directory>

    <Directory"/webroot/private">

    Require ip 127.0.0.1::1 172.25.0.11

    </Directory>

    [root@server0~]#systemctl restart httpd//重启httpd服务

    若要保持原有private子目录,建议也拷贝过来:

    [root@server0~]#cp -rf /var/www/html/private//webroot/

    3)最终访问测试

    从浏览器能成功访问调整后的虚拟站点http://server0.example.com/

    [root@desktop0~]#elinks -dump  http://server0.example.com/

    Default Site.

  • 相关阅读:
    java中equals与hashCode的重写问题
    关于java的二维码的生成与解析
    bootstrap的时间控件使用(双日历)
    Mysql表,列,库的增删查改
    关于js重名方法的先后调用问题
    javascript的比较运算符
    javascript的运算符
    JavaScript的数据类型
    javascript的基本语法
    MAP集合
  • 原文地址:https://www.cnblogs.com/qingbai/p/11939992.html
Copyright © 2020-2023  润新知