Linux 网页内容访问和部署动态网站 、 安全Web服务(Engineer06----DAY13)

设置防火墙默认的区域:

虚拟机server
[root@server0 ~]# firewall-cmd --set-default-zone=trusted

虚拟机desktop
[root@desktop0 ~]# firewall-cmd --set-default-zone=trusted

########################################################
案例3:为虚拟机 server 配置以下虚拟Web主机

  • 实现三个网站的部署
  • 实现客户端访问server0.example.com网页内容为 卖女孩的小火柴
  • 实现客户端访问www0.example.com网页内容为 奔跑吧骆驼
  • 实现客户端访问webapp0.example.com网页内容为 20里春风不如你

[root@server0 ~]# yum -y install httpd
[root@server0 ~]# vim /etc/httpd/conf.d/nsd01.conf
<VirtualHost *:80>
ServerName server0.example.com
DocumentRoot /var/www/abc01

<VirtualHost *:80>
ServerName www0.example.com
DocumentRoot /var/www/abc02

<VirtualHost *:80>
ServerName webapp0.example.com
DocumentRoot /var/www/abc03

[root@server0 ~]# cd /var/www/
[root@server0 www]# mkdir abc01 abc02 abc03
[root@server0 www]# echo ‘

卖女孩的小火柴’ > abc01/index.html
[root@server0 www]# echo ‘

奔跑吧骆驼’ > abc02/index.html
[root@server0 www]# echo ‘

20里春风不如你’ > abc03/index.html
[root@server0 /]# systemctl restart httpd
###########################################################
配置目录访问

文件夹权限
• 针对 DocumentRoot 网页目录的权限控制
– httpd 运行身份(用户/组):apache

客户机地址限制
• 使用 配置区段
– 每个文件夹自动继承其父目录的ACL访问权限
– 除非针对子目录有明确设置
<Directory 目录的绝对路径>
… …
Require all denied|granted
Require ip 172.25.0.11 #仅允许该172.25.0.11地址访问

Require all denied #拒绝所有人访问

<Directory “/var/www”>
Require all granted #允许所有人访问

案例1:配置网页内容访问
在 Web 网站 http://server0.example.com 的
DocumentRoot 目录下创建一个名为 private 的子目录,要求如下:
1)查看server0.example.com 的DocumentRoot路径
]# cat /etc/httpd/conf.d/nsd01.conf
2)创建一个名为 private 的子目录
]# mkdir /var/www/abc01/private
]# echo ‘

wo shi private’ > /var/www/abc01/private/index.html
]# cat /var/www/abc01/private/index.html

3)从虚拟机server0上,任何人都可以浏览 private 的内容,
但是从其他系统不能访问这个目录的内容
参照主配置文件:/etc/httpd/conf/httpd.conf
[root@server0 ~]# vim /etc/httpd/conf.d/nsd02.conf
<Directory “/var/www/abc01/private”>
Require ip 172.25.0.11 #仅允许172.25.0.11进行访问

[root@server0 ~]# systemctl restart httpd

虚拟机desktop:测试访问
[root@desktop0 ~]# firefox server0.example.com/private
Forbidden

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

###########################################################
案例2:使用自定Web根目录
调整 Web 站点 http://server0.example.com 的网页目录,要求如下:
1)新建目录 /webroot,作为此站点新的网页目录
]# mkdir /webroot
]# echo ‘

wo shi webroot’ > /webroot/index.html
]# cat /webroot/index.html

]# vim /etc/httpd/conf.d/nsd01.conf
<VirtualHost *:80>
ServerName server0.example.com
DocumentRoot /webroot #修改网页文件目录


2)修改访问控制
[root@server0 ~]# vim /etc/httpd/conf.d/nsd02.conf

<Directory “/webroot”> #针对/webroot目录进行访问控制
Require all granted #允许所有人进行访问

[root@server0 ~]# systemctl restart httpd

3)SELinux策略:布尔值 安全上下文值 非默认端口的开放

安全上下文值:类似与标签 标识作用
]# getenforce
]# semanage fcontext -l | less #查看所有的上下文值
]# ls -Zd /var/www/ #查看目录的上下文值
]# ls -Zd /webroot/
• 方式1:参照标准目录,重设新目录的属性
– chcon [-R] --reference=模板目录 新目录

]# chcon -R --reference=/var/www /webroot
]# ls -Zd /webroot

正则表达式:以描述的语言,表达心中所想

#########################################################
部署动态网站

静态网站的运行
• 服务端的原始网页 = 浏览器访问到的网页
– 由Web服务软件处理所有请求
– 文本(txt/html)、图片(jpg/png)等静态资源

动态网站的运行
• 服务端的原始网页 ≠ 浏览器访问到的网页
– 由Web服务软件接受请求,动态程序转后端模块处理
– PHP网页、Python网页、JSP网页…

一、下载Python页面文件
为站点 webapp0.example.com 配置提供动态Web内容
[root@server0 ~]# cat /etc/httpd/conf.d/nsd01.conf
[root@server0 ~]# cd /var/www/abc03/
[root@server0 abc03]# wget http://classroom.example.com/pub/materials/webinfo.wsgi

[root@server0 abc03]# cat webinfo.wsgi

二、方便用户的访问,直接访问域名可以看到webinfo.wsgi
页面跳转:Alias 网络路径 实际呈现的网页文件路径

  网络路径 /:网页文件的根目录,匹配客户端浏览器中直接输入域名

]# vim /etc/httpd/conf.d/nsd01.conf

<VirtualHost *:80>
ServerName webapp0.example.com
DocumentRoot /var/www/abc03
Alias / /var/www/abc03/webinfo.wsgi
当客户端直接访问 网页文件根目录时,呈现/var/www/abc03/webinfo.wsgi

]# systemctl restart httpd
]# firefox webapp0.example.com

