RHEL7-使用Apache服务部署静态网站

1. 安装Apache服务程序

1.1 在虚拟机中选中光盘镜像,并设置连接

1.2 将光盘设备挂载到/media/cdrom目录

[root@localhost ~]# mkdir -p /media/cdrom
[root@localhost ~]# mount /dev/cdrom /media/cdrom/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]#

1.3 创建yum仓库的配置文件,在结尾处添加内容:

[root@localhost ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo

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

1.4 安装Apache服务程序

(注意,apache的软件包名称为httpd,同时因为我的httpd软件包已是最新版本,

所以程序并没有再次更新,如果首次部署的话会提示安装或者更新版本的信息)

[root@localhost ~]# yum install httpd
已加载插件:fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base                                                     | 3.6 kB     00:00
extras                                                   | 3.4 kB     00:00
rhel7                                                    | 4.1 kB     00:00
updates                                                  | 3.4 kB     00:00
(1/4): rhel7/group_gz                                      | 136 kB   00:00
(2/4): rhel7/primary_db                                    | 3.6 MB   00:00
(3/4): extras/x86_64/primary_db                            | 160 kB   00:00
(4/4): updates/x86_64/primary_db                           | 7.1 MB   00:04
Loading mirror speeds from cached hostfile
软件包 httpd-2.4.6-40.el7.centos.4.x86_64 已安装并且是最新版本
无须任何处理

1.5 运行Apache服务并设置开机启动

[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /etc/systemd/system/httpd.service.

1.6 浏览器访问http://127.0.0.1,确认是否可以看到默认页面

2. 配置服务文件参数

2.1 httpd服务各目录的作用

 2.2 /etc/httpd/conf/httpd.conf文件的详细

默认的网站数据是存放在/var/www/html目录中的,首页名称是index.html,

使用echo命令将指定的字符写入到网站数据目录中的index.html文件中

[root@localhost ~]# echo "Welcome To Linux World ! " > /root/httpd.conf.txt

打开浏览器再次访问127.0.0.1,可以正常访问刚刚编辑过的网页。

编辑Apache服务程序的主配置文件:

119行 DocumentRoot "/var/www/html"    ->   DocumentRoot "/home/wwwroot"

124行 <Directory "/var/www">    ->   <Directory "/home/wwwroot">

建立网站数据目录并创建新的index.html网页:

[root@localhost ~]# mkdir /home/wwwroot
[root@localhost ~]# echo "The new web site" > /home/wwwroot/index.html

重启apache服务后再次查看127.0.0.1,发现修改后的index.html并没有被显示

[root@localhost ~]# systemctl restart httpd.service

尝试访问http://127.0.0.1/index.html,发现抛403错误,拒绝访问

 为什么会出现被就禁止的情况呢?答案是因为SELinux。

3. 强制访问控制安全子系统SELinux

 SELinux全称为Security-Enhanced Linux,是美国国家安全局在Linux社区帮助下开发的

一个强制访问控制的安全子系统,SELinux属于MAC强制访问控制(Mandatory AccessControl)

即让系统中的各个服务进程都受到约束,仅能访问到所需要的文件。

原文地址:https://www.cnblogs.com/tdcqma/p/5868941.html