Linux_12/ Apache, SELinux

网站数据目录修改 个人主页密码 虚拟网站主机-IP 虚拟网站主机-主机域名 虚拟网站主机-端口号80,443,8080
 

vim /etc/httpd/conf.d/userdir.conf
32 AllowOverride all

33 authuserfile "/etc/httpd/passwd"

#刚刚生成出来的密码验证文件保存路径

34 authname "My privately website"

#当用户尝试访问个人用户网站时的提示信息

35 authtype basic

36 require user linuxprobe

#用户进行账户密码登录时需要验证的用户名称

 vim /etc/httpd/conf/httpd.conf

<virtualhost 192.168.10.77>
         documentroot /home/wwwroot/77
         servername www.zhxu.com
     <directory /home/wwwroot/77>
         allowoverride none
         require all granted
     </directory>
 </virtualhost>

###以上8行,重复三次,四处不同


vim /etc/httpd/conf/httpd.conf

<virtualhost    192.168.10.77>
        documentroot /home/wwwroot/www
        servername www.zhxu.com
    <directory  /home/wwwroot/www>
        allowoverride none
        require all granted
    </directory>
</virtualhost>

###以上8行,重复三次,三处不同

vim /etc/httpd/conf/httpd.conf

<virtualhost 192.168.10.77:6111>
        documentroot /home/wwwroot/6111
        servername www.zhxu.com
    <directory /home/wwwroot/6111>
        allowoverride none
        require all greanted
    </directory>
</virtualhost>

###以上8行,重复两次,三处不同

       

vim /etc/httpd/conf/httpd.conf

Listen 6111
Listen 6222

         
         

0, 虚拟机设置

 

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仓库配置文件

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

3,手动安装Apache: httpd

[root@localhost ~]# yum install httpd
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
......

Installed:
  httpd.x86_64 0:2.4.6-17.el7                                                                                                                 

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7      apr-util.x86_64 0:1.5.2-6.el7      httpd-tools.x86_64 0:2.4.6-17.el7      mailcap.noarch 0:2.1.41-2.el7     

Complete!
4,启用httpd,并加入到开机启动项

