提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
-
目录
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