使用Apache服务部署静态网站

 前言:本博客仅作记录学习使用,部分图片出自网络,如有侵犯您的权益,请联系删除 

目录

一、网站服务程序

​二、配置服务文件参数

​三、SELinux安全子系统

四、个人用户主页功能

​五、虚拟网站主机功能

六、Apache的访问控制

致谢


一、网站服务程序

Web网络服务是一种被动访问的服务程序,即只有接收到互联网中其他主机发出的请求后才会响应,最终用于提供服务程序的Web服务器会通过HTTP(超文本传输协议)HTTPS(安全超文本传输协议)把请求的内容传送给客户。

目前能够提供Web网络服务的程序有IIS、Nginx和ApacheIIS(Internet Information Sevices,互联网信息服务)是Windows系统中默认的Web服务程序,这是一款图形化的网站管理工具,不仅可以提供Web服务,还可以提供FTP、NMTP、SMTP等服务。但是,IIS只能再Windows系统中使用。

Nginx程序是一款轻量级的网站服务软件,因为稳定性和丰富的功能而快速占领服务器市场,其系统资源消耗低且并发能力强;

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

示例:手动安装apache服务程序。注意使用dnf命令安装时,Apache服务的软件包名称为httpd

 [root@linux ~]# dnf install httpd

启用httpd服务程序并将其加入到开机启动项中:

 [root@linux ~]# systemctl start httpd
 [root@linux ~]# systemctl enable httpd
 Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

在浏览器中输入http://127.0.0.1并按回车键,就可以看到用于提供Web服务的默认页面了:

二、配置服务文件参数

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

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

主配置文件中保存的是最重要的服务参数,在httpd服务程序的主配置文件中,存在三种类型的信息:注释行信息、全局配置、区域配置

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

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

得知DocumentRoot参数用于定义网站数据的保存路径,其参数的默认值是把网站数据存放到/var/www/html目录中;当前网站普遍首页是index.html,因此可以向其中写入内容来替换默认的首页面:

 [root@linux ~]# echo "Welcome to Apache" > /var/www/html/index.html
 [root@linux ~]# firefox

在默认情况下,网站数据保存在/var/www/html目录中,而如果想把保存网站的数据的目录修改为/home/wwwroot目录:

第1步:建立网站数据的保存目录,并创建首页文件:

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

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

 [root@linux ~]# vim /etc/httpd/conf/httpd.conf
 117 #
 118 # DocumentRoot: The directory out of which you will serve your
 119 # documents. By default, all requests are taken from this directory, but
 120 # symbolic links and aliases may be used to point to other locations.
 121 #
 122 DocumentRoot "/home/wwwroot"
 123 
 124 #
 125 # Relax access to content within /var/www.
 126 #
 127 <Directory "/home/wwwroot">
 128     AllowOverride None
 129     # Allow open access:
 130     Require all granted
 131 </Directory>
 132 
 133 # Further relax access to the default document root:
 134 <Directory "/home/wwwroot">

第3步:重新启动httpd服务程序并验证效果;发现,权限不足!!

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

三、SELinux安全子系统

SELinux(Security-Enhanced Linux)是强制访问控制MAC,Mandatory Access Control)的安全子系统。Linux系统使用SELinux技术的目的是为了让各个服务进程都受到约束,使其仅获取到本应获取的资源

通俗来说:对服务程序的功能进行限制——SELinux域限制可以确保服务程序做不了出格的事情;对文件资源的访问控制——SELinux安全上下文确保文件资源只能被其所属的服务程序进行访问

"SELinux域"和"SELinux安全上下文"称为是Linux系统中的双保险;SELinux服务有三种配置模式:

  • enforcing:强制启用安全策略模式,将拦截服务的不合法请求
  • permissive:遇到服务越权访问时,只发出警告而不强制拦截
  • disabled:对于越权的行为不警告也不拦截

学习中的所有实验都是在强制启用安全策略模式下进行的,虽然在禁用SELinux服务后确实能够减少报错几率,但这在生产环境中相当不推荐。检查本机SELinux服务主配置文件中定义的状态,设置成enforcing

 [root@linux ~]# 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 three 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@linux ~]# getenforce
 Enforcing

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