三、安装mod_wsgi软件,可以翻译执行Python页面的代码
[root@server0 /]# yum -y install mod_wsgi
[root@server0 /]# vim /etc/httpd/conf.d/nsd01.conf
<VirtualHost *:80>
ServerName webapp0.example.com
DocumentRoot /var/www/abc03
WsgiScriptAlias / /var/www/abc03/webinfo.wsgi

[root@server0 /]# systemctl restart httpd

[root@desktop0 ~]# firefox webapp0.example.com

页面内容为
UNIX时间戳:自1970-1-1 8:0:0 到达现在时间,所经历的秒数

四、此虚拟主机侦听在端口8909
[root@server0 /]# vim /etc/httpd/conf.d/nsd01.conf
Listen 8909 #设置httpd程序监听8909端口
<VirtualHost *:8909> #设置本虚拟Web主机将在8909端口呈现
ServerName webapp0.example.com
DocumentRoot /var/www/abc03
WsgiScriptAlias / /var/www/abc03/webinfo.wsgi

五、SELinux策略:修改非默认端口的开放策略
[root@server0 /]# getenforce
[root@server0 /]# semanage port -l | grep http
[root@server0 /]# semanage port -a -t http_port_t -p tcp 8909
-a:添加 -t:类型 -p:协议

[root@server0 /]# systemctl restart httpd

[root@desktop0 ~]# firefox webapp0.example.com:8909

###########################################################
安全Web服务

http:超文本传输协议
https:安全的超文本传输协议

PKI公钥基础设施
• Public Key Infrastructure,公钥基础设施
– 公钥:主要用来加密数据
– 私钥:主要用来解密数据(与相应的公钥匹配)
– 数字证书:证明拥有者的合法性/权威性(单位名称、有效期、公钥、颁发机构及签名、…)
– Certificate Authority,数字证书授权中心:负责证书
的申请/审核/颁发/鉴定/撤销等管理工作

虚拟机server:构建安全的Web,以www0.example.com为例
1.下载网站证书(营业执照)
]# cd /etc/pki/tls/certs/
]# wget http://classroom.example.com/pub/tls/certs/server0.crt
]# ls

2.下载根证书(工商局信息)
]# cd /etc/pki/tls/certs/
]# wget http://classroom.example.com/pub/example-ca.crt
]# ls

3.下载私钥(解密数据)
]# cd /etc/pki/tls/private/
]# wget http://classroom.example.com/pub/tls/private/server0.key

]# ls
4.安装软件,支持安全加密的通信
[root@server0 /]# yum -y install mod_ssl
[root@server0 /]# ls /etc/httpd/conf.d/

5.修改配置文件
[root@server0 /]# vim /etc/httpd/conf.d/ssl.conf
补充vim功能 末行模式:set nu #开启行号

59 DocumentRoot “/var/www/html” #取消注释
60 ServerName www0.example.com:443 #取消注释

指定网站证书
100 SSLCertificateFile /etc/pki/tls/certs/server0.crt
指定解密的私钥
107 SSLCertificateKeyFile /etc/pki/tls/private/server0.key
指定根证书
122 SSLCACertificateFile /etc/pki/tls/certs/example-ca.crt

[root@server0 /]# systemctl restart httpd
访问测试
[root@desktop0 ~]# firefox https://www0.example.com
我已充分了解可能的风险-------》添加例外------》确认安全例外
###########################################################
补充内容:Samba共享服务,专门用于Windows与Linux跨平台的共享

反向编译

虚拟机server:
1.安装samba软件
[root@server0 /]# yum -y install samba

2.建立Samba共享用户(帐号):专用于访问samba共享的时候,验证的用户
基于本地用户,需要设置Samba共享独立的密码
[root@server0 /]# useradd dc
[root@server0 /]# pdbedit -a dc #将用户添加为Samba共享用户
new password:
retype new password:

[root@server0 /]# pdbedit -L
dc:1001:

3.创建samba共享的目录
[root@server0 /]# mkdir /nsd1911
[root@server0 /]# echo haha > /nsd1911/a.txt
[root@server0 /]# ls /nsd1911/

4.修改配置文件
[root@server0 /]# vim /etc/samba/smb.conf
命令模式下按大写的G到全文的最后一行

[共享名]
path = 实际共享的目录路径

[nsd]
path = /nsd1911

[root@server0 /]# systemctl restart smb

5.修改SELinux策略:布尔值(功能的开关)
[root@server0 /]# getsebool -a | grep samba #查看布尔值
[root@server0 /]# setsebool samba_export_all_ro on #修改布尔值
[root@server0 /]# getsebool -a | grep samba

虚拟机desktop:
1.安装cifs-utils软件,支持挂载samba共享
[root@desktop0 ~]# yum -y install cifs-utils

2.挂载访问
NFS共享的格式:mount IP地址:/共享的路径 挂载点目录
samba共享的格式:
mount -o user=dc,pass=123 //IP地址/共享名 挂载点目录

]# mount -o user=dc,pass=123 //172.25.0.11/nsd /mnt
]# df -h
]# ls /mnt

3.开机自动挂载
[root@desktop0 /]# vim /etc/fstab
//172.25.0.11/nsd /mnt cifs
defaults,user=dc,pass=123,_netdev 0 0

[root@desktop0 ~]# umount /mnt
[root@desktop0 ~]# df -h
[root@desktop0 ~]# mount -a
[root@desktop0 ~]# df -h

#########################################################

发布了55 篇原创文章 · 获赞 0 · 访问量 430

猜你喜欢

转载自blog.csdn.net/weixin_45533230/article/details/103715924