[root@localhost ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
   Active: inactive (dead)

[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
[root@localhost ~]#
[root@localhost ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Tue 2018-11-27 10:49:01 CST; 10s ago
 Main PID: 32331 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─32331 /usr/sbin/httpd -DFOREGROUND
           ├─32332 /usr/sbin/httpd -DFOREGROUND
           ├─32333 /usr/sbin/httpd -DFOREGROUND
           ├─32334 /usr/sbin/httpd -DFOREGROUND
           ├─32335 /usr/sbin/httpd -DFOREGROUND
           └─32336 /usr/sbin/httpd -DFOREGROUND

Nov 27 10:49:00 localhost.localdomain httpd[32331]: AH00558: httpd: Could not reliably determine the server's fully qualified domain ...essage
Nov 27 10:49:01 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#

 5,火狐,http://127.0.0.1验证. 可能没有数据,可能是没有权限(SELinux)

 

6,配置参数文件

Linux系统中的配置文件
1 /etc/httpd 服务目录
2 /etc/httpd/conf/httpd.conf 主配置文件
3 /var/www/html 网站数据目录
4 /var/log/httpd/access_log 访问日志
5 /var/log/httpd/error_log 错误日志
配置httpd服务程序时最常用的参数以及用途描述
1 ServerRoot 服务目录
2 ServerAdmin 管理员邮箱
3 User 运行服务的用户
4 Group 运行服务的用户组
5 ServerName 网站服务器的域名
6 DocumentRoot 网站数据目录
7 Listen 监听的IP地址与端口号
8 DirectoryIndex 默认的索引页页面
9 ErrorLog 错误日志文件
10 CustomLog 访问日志文件
11 Timeout 网页超时时间,默认为300秒

 

6.1,修改首页

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

[root@localhost html]# cd /var/www/html/
[root@localhost html]# echo "Welcome to my world" > index.html

 

6.2,修改网站数据保存目录
在默认情况下,网站数据是保存在/var/www/html目录中,而如果想把保存网站数据的目录修改为/home/wwwroot目录,该怎么操作呢?且看下文。

6.2.1,新建保存目录,并编辑首页

[root@localhost ~]# mkdir /home/wwwroot  #新建目录
[root@localhost ~]# ls -ld /home/wwwroot/  #
[root@localhost ~]# echo "Welcome to ZHXU home" > /home/wwwroot/index.html
6.2.2,打开httpd服务程序的主配置文件,将约第119行用于定义网站数据保存路径的参数DocumentRoot修改为/home/wwwroot,同时还需要将约第124行用于定义目录权限的参数Directory后面的路径也修改为/home/wwwroot。配置文件修改完毕后即可保存并退出。

[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf 
………………省略部分输出信息………………
113 
114 #
115 # DocumentRoot: The directory out of which you will serve your
116 # documents. By default, all requests are taken from this directory, bu t
117 # symbolic links and aliases may be used to point to other locations.
118 #
119 DocumentRoot "/home/wwwroot"
120 
121 #
122 # Relax access to content within /var/www.
123 #
124 <Directory "/home/wwwroot">
125 AllowOverride None
126 # Allow open access:
127 Require all granted
128 </Directory>
………………省略部分输出信息………………

6.2.3,重新启动httpd服务程序并验证效果,浏览器刷新页面后的内容如图所示。奇怪!为什么看到了httpd服务程序的默认首页面?按理来说,只有在网站的首页面文件不存在或者用户权限不足时,才显示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

 

 


7,SELinux安全子系统

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

例如,您在自己的电脑上下载了一个美图软件,当您全神贯注地使用它给照片进行美颜的时候,它却在后台默默监听着浏览器中输入的密码信息,而这显然不应该是它应做的事情(哪怕是访问电脑中的图片资源,都情有可原)。SELinux安全子系统就是为了杜绝此类情况而设计的,它能够从多方面监控违法行为:对服务程序的功能进行限制(SELinux域限制可以确保服务程序做不了出格的事情);对文件资源的访问限制(SELinux安全上下文确保文件资源只能被其所属的服务程序进行访问)。

SELinux服务有三种配置模式,具体如下。

enforcing:强制启用安全策略模式,将拦截服务的不合法请求。

permissive:遇到服务越权访问时,只发出警告而不强制拦截。

disabled:对于越权的行为不警告也不拦截。

     
1 getenforce  
2  setenforce

0-Permissive

1-Enforcing

7.1,获取当前SELinux的服务模式

[root@localhost ~]# getenforce
Enforcing
7.2,用setenforce [0|1]命令修改SELinux当前的运行模式(0为禁用,1为启用)。注意,这种修改只是临时的,在系统重启后就会失效:

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
7.3,此时刷新火狐浏览器,6.2.3的问题解决了

现在,我们来回忆一下6.2.3的操作中到底是哪里出问题了呢?

httpd服务程序的功能是允许用户访问网站内容,因此SELinux肯定会默认放行用户对网站的请求操作。但是,我们将网站数据的默认保存目录修改为了/home/wwwroot,而这就产生问题了。在6.1小节中讲到,/home目录是用来存放普通用户的家目录数据的,而现在,httpd提供的网站服务却要去获取普通用户家目录中的数据了,这显然违反了SELinux的监管原则。

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

[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce
Enforcing

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

  命令 解释
1 ls -Z Display security context so it fits on most displays.  Displays only mode, user, group, security context and file name.

在文件上设置的SELinux安全上下文是由用户段、角色段以及类型段等多个信息项共同组成的。其中,用户段system_u代表系统进程的身份,角色段object_r代表文件目录的角色,类型段httpd_sys_content_t代表网站服务的系统文件。针对当前这种情况,我们只需要使用semanage命令,将当前网站目录/home/wwwroot的SELinux安全上下文修改为跟原始网站目录的一样就可以了。

7.4,semanage

常用参数:

-l参数用于查询;

-a参数用于添加;

-m参数用于修改;

-d参数用于删除。

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

[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot  #此目录后面不能加/,红帽bug
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*

使用restorecon命令将设置好的SELinux安全上下文立即生效。在使用restorecon命令时,可以加上-Rv参数对指定的目录进行递归操作,以及显示SELinux安全上下文的修改过程。最后,再次刷新页面,就可以正常看到网页内容了:

[root@localhost ~]# 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@localhost ~]#firefox  #验证浏览器显示

---------------------------------------------------------------------------------------------------------------------------------------------------

7.5,个人用户主页功能

---------------------------------------------------------------------------------------------------------------------------------------------------

 7.5.1 开启个人主页功能

[root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf
17 # UserDir disabled  #让httpd服务程序开启个人用户主页功能
24 UserDir public_html  #UserDir参数表示网站数据在用户家目录中的保存目录名称,即public_html目录


7.5.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

7.5.3 启动Firefox发现,网址/~用户名(127.0.0.1/~/linuxprobe) 显示forbidden

 

7.5.4 查看所有与HTTP协议相关的安全策略。其中,off为禁止状态,on为允许状态。

[root@linuxprobe ~]# getsebool -a | grep http | grep enable

httpd_enable_cgi --> on

httpd_enable_ftp_server --> off

httpd_enable_homedirs --> off

7.5.5 修改SELnux策略中各条规则的布尔值。

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

[root@linuxprobe ~]# firefox  #验证

7.5.6 设置身份验证用户可以访问。

————————————————————————————————————————————————————————————————

 #密码生成

————————————————————————————————————————————————————————————————

[root@linuxprobe ~]# htpasswd -c /etc/httpd/passwd linuxprobe

#修改配置文件

[root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf
32 AllowOverride all

33 authuserfile "/etc/httpd/passwd"  #刚刚生成出来的密码验证文件保存路径

34 authname "My privately website"   #当用户尝试访问个人用户网站时的提示信息

35 authtype basic

36 require user linuxprobe  #用户进行账户密码登录时需要验证的用户名称

#重启服务验证

 

posted @ 2018-12-03 22:15  linux_ironman  阅读(196)  评论(0编辑  收藏  举报