[root@linux ~]# setenforce 0
[root@linux ~]# getenforce
Permissive

再次刷新网页,就可以看到正常的网页内容:

原理:/home目录是用来存放普通用户的家目录数据的,而现在,httpd提供的网站服务却要去获取普通用户家目录中的数据了,显然违反SELinux的监管原则

现在,把SELinux服务恢复到强制启用安全策略模式,然后分别查看原始网站数据的保存目录与当前网站数据的保存目录是否用于不同的SELinux安全上下文。ls命令中"-Z"参数用于查看文件的安全上下文值,而"-d"参数代表对象是个文件夹

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

# 用户段system_u:代表系统进程的身份;角色段object_r:代表文件目录的角色;类型段httpd_sys+content_t:代表网站服务的系统文件。

1、semanage命令

semanage命令用于管理SELinux的策略,语法:"semanage [参数] [文件]"

SELinux服务极大地提升了Linux系统的安全性,将用户权限牢牢地所在笼子里。semanage命令不仅能够像传统chcon命令那样—设置文件、目录的策略,还能够管理网络端口、消息接口。常用参数如下:

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

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

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

执行上述设置后,还无法立即访问网站,还需要使用restorecon命令设置好的SELinux安全上下文立即生效。加上-Rv参数对指定的目录进行递归操作,以及显示SELinux安全上下文的修改过程:

root@Linux ~]# restorecon -Rv /home/wwwroot

四、个人用户主页功能

如果想在系统中为每一位用户建立一个独立的网站,通常的方法是基于虚拟网站主机功能来部署多个网站。httpd服务程序提供的个人用户主页功能可以让系统内的所有用户在自己的家目录中管理个人的网站;

第1步:在httpd服务程序中,默认没有开启个人用户主页功能;编辑配置文件,将UserDir disabled注释加上让httpd服务程序开启个人用户主页功能;在把UserDir publiuc_html参数前面的注释去掉:表示网站数据在用户家目录中的保存目录名称;

 [root@Linux ~]# vim /etc/httpd/conf.d/userdir.conf
 ...
 11 <IfModule mod_userdir.c>
 12     #
 13     # UserDir is disabled by default since it can confirm the presence
 14     # of a username on the system (depending on home directory
 15     # permissions).
 16     #
 17     UserDir disabled
 18 
 19     #
 20     # To enable requests to /~user/ to serve the user's public_html
 21     # directory, remove the "UserDir disabled" line above, and uncomment
 22     # the following line instead:
 23     # 
 24     UserDir public_html
 25 </IfModule>
 ...

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

[root@Linux ~]# su - linuxprobe
[linuxprobe@Linux ~]$ mkdir public_html
[linuxprobe@Linux ~]$ echo "This is Linuxprobe's website" > public_html/index.html
[linuxprobe@Linux ~]$ chmod -R 755 /home/linuxprobe

第3步重新启动httpd服务程序,在浏览器的地址栏输入网址,格式"网址/~用户名",理论上可以访问,但报错,猜测SELinux的原因

[linuxprobe@Linux ~]$ exit
logout
[root@Linux ~]# systemctl restart httpd

第4步分析:httpd服务程序在提供个人用户主页功能时,该用户的网站数据目录本身就应该是存放到这位用户对应的家目录中,所以不需要修改家目录的SELinux安全上下文,前面提到,SELinux域确保服务程序不能执行违规的操作,只能本本分分的为用户提供服务。所以httpd服务中突然开启的这项个人用户主页功能有没有没SELinux服务允许呢?

使用getsebool命令查询并过滤出所有与HTTP协议相关的安全策略。其中off为禁止状态、on为允许状态

[root@Linux ~]# 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服务的个人用户功能用到的SELinux域安全策略是httpd_enable_homedirs;使用setsebool命令来修改SELinux策略中各条规则的布尔值。记得添加-P参数,让修改后的SELinux策略规则永久生效且立即生效

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

有时,网站的拥有者并不希望直接将网页内容显示出来;只想让通过身份验证的用户访客看到里面的内容

