如何使用Apache服务部署静态网站

学习目标:

  • 了解网站服务程序有哪些?
  • 如何配置服务文件参数?
  • SELinux安全子系统有何作用及影响?
  • 如何设置个人用户主页功能?

学习内容:

  1. 目前能够提供Web网络服务的程序有IISNginxApache等。
    类型功能
    IISwindows默认一款图形化网站管理工具,不仅可以提供web服务,还可以提供FTP、NMTP、SMTP等服务。
    Nginx一款轻量级的网站服务软件,其有着系统资源消耗低且并发能力强的特性!
    ApacheApache的跨平台及安全性好,且拥有快速可靠的API扩展,能够实时监视服务状态与定制日志消息。
  2. Linux中httpd服务程序的主要配置文件如表格所示,其中主配置文件中保存的是最重要的服务参数。

    作用文件名称
    服务目录/etc/httpd
    主配置文件/etc/httpd/httpd.conf
    网站数据目录/var/www/html
    访问日志/var/log/httpd/access_log
    错误日志/var/log/httpd/error_log
  3. Linux系统使用SELinux技术的目的是为了让各个服务进程都受到约束,使其仅获取到本应获取的资源,SELinux域限制可以确保服务程序做不了出格的事;SELinux安全上下文确保文件资源只能被其所属的服务程序进行访问。

  4. SELinux服务有三种配置模式,其主配置文件是 /etc/selinux/config

    类型作用
    enforcing拦截服务的不合法请求
    permissive只发出警告而不强制拦截
    disabled对于越权行为不警告也不拦截
  5. semanage命令用于管理SELinux的策略,语法格式为:“semanage 【参数】【文件】”,semanage命令不仅能够像传统的chcon命令那样设置文件、目录的策略,还能管理网络端口、消息接口。常用参数如表:

    参数作用
    -l查询
    -a添加
    -m修改
    -d删除
  6. httpd服务程序提供的个人用户主页功能可以在系统中为每位用户建立一个独立的网站,让用户自己在家目录中管理自己的网站。


实验输出:

实验一:在默认情况下,网站数据保存在/var/www/html中,请把把保存网站的数据修改为/home/wwwroot目录。

step1:建立网站数据的保存目录,并创建首页文件。

[root@linuxprobe ~]# mkdir /home/wwwroot
[root@linuxprobe ~]# echo "I love you so much" > /home/wwwroot/index.html

step2:打开httpd服务程序的主配置文件,将122行用于定义网站数据保存路径的参数DocumentRoot修改为/home/wwwroot,同事还需要将127行与134行用于定义目录权限的参数Directory后面的路径也修改为/home/wwwroot。

[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
122 DocumentRoot "/home/wwwroot"

#
# Relax access to content within /var/www.
#
127 <Directory "/home/wwwroot">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# Further relax access to the default document root:
134 <Directory "/home/wwwroot">
    #

step3:重新启动httpd服务程序并验证结果,怎么提示“Forbidden”?!

step4:其实这里是selinux在捣鬼,,我们可以进入selinux的主配置文件,将默认开始的enforcing改为permissive,或者用setenforce命令。

[root@linuxprobe ~]# vim /etc/selinux/config 
[root@linuxprobe ~]# setenforce 0
[root@linuxprobe ~]# getenforce
Permissive

这时候再刷新一下网页就发现ok了

 实验一反思:httpd的功能是允许用户请求网站服务的,但是为什么当我们将网站的默认路径改为/home/wwwroot就被禁止了呢,原因就是/home目录是用来存放普通用户的家目录数据的,而现在httpd提供的网站服务却要去获取普通用户家目录的数据,显然违反了规则。我们现在先将SELinux服务恢复为强制模式,然后分别查看原始网站数据的保存目录及当前网站数据的保存目录是否拥有不同的selinux安全上下文值。

step5:输入ls -Zd/var/www/html命令,-Z参数用于查看文件的安全上下文值

[root@linuxprobe ~]# setenforce 1
[root@linuxprobe ~]# ls -Zd /var/www/html
system_u:object_r:httpd_sys_content_t:s0 /var/www/html  //httpd_sys_content_t代表网站服务的系统文件
[root@linuxprobe ~]# ls -Zd /home/wwwroot
unconfined_u:object_r:user_home_dir_t:s0 /home/wwwroot

实验二:针对以上的问题,我们可以向新的网站数据目录新添加一条SELinux安全上下文,让这个目录以及里面的所有文件都能够被httpd服务程序访问到。

step1:

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

step2:在执行上述设置后,还无法立即访问网站,还需使用restorecon命令将设置好的上下文生效

[root@linuxprobe ~]# restorecon -Rv /home/wwwroot/
Relabeled /home/wwwroot from unconfined_u:object_r:user_home_dir_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /home/wwwroot/index.html from unconfined_u:object_r:user_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

实验三:如果zhsan这个用户需要建立一个网站只给自己访问,要怎么做呢?

step1:需要编辑其配置文件,在17行前加#,表示开启个人主页功能;24行前的#去掉,UserDir参数表示网站数据在用户家目录中的保存目录名称,即public_html目录。

[root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf
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).
    #
17 #    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:
    # 
24    UserDir public_html
</IfModule>

 step2:在家目录建立用于保存网站数据的目录及首页面文件,还需要吧家目录的权限修改为755,保证其他人也可以读取。同时重启httpd服务。

[root@linuxprobe ~]# su - zhsan
[zhsan@linuxprobe ~]$ mkdir public_html
[zhsan@linuxprobe ~]$ echo "nothing happens">public_html/index.html
[zhsan@linuxprobe ~]$ chmod -R 755 /home/zhsan 
[zhsan@linuxprobe ~]$ exit
logout
[root@linuxprobe ~]# systemctl restart httpd
[root@linuxprobe ~]# 

step3:在浏览器输入“网址/~用户名”,刷新发现还是被禁用。

 step4:首先用户在使用httpd服务程序访问网站数据时,因为网站数据时存储到自己的家目录的,因此用户自己就是可以访问的,所以应该不需要修改SELinux上下文。那么在前面提到过SELinux域的概念,SELinux域确保服务程序不能执行违规的操作,那么httpd服务开启的这项个人用户主页功能到底有没有被SELinux域默认允许呢?

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

 step5:使用setsebool命令,后面加 “-p” 参数,让修改后的SELinux策略规则永久生效且立即生效。然后刷新网页!

step6:有时候某位用户只想让自己的网页内容显示给自己,那么可以给该网站设置密码。先使用htpasswd命令生成密码数据库。-c参数表示第一次生成,然后再分别添加密码数据库的存放文件,以及验证要用到的用户名称。

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

 step7:修改配置文件31~37行的参数信息。重启httpd服务程序。

[root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf
······
</IfModule>

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
31 <Directory "/home/*/public_html">
32     AllowOverride all
33    authuserfile "/etc/httpd/passwd"  //生成出的密码验证文件保存路径
34    authname "My life have no ending!"  //当用户访问网站时的提示信息
35    authtype basic                    //验证方式为密码模式
36    require user zhsan                //访问网站时需要验证的用户名称
37 </Directory>

 


学些小结:

        大家可以看到这一节的内容偏多且比较复杂,需要一定的耐心及探索精神才可以完成,但是各位小伙伴一定要深入试验一下,去感受一下Apache服务带来的魅力。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值