Linux就该这么学——Day10

10.1 网站服务程序

Apache 程序是目前拥有很高市场占有率的 Web 服务程序之一,其跨平台和安全性广泛被认可且拥有快速、可靠、简单的API 扩展。

Apache 服务程序可以运行在Linux 系统、UNIX 系统甚至是Windows 系统中,支持基于IP、域名及端口号的虚拟主机功能,支持多种认证方式,集成有代理服务器模块、安全Socket 层(SSL),能够实时监视服务状态与定制日志消息,并有着各类丰富的模块支持。

Apache 服务程序的部署步骤:
第 1 步:把光盘设备中的系统镜像挂载到 /media/cdrom 目录。

[root@linuxprobe ~]# mkdir -p /media/cdrom
[root@linuxprobe ~]# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only

第 2 步:创建 Yum 仓库的配置文件,下述命令中具体参数的含义可参考4.1.4 小节。

[root@linuxprobe ~]# vim /etc/yum.repos.d/rhel7.repo
[rhel7]
name=rhel7
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0

第 3 步:安装 Apache 服务程序。

[root@linuxprobe ~]# yum install httpd
Loaded plugins: langpacks, product-id, subscription-manager
………………省略部分输出信息………………
Dependencies Resolved
===============================================================================
Package Arch Version Repository Size
===============================================================================
Installing:
httpd x86_64 2.4.6-17.el7 rhel 1.2 M
Installing for dependencies:
apr x86_64 1.4.8-3.el7 rhel 103 k
apr-util x86_64 1.5.2-6.el7 rhel 92 k
httpd-tools x86_64 2.4.6-17.el7 rhel 77 k
mailcap noarch 2.1.41-2.el7 rhel 31 k
Transaction Summary
===============================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 1.5 M
Installed size: 4.3 M
Is this ok [y/d/N]: y
Downloading packages:
………………省略部分输出信息………………
Complete!

第 4 步:启用 httpd 服务程序并将其加入到开机启动项中,使其能够随系统开机而运行,从而持续为用户提供Web 服务:

[root@linuxprobe ~]# systemctl start httpd
[root@linuxprobe ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

10.2 配置服务文件参数

httpd 服务程序的主要配置文件及存放位置:

文件名称存放位置
服务目录/etc/httpd
主配置文件/etc/httpd/conf/httpd.conf
网站数据目录/var/www/html
访问日志/var/log/httpd/access_log
错误日志/var/log/httpd/error_log

在 httpd 服务程序主配置文件中,最为常用的参数:

参数用途
ServerRoot服务目录
ServerAdmin管理员邮箱
User运行服务的用户
Group运行服务的用户组
ServerName网站服务器的域名
DocumentRoot网站数据目录
Directory网站数据目录的权限
Listen监听的IP 地址与端口号
DirectoryIndex默认的索引页页面
ErrorLog错误日志文件
CustomLog访问日志文件
Timeout网页超时时间,默认为300 秒

DocumentRoot 参数用于定义网站数据的保存路径,其参数的默认值是把网站数据存放到/var/www/html目录中;而当前网站普遍的首页面名称是index.html,因此可以向/var/www/html目录中写入一个文件,替换掉 httpd 服务程序的默认首页面,该操作会立即生效。

[root@linuxprobe ~]# echo "Welcome To LinuxProbe.Com" > /var/www/html/index.html
[root@linuxprobe ~]# firefox

而如果想把保存网站数据的目录修改为/home/wwwroot目录,该怎么操作呢?
第 1 步:建立网站数据的保存目录,并创建首页文件。

[root@linuxprobe ~]# mkdir /home/wwwroot
[root@linuxprobe ~]# echo "The New Web Directory" > /home/wwwroot/index.html

第 2 步:打开 httpd 服务程序的主配置文件,将参数 DocumentRoot(定义网站数据保存路径)修改为/home/wwwroot,将参数 Directory(定义目录权限)后面的路径也修改为/home/wwwroot

[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/home/wwwroot"

#
# Relax access to content within /var/www.
#
<Directory "/home/wwwroot">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
………………省略部分输出信息………………
[root@linuxprobe ~]#

第 3 步:重新启动 httpd 服务程序后,尝试访问 http://127.0.0.1/index.html 页面时,竟然发现页面中显示“Forbidden,You don’t have permission to access /index.html on this server.”。症结在 SELinux

[root@linuxprobe ~]# systemctl restart httpd
[root@linuxprobe ~]# firefox

10.3 SELinux 安全子系统

SELinux(Security-Enhanced Linux)是美国国家安全局在Linux 开源社区的帮助下开发的一个强制访问控制(MAC,Mandatory Access Control)的安全子系统。RHEL 7 系统使用 SELinux 技术的目的是为了让各个服务进程都受到约束,使其仅获取到本应获取的资源。

SELinux 服务有三种配置模式:

  • enforcing:强制启用安全策略模式,将拦截服务的不合法请求
  • permissive:遇到服务越权访问时,只发出警告而不强制拦截
  • disabled:对于越权的行为不警告也不拦截
    本书中的所有实验都是在强制启用安全策略模式下进行的,虽然禁用 SELinux 服务确实能够减少报错几率,但这在生产环境中相当不推荐。建议查看SELinux 服务主配置文件中定义的默认状态,如果是 permissivedisabled,建议赶紧修改为 enforcing
[root@linuxprobe ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

SELinux 服务的主配置文件中,定义的是 SELinux 的默认运行状态,可以将其理解为系统重启后的状态,因此更改后不会立即生效。可以使用 getenforce 命令获得当前 SELinux 服务的运行模式:

[root@linuxprobe ~]# getenforce
Enforcing

为了确认10.2 的修改结果确实是 SELinux 导致的,可以用 setenforce [0|1] 命令修改 SELinux 当前的运行模式(0 为禁用,1 为启用)。注意,这种修改只是临时的,在系统重启后就会失效。

[root@linuxprobe ~]# setenforce 0
[root@linuxprobe ~]# getenforce
Permissive
[root@linuxprobe ~]# firefox

再次刷新网页,就会看到正常的网页内容了。

把 SELinux 服务恢复到强制启用安全策略模式,然后使用 ls -Zd DIR 命令分别查看原始网站数据的保存目录与当前网站数据的保存目录是否拥有不同的SELinux 安全上下文值

[root@linuxprobe ~]# setenforce 1
[root@linuxprobe ~]# ls -Zd /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
[root@linuxprobe ~]# ls -Zd /home/wwwroot
drwxrwxrwx. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot

在文件上设置的 SELinux安全上下文 是由用户段角色段以及类型段等多个信息项共同组成的。其中,用户段 system_u 代表系统进程的身份,角色段 object_r 代表文件目录的角色,类型段 httpd_sys_content_t 代表网站服务的系统文件。

针对当前这种情况,可以使用 semanage 命令,将当前网站目录 /home/wwwroot 的 SELinux 安全上下文修改为跟原始网站目录的一样就可以了。

10.3.1 semanage 命令

semanage 命令用于管理 SELinux 的策略,格式为“semanage [选项] [文件]”。

semanage 命令不仅能够像传统 chcon 命令一样设置文件、目录的策略,还可以管理网络端口、消息接口。使用 ·semanage· 命令时,经常用到的几个参数及其功能如下所示:

  • -l 参数:用于查询
  • -a 参数:用于添加
  • -m 参数:用于修改
  • -d 参数:用于删除

可以向新的网站数据目录中新添加一条 SELinux安全上下文,让这个目录以及里面的所有文件能够被 httpd 服务程序所访问到:

[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*

执行上述设置之后,还无法立即访问网站,还需要使用 restorecon 命令让修改的 SELinux安全上下文生效。在使用 restorecon 命令时,可以加上 -Rv 参数对指定的目录进行递归操作,以及显示SELinux 安全上下文的修改过程。最后,再次刷新页面,就可以正常看到网页内容。

[root@linuxprobe ~]# restorecon -Rv /home/wwwroot/
restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->
unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:home_root_
t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
[root@linuxprobe ~]# firefox

【注】因为在 RHCSA、RHCE 或 RHCA 考试中,都需要重启机器后再执行判分脚本。因此,建议养成将所需服务添加到开机启动项中的习惯,比如这里就需要添加 systemctl enable httpd 命令。

10.4 个人用户主页功能

httpd 服务程序提供的个人用户主页功能可以让系统内所有的用户在各自的家目录中管理个人的网站。但是这个功能是默认关闭的。

第 1 步:编辑下面的配置文件,在 UserDir disabled 参数前面加上井号(#),表示让 httpd 服务程序开启个人用户主页功能;再把 UserDir public_html 参数前面的井号(#)去掉。(UserDir 参数表示网站数据在用户家目录中的保存目录名称,即public_html 目录)

[root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf
#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
#
# The path to the end user account 'public_html' directory must be
# accessible to the webserver userid. This usually means that ~userid
# must have permissions of 711, ~userid/public_html must have permissions
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a "403 Forbidden" message.
#
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
# UserDir disabled

#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disabled" line above, and uncomment
# the following line instead:
#
UserDir public_html
</IfModule>

#
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
#
<Directory "/home/*/public_html">
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
</Directory>

第 2 步:在用户家目录中建立用于保存网站数据的目录及首页面文件。另外,还需要把家目录的权限修改为755,保证其他人也有权限读取里面的内容。

[root@linuxprobe home]# su - linuxprobe
Last login: Fri May 22 13:17:37 CST 2017 on :0
[linuxprobe@linuxprobe ~]$ mkdir public_html
[linuxprobe@linuxprobe ~]$ echo "This is linuxprobe's website" > public_html/
index.html
[linuxprobe@linuxprobe ~]$ chmod -Rf 755 /home/linuxprobe

第 3 步:重新启动 httpd 服务程序,在浏览器的地址栏中输入网址,其格式为“网址/~用户名”,从理论上来讲就可以看到用户的个人网站了。不出所料的是,系统显示报错页面,这一定还是SELinux 惹的祸。

第 4 步:思考这次报错的原因是什么。httpd 服务程序在提供个人用户主页功能时,该用户的网站数据目录本身就应该是存放到与这位用户对应的家目录中的,所以应该不需要修改家目录的SELinux 安全上下文。除了安全上下文,还存在 Linux 域的概念。

Linux 域确保服务程序不能执行违规的操作,只能本本分分地为用户提供服务。httpd 服务中突然开启的个人用户主页功能到底有没有被SELinux 域默认允许呢?
接下来使用 getsebool 命令查询并过滤出所有与 HTTP 协议相关的安全策略。其中,off 为禁止状态,on 为允许状态。
可以看到个人用户主页功能 httpd_enable_homedirs --> off 是禁止状态。

[root@linuxprobe ~]# getsebool -a | grep http
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off
named_tcp_bind_http_port --> off
prosody_bind_http_port --> off

使用 setsebool 命令来修改 SELinux 策略中各条规则的布尔值。加上 -P 参数,让修改后的SELinux 策略规则永久生效且立即生效。

[root@linuxprobe ~]# setsebool -P httpd_enable_homedirs=on
[root@linuxprobe ~]# firefox

有时,网站的拥有者并不希望直接将网页内容显示出来,只想让通过身份验证的用户访客看到里面的内容,这时就可以在网站中添加口令功能了。
第 1 步:先使用 htpasswd 命令生成密码数据库。-c 参数表示第一次生成;后面再分别添加密码数据库的存放文件,以及验证要用到的用户名称(该用户不必是系统中已有的本地账户)。

[root@linuxprobe ~]# htpasswd -c /etc/httpd/passwd linuxprobe
New password:此处输入用于网页验证的密码
Re-type new password:再输入一遍进行确认
Adding password for user linuxprobe

第 2 步:编辑个人用户主页功能的配置文件/etc/httpd/conf.d/userdir.conf,重启 httpd 服务程序即可生效。

[root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf
#
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
#
<Directory "/home/*/public_html">
AllowOverride all
#刚刚生成出来的密码验证文件保存路径
authuserfile "/etc/httpd/passwd"
#当用户尝试访问个人用户网站时的提示信息
authname "My privately website"
authtype basic
#用户进行账户密码登录时需要验证的用户名称
require user linuxprobe
</Directory>
[root@linuxprobe ~]# systemctl restart httpd

此时,当用户再想访问某个用户的个人网站时,就必须要输入账户和密码才能正常访问了。【注意】验证时使用的账户和密码是用 htpasswd 命令生成的专门用于网站登录的口令密码,而不是系统中的用户密码。

10.5 虚拟主机功能

利用虚拟主机功能,可以把一台处于运行状态的物理服务器分割成多个“虚拟的服务器”。

Apache 的虚拟主机功能是服务器基于用户请求的不同IP 地址、主机域名或端口号,实现提供多个网站同时为外部提供访问服务的技术。用户请求的资源不同,最终获取到的网页内容也各不相同。

10.5.0 使用 Yum 仓库安装 screen 服务

第一步:挂载光盘
mkdir -p /media/cdrom
mount /dev/cdrom /media/cdrom

第二步:编辑/etc/fstab,使其永久生效

/dev/cdrom /media/cdrom iso9660 defaults 0 0

第三步:编辑yum仓库的配置文件/etc/yum.repos.d
cd /etc/yum.repos.d
vim rhel7.repo (名字随便起,repo结尾即可)

[rhel7]
name=rhel7
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0

第四步:安装 httpd 服务程序
yum install httpd

10.5.1 基于IP 地址

如果一台服务器有多个IP 地址,而且每个IP 地址与服务器上部署的每个网站一一对应,这样当用户请求访问不同的IP 地址时,会访问到不同网站的页面资源。而且,每个网站都有一个独立的IP 地址,对搜索引擎优化也大有裨益。因此以这种方式提供虚拟网站主机功能不仅最常见,也受到了网站站长的欢迎。

第一步:为主机设置多个IP地址
vi /etc/sysconfig/network-scripts/ifcfg-eno16777728

IPADDR0=192.168.10.10
IPADDR1=192.168.10.20
IPADDR2=192.168.10.30

第二步:重启网卡服务
systemctl restart network

第三步:检查3个IP地址的连通性
ping 192.168.10.10
ping 192.168.10.20
ping 192.168.10.30

第四步:创建3个目录用于保存站点数据
mkdir -p /home/wwwroot/10
mkdir -p /home/wwwroot/20
mkdir -p /home/wwwroot/30

第五步:修改 httpd 服务的配置文件【不区分大小写】
vim /etc/httpd/conf/httpd.conf

<VirtualHost 192.168.10.10>
DocumentRoot /home/wwwroot/10
ServerName www.linuxprobe.com
<Directory /home/wwwroot/10 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.20>
DocumentRoot /home/wwwroot/20
ServerName bbs.linuxprobe.com
<Directory /home/wwwroot/20 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.30>
DocumentRoot /home/wwwroot/30
ServerName tech.linuxprobe.com
<Directory /home/wwwroot/30 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

第六步:重启HTTP服务,并加入开机启动
systemctl restart httpd

第七步:手动修改网站数据目录的SELinux 安全上下文【注意:目录最后不可以加/】
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/10
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/10/*
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/20
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/20/*
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/30
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/30/*

第八步:使用 restorecon 命令让新设置的SELinux 安全上下文立即生效
restorecon -Rv /home/wwwroot

10.5.2 基于主机域名

当服务器无法为每个网站都分配一个独立IP地址的时候,可以尝试让 Apache 自动识别用户请求的域名,从而根据不同的域名请求来传输不同的内容。在这种情况下,只需要保证位于生产环境中的服务器上有一个可用的IP地址(这里以192.168.10.10 为例)就可以了。

由于当前还没有介绍如何配置DNS解析服务,因此需要手工定义IP地址与域名之间的对应关系。/etc/hosts 是Linux 系统中用于强制把某个主机域名解析到指定IP 地址的配置文件。简单来说,只要这个文件配置正确,即使网卡参数中没有DNS 信息也依然能够将域名解析为某个IP 地址。

第 1 步:手工定义IP地址与域名之间对应关系的配置文件/etc/hosts

[root@linuxprobe ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.10 www.linuxprobe.com bbs.linuxprobe.com tech.linuxprobe.com

第 2 步:通过分别 ping 这些域名来验证域名是否已经成功解析为IP地址

[root@linuxprobe ~]# ping -c 4 www.linuxprobe.com
PING www.linuxprobe.com (192.168.10.10) 56(84) bytes of data.
64 bytes from www.linuxprobe.com (192.168.10.10): icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from www.linuxprobe.com (192.168.10.10): icmp_seq=2 ttl=64 time=0.077 ms
64 bytes from www.linuxprobe.com (192.168.10.10): icmp_seq=3 ttl=64 time=0.061 ms
64 bytes from www.linuxprobe.com (192.168.10.10): icmp_seq=4 ttl=64 time=0.069 ms
--- www.linuxprobe.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.061/0.069/0.077/0.008 ms
[root@linuxprobe ~]#

第 3 步:分别在 /home/wwwroot 中创建用于保存不同网站数据的三个目录,写入网站的首页文件。
mkdir -p /home/wwwroot/www
mkdir -p /home/wwwroot/bbs
mkdir -p /home/wwwroot/tech
echo "WWW.linuxprobe.com" > /home/wwwroot/www/index.html
echo "BBS.linuxprobe.com" > /home/wwwroot/bbs/index.html
echo "TECH.linuxprobe.com" > /home/wwwroot/tech/index.html

第 4 步:修改 httpd 服务的配置文件
vim /etc/httpd/conf/httpd.conf

<VirtualHost 192.168.10.10>
DocumentRoot “/home/wwwroot/www”
ServerName “www.linuxprobe.com”
<Directory “/home/wwwroot/www”>
AllowOverride None
Require all granted
</directory>
</VirtualHost>
<VirtualHost 192.168.10.10>
DocumentRoot “/home/wwwroot/bbs”
ServerName “bbs.linuxprobe.com”
<Directory “/home/wwwroot/bbs”>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10>
DocumentRoot “/home/wwwroot/tech”
ServerName “tech.linuxprobe.com”
<Directory “/home/wwwroot/tech”>
AllowOverride None
Require all granted
</directory>
</VirtualHost>

第 5 步:修改 SELinux 安全上下文
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www/*
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs/*
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech/*

第 6 步:让新配置的 SELinux 安全上下文立即生效
restorecon -Rv /home/wwwroot

10.5.3 基于端口号

基于端口号的虚拟主机功能可以让用户通过指定的端口号来访问服务器上的网站资源。在使用Apache 配置虚拟网站主机功能时,基于端口号的配置方式是最复杂的。因此不仅要考虑httpd 服务程序的配置因素,还需要考虑到SELinux 服务对新开设端口的监控。一般来说,使用80、443、8080 等端口号来提供网站访问服务是比较合理的,如果使用其他端口号则会受到SELinux 服务的限制。

第一步:分别在 /home/wwwroot 中创建用于保存不同网站数据的两个目录
mkdir -p /home/wwwroot/6111
mkdir -p /home/wwwroot/6222
echo "port:6111" > /home/wwwroot/6111/index.html
echo "port:6222" > /home/wwwroot/6222/index.html

第二步:修改 httpd 服务配置文件,添加用于监听 6111 和 6222 端口的参数
vim /etc/httpd/conf/httpd.conf

Listen 6111
Listen 6222

第三步:在 httpd 服务配置文件中,分别追加写入两个基于端口号的虚拟主机网站参数
vim /etc/httpd/conf/httpd.conf

<VirtualHost 192.168.10.10:6111>
DocumentRoot “/home/wwwroot/6111”
ServerName www.linuxprobe.com
<Directory “/home/wwwroot/6111”>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10:6222>
DocumentRoot “/home/wwwroot/6222”
ServerName bbs.linuxprobe.com
<Directory “/home/wwwroot/6222”>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

第四步:设置网站数据目录文件的 SELinux 安全上下文
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111/*
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222/*

第五步:用 restorecon 命令让新配置的 SELinux 安全上下文立即生效
restorecon -Rv /home/wwwroot/

第六步:重启 HTTP 服务

[root@linuxprobe ~]# systemctl restart httpd
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctlxn' for details.

第七步:使用 semanage 命令查询并过滤出所有与HTTP 协议相关且 SELinux 服务允许的端口列表 http_port_t

[root@linuxprobe ~]# semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989

第八步:将这两个端口号手动添加到 SELinux 安全上下文

[root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6111
[root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6222
[root@linuxprobe ~]# semanage port -l| grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 6222, 6111, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989

第九步:用 restorecon 命令让新配置的SELinux 安全上下文立即生效
restorecon -Rv /home/wwwroot/

第十步:重启 HTTP 服务
systemctl restart httpd

10.6 Apache 的访问控制

Apache 可以基于源主机名、源IP 地址或源主机上的浏览器特征等信息对网站上的资源进行访问控制。

通过 Allow 指令允许某个主机访问服务器上的网站资源,通过 Deny 指令实现禁止访问。在允许或禁止访问网站资源时,还会用到 Order 指令,这个指令用来定义Allow或Deny 指令起作用的顺序,其匹配原则是按照顺序进行匹配,若匹配成功则执行后面的默认
指令。

比如“Order Allow, Deny”表示先将源主机与允许规则进行匹配,若匹配成功则允许访问请求,反之则拒绝访问请求。

第 1 步:先在服务器上的网站数据目录中新建一个子目录,并在这个子目录中创建一个包含 Successful 单词的首页文件。

[root@linuxprobe ~]# mkdir /var/www/html/server
[root@linuxprobe ~]# echo "Successful" > /var/www/html/server/index.html

第 2 步:打开 httpd 服务的配置文件/etc/httpd/conf/httpd.conf,在第129 行后面添加下述规则来限制源主机的访问。这段规则的含义是允许使用Firefox 浏览器的主机访问服务器上的首页文件,除此之外的所有请求都将被拒绝。

[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
129 <Directory "/var/www/html/server">
130 SetEnvIf User-Agent "Firefox" ff=1
131 Order allow,deny
132 Allow from env=ff
133 </Directory>
………………省略部分输出信息………………
[root@linuxprobe ~]# systemctl restart httpd
[root@linuxprobe ~]# firefox

除了匹配源主机的浏览器特征之外,还可以通过匹配源主机的IP 地址进行访问控制。例如,我们只允许IP 地址为192.168.10.20 的主机访问网站资源,那么就可以在 httpd 服务配置文件的第129 行后面添加下述规则。这样在重启httpd 服务程序后再用本机(即服务器,其IP地址为192.168.10.10)来访问网站的首页面时就会提示访问被拒绝了。

[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
129 <Directory "/var/www/html/server">
130 Order allow,deny
131 Allow from 192.168.10.20
132 Order allow,deny
133 Allow from env=ie
134 </Directory>
………………省略部分输出信息………………
[root@linuxprobe ~]# systemctl restart httpd
[root@linuxprobe ~]# firefox
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值