第1步:先使用htppasswd命令生成密码数据库-c参数表示第一次生成;后面再分别添加面数据库的存放文件,以及验证要用到的用户名称(该用户不必是系统中已有的本地账户)

[root@Linux ~]# htpasswd -c /etc/httpd/passwd linuxprobe
New password: 
Re-type new password: 
Adding password for user linuxprobe

第2步:编辑个人用户主页功能的配置文件:修改以下内容:

[root@Linux ~]# vim /etc/httpd/conf.d/userdir.conf 
<Directory "/home/*/public_html">
    AllowOverride all
    authuserfile "/etc/httpd/passwd"
    authname "My privately website"
    authtype basic
    require user linuxprobe
</Directory>
[root@Linux ~]# systemctl restart httpd

当用户想要再访问用户的个人网站时,就必须要输入账户和密码才能正常访问。另外,账户和密码是用htpasswd命令生成的专门用来网站登录的口令密码,不是系统中的用户密码;

五、虚拟网站主机功能

利用虚拟主机功能,可以把一台处于运行状态的物理服务器分割成多个"虚拟的服务器";但无法实现目前云主机技术的硬件资源隔离,让这些虚拟的服务器共同使用物理服务器的硬件资源;

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

1、基于IP地址

如果一台服务器有多个IP地址,而且每个IP地址与服务器上部署的每个网站一一对应,这样当用户请求访问不同的IP地址时,会访问到不同网站的页面资源。而且、每个网站都有一个独立的IP地址,对搜索引擎优化也有好处;

示例:前期准备:首先配置三个可用网络IP:

分别检查3个IP地址的连通性:

[root@Linux ~]# ping -c 4 10.0.0.2
[root@Linux ~]# ping -c 4 10.0.0.3
[root@Linux ~]# ping -c 4 10.0.0.4
...

第1步分别在/home/wwwroot中创建用于保存不同网站数据的3个目录,并向其中分别写入网站的首页文件。每个首页文件中应有明确区分不同网站信息的内容,方便直观;

[root@Linux ~]# mkdir -p /home/wwwroot/2
[root@Linux ~]# mkdir -p /home/wwwroot/3
[root@Linux ~]# mkdir -p /home/wwwroot/4
[root@Linux ~]# echo "IP:10.0.0.2" > /home/wwwroot/2/index.html
[root@Linux ~]# echo "IP:10.0.0.3" > /home/wwwroot/3/index.html
[root@Linux ~]# echo "IP:10.0.0.4" > /home/wwwroot/4/index.html

第2步:在httpd服务的配置文件中约132行开始,分别追加写入三个基于IP地址的虚拟主机网站参数,保存退出;重启httpd服务:

[root@Linux ~]# vim /etc/httpd/conf/httpd.conf 
...
133 <VirtualHost 10.0.0.2>
134     DocumentRoot /home/wwwroot/2
135     ServerName www.linuxprobe.com
136     <Directory /home/wwwroot/2>
137     AllowOverride None
138     Require all granted
139     </Directory>
140 </VirtualHost>
141 
142 <VirtualHost 10.0.0.3>
143     DocumentRoot /home/wwwroot/3
144     ServerName www.linuxcool.com
145     <Directory /home/wwwroot/3>
146     AllowOverride None
147     Require all granted
148     </Directory>
149 </VirtualHost>
150 
151 <VirtualHost 10.0.0.4>
152     DocumentRoot /home/wwwroot/4
153     ServerName www.linuxdown.com
154     <Directory /home/wwwroot/4>
155     AllowOverride None
156     Require all granted
157     </Directory>
158 </VirtualHost>
...
[root@Linux ~]# systemctl restart httpd

第3步:此时访问网站,发现权限不够;由于当前的/home/wwwroot目录及里面的网站数据目录与SELinux安全上下文与网站服务不吻合;手动把新的网站数据目录的SELinux安全上下文设置正确,并使用restorecon命令让新设置的SELinux安全上下文立即生效

[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/2
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/3
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/4
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/2/*
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/3/*
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/4/*
[root@Linux ~]# restorecon -Rv /home/wwwroot

2、基于主机域名

当服务器无法为每个网站都分配一个独立的IP地址的时候,可以尝试让Apache自动识别用户请求的域名,从而根据不同的域名请求来传输不同的内容。

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

第1步手工定义IP地址与域名之间对应关系的配置文件,保存退出立即生效:可以通过分别ping这些域名来验证域名是否已经成功解析为IP地址:

[root@Linux ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
10.0.0.2    www.linuxprobe.com      
10.0.0.3    www.linuxcool.com
10.0.0.4	www.linuxdown.com
[root@Linux ~]# ping -c 4 www.linuxprobe.com
PING www.linuxprobe.com (10.0.0.2) 56(84) bytes of data.
64 bytes from www.linuxprobe.com (10.0.0.2): icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from www.linuxprobe.com (10.0.0.2): icmp_seq=2 ttl=64 time=0.070 ms
64 bytes from www.linuxprobe.com (10.0.0.2): icmp_seq=3 ttl=64 time=0.038 ms
64 bytes from www.linuxprobe.com (10.0.0.2): icmp_seq=4 ttl=64 time=0.034 ms

--- www.linuxprobe.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3088ms
rtt min/avg/max/mdev = 0.034/0.053/0.070/0.017 ms

第2步分别在/home/wwwroot中创建用于保存不同网站数据的的三个目录,并向其中分别写入网站的首页文件

[root@Linux ~]# mkdir -p /home/wwwroot/linuxprobe
[root@Linux ~]# mkdir -p /home/wwwroot/linuxcool
[root@Linux ~]# mkdir -p /home/wwwroot/linuxdown
[root@Linux ~]# echo "www.linuxprobe.com" > /home/wwwroot/linuxprobe/index.html
[root@Linux ~]# echo "www.linuxcool.com" > /home/wwwroot/linuxcool/index.html
[root@Linux ~]# echo "www.linuxdown.com" > /home/wwwroot/linuxdown/index.html

第3步:在httpd服务的配置文件中大约132行,分别追加写入三个基于主机名的虚拟主机网站参数,保存退出并重启httpd服务

[root@Linux ~]# vim /etc/httpd/conf/httpd.conf
133 <VirtualHost 10.0.0.2>
134     Documentroot /home/wwwroot/linuxprobe
135     ServerName www.linuxprobe.com
136     <Directory /home/wwwroot/linuxprobe>
137     AllowOverride None
138     Require all granted
139     </Directory>
140 </VirtualHost>
141 
142 <VirtualHost 10.0.0.3>
143     Documentroot /home/wwwroot/linuxcool
144     ServerName www.linuxcool.com
145     <Directory /home/wwwroot/linuxcool>
146     AllowOverride None
147     Require all granted
148     </Directory>
149 </VirtualHost>
150     
151 <VirtualHost 10.0.0.4>
152     Documentroot /home/wwwroot/linuxdown
153     ServerName www.linuxdown.com
154     <Directory /home/wwwroot/linuxdown>
155     AllowOverride None
156     Require all granted
157     </Directory>
158 </VirtualHost>
159 
[root@Linux ~]# systemctl restart httpd

第4步:因为当前的网站数据目录还是在/home/wwwroot目录中,因此还是必须要正确设置网站数据目录文件的SELinux安全上下文,使其与网站数据服务功能相吻合。

[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/linuxprobe
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/linuxprobe/*
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/linuxcool
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/linuxcool/*
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/linuxdown
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/linuxdown/*
[root@Linux ~]# restorecon -Rv /home/wwwroot

3、基于端口号

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

第1步:分别在/home/wwwroot中创建用于保存不同网站数据的三个目录,并向其中写入网站的首页文件...

[root@Linux ~]# mkdir -p /home/wwwroot/6111
[root@Linux ~]# mkdir -p /home/wwwroot/6222
[root@Linux ~]# mkdir -p /home/wwwroot/6333
[root@Linux ~]# echo "port:6111" > /home/wwwroot/6111/index.html
[root@Linux ~]# echo "port:6222" > /home/wwwroot/6222/index.html
[root@Linux ~]# echo "port:6333" > /home/wwwroot/6333/index.html

第2步:在httpd服务配置文件的第64行至48行分别添加用于监听6111、6222和6333端口的参数

[root@Linux ~]# vim /etc/httpd/conf/httpd.conf 
37 # Listen: Allows you to bind Apache to specific IP addresses and/or
 38 # ports, instead of the default. See also the <VirtualHost>
 39 # directive.
 40 #
 41 # Change this to Listen on specific IP addresses as shown below to 
 42 # prevent Apache from glomming onto all bound IP addresses.
 43 #
 44 #Listen 12.34.56.78:80
 45 Listen 80
 46 Listen 6111
 47 Listen 6222
 48 Listen 6333

第3步:在httpd服务的配置文件中大约134行开始,分别追加写入三个基于端口号的虚拟主机网站参数,保存退出并重启httpd服务

[root@Linux ~]# vim /etc/httpd/conf/httpd.conf 
<VirtualHost 10.0.0.2:6111>
	DocumentRoot /home/wwwroot/6111
	ServerName www.linuxprobe.com
	<Directory /home/wwwroot/6111>
	AllowOverride None
	Require all granted
	</Directory>
</VirtualHost>

<VirtualHost 10.0.0.2:6222>
	DocumentRoot /home/wwwroot/6222
	ServerName www.linuxcool.com
	<Directory /home/wwwroot/6222>
	AllowOverride None
	Require all granted
	</Directory>
</VirtualHost>

<VirtualHost 10.0.0.2:6333>
	DocumentRoot /home/wwwroot/6333
	ServerName www.linuxdown.com
	<Directory /home/wwwroot/6333>
	AllowOverride None
	Require all granted
	</Directory>
</VirtualHost>

第4步为网站数据目录文件设置SELinux安全上下文

[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111/*
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222/*
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6333
[root@Linux ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6333/*
[root@Linux ~]# restorecon -Rv /home/wwwroot/
[root@Linux ~]# systemctl restart httpd
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xe" for details.

重启居然报错了!!!因为SELinux服务会检测到6111、6222和6333端口原本不属于Apache服务应该需要的资源,但现在却以httpd服务程序的名义监听使用了,所以SELinux会拒绝使用Apache服务使用三个端口。使用semanage命令查询并过滤出所有与HTTP协议相关且SELinux服务允许的端口列表

[root@Linux ~]# 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

第5步SELinux允许的与HTTP协议相关的端口号中默认没有包含这三个端口,因此需要手动添加

[root@Linux ~]# semanage port -a -t http_port_t -p tcp 6111
[root@Linux ~]# semanage port -a -t http_port_t -p tcp 6222
[root@Linux ~]# semanage port -a -t http_port_t -p tcp 6333
[root@Linux ~]# 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      6333, 6222, 6111, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
[root@Linux ~]# systemctl restart httpd
[root@Linux ~]# firefox

六、Apache的访问控制

Apache可以基于源主机名、源IP地址或源主机上的浏览器特征等信息对网站上的资源进行访问控制。它通过Allow指令允许某个主机访问服务器上的网站资源,通过Deny指令实现禁止访问。在允许或禁止访问网站资源时,还会用到Order指令,这指令用来定义Allow或Deny指令起作用的顺序,其匹配成功则执行后面的默认命令。比如"Order Allow,Deny"表示先将源主机与允许规则进行匹配,若匹配成功则允许访问请求,反之拒绝

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

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

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

[root@Linux ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/server">
	SetEnvIf User-Agent "Firefox" ff=1
	Order allow,deny
	Allow from env=ff
</Directory>
...
[root@Linux ~]# systemctl restart httpd
[root@Linux ~]# firefox

这样就只允许火狐浏览器访问了。

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

[root@Linux ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/server">
	Order allow,deny
	Allow from 10.0.0.3
</Directory>
[root@Linux ~]# systemctl restart httpd
[root@Linux ~]# firefox

致谢

在此,我要对所有为知识共享做出贡献的个人和机构表示最深切的感谢。同时也感谢每一位花时间阅读这篇文章的读者,如果文章中有任何错误,欢迎留言指正。 

学习永无止境,让我们共同进步!!

  • 21
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李学不完

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值