Apahce配置

安装apache软件

yum  install httpd

重启服务,加入开机启动

systemctl restart httpd
systemctl enable httpd

编辑index.html文件

# cd /var/www/html
# echo "hello world" > index.html

重启httpd服务,打开浏览器,输入IP地址就可以访问网页“hello world”

切换路径 --- 编辑主配置文件

# vim /etc/httpd/conf/httpd.conf 
  1. 修改网站的存放路径,119行。
  2. 重启,加入开机启动
# systemctl restart httpd
# systemctl enable httpd

这个时候打开浏览器是访问不了的,因为selinux的安全上下文不符合要求,接下来我们配置selinux    

SELinux

安全子系统。进一步保护Linux的安全。从两个维度保护。

  1. SELinux安全上下文(文件上做限制,只能获取该服务所需要的的资源)
  2. SELinux域(限制服务功能)

配置方法

cat /etc/selinux/config

临时配置并查看

# getenforce 
Enforcing
# setenforce 
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
# setenforce 0
# getenforce 
Permissive

  具体看看...

  1. 安全上下文 如何查看安全上下文?ls -lZd,大写Z,d用来看目录。
# ls -lZd /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
# ls -lZd /home/wwwroot/
drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot/

如何修改安全上下文? 注意:-a是修改的意思,-t 添加安全上下文 对目录修改的时候 不要加斜杠。 不仅要修改目录的安全上下文,还要修改目录里面文件的安全上下文。

# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
# restorecon -Rv /home/wwwroot
restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:user_home_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/.index.html.swo context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:user_home_t:s0

使修改立即生效,-R 递归操作,-v显示过程

# restorecon -Rv /home/wwwroot

  2. SELinux安全域 userdir.conf 专门用来开启个人用户主页功能 默认情况下是禁用的,因此要在配置文件里将其开启。

cat /etc/httpd/conf.d/userdir.conf

  打开浏览器,浏览用户linuxprobe的家目录,无法访问对应的index.html。

获取selinux对指定服务有哪些限制 注意,如要要查看某个服务,可以通过管道符 grep httpd筛选

# getsebool -a | grep httpd

打开httpd的某个安全域的开关

# setsebool -P httpd_enable_homedirs=on

解决了selinux的域的问题,就可以访问了 个人用户主页加密

# htpasswd -c /etc/httpd/passwd linuxprobe
New password: 
Re-type new password: 
Adding password for user linuxprobe


虚拟主机

  1. 基于IP地址
  2. 基于域名
  3. 基于端口号

具体配置方法

  1. 基于IP地址 1.1. 配置网卡,配置三个IP 1.2.创建三个目录
# mkdir -p /home/wwwroot/10
# mkdir -p /home/wwwroot/20
# mkdir -p /home/wwwroot/30
# cd /home/wwwroot/
# echo "1010 helloworld" > 10/index.html
# echo "2020 helloworld" > 20/index.html
# echo "3030 helloworld" > 30/index.html

1.3.编辑配置文件,添加虚拟主机配置信息 ![](https://s4.51cto.com/images/blog/202005/08/bb6a796965d51acf2bfa36be2509e9a6.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdG[root@localhost ~]# systemctl restart httpd [root@localhost ~]# systemctl enable httpd k=) 1.4.重启httpd服务

[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# systemctl enable httpd

1.5. 访问浏览器,输入IP地址 1.6.临时关闭selinux,确认问题点

setenforce 0

发现确实是selinux的问题,然后开启selinux

setenforce 1

1.7. 设置selinux的安全上下文和域

# ls -lZd /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
# cd /home/wwwroot/
# 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/20
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/30
# 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/30/*

1.8. 立即生效

# 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/linuxporbe context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:user_home_t:s0
restorecon reset /home/wwwroot/linuxporbe/hahah context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:user_home_t:s0
restorecon reset /home/wwwroot/10 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/10/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/20 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/20/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/30 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/30/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

1.9. 访问浏览器,生效 2. 基于域名的虚拟主机 2.1. 创建目录

# mkdir /wwwroot
# cd wwwroot/
# mkdir www
# mkdir bbs
# mkdir tech
# echo "www hello world" > ./www/index.html
# echo "bbs hello world" > ./bbs/index.html
# echo "tech hello world" > ./tech/index.html

  2.2. 编辑hosts文件,模拟多个域名   2.3.编辑httpd的配置文件

120 <virtualhost 192.168.10.10>
121 documentroot /home/wwwroot/www
122 servername www.linuxprobe.com
123 <directory /home/wwwroot/www>
124 allowoverride none
125 require all granted
126 </directory>
127 </virtualhost>
128 
129 <virtualhost 192.168.10.10>
130 documentroot /home/wwwroot/bbs
131 servername bbs.linuxprobe.com
132 <directory /home/wwwroot/bbs>
133 allowoverride none
134 require all granted
135 </directory>
136 </virtualhost>
137 
138 <virtualhost 192.168.10.10>
139 documentroot /home/wwwroot/tech
140 servername tech.linuxprobe.com
141 <directory /home/wwwroot/tech>
......................

  2.4. 重启httpd服务,通过域名访问网页报错   2.5. 配置selinux上下文

   68  semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
   58  semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www
   59  semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www/*
   60  semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs
   61  semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs/*
   62  semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech
   63  semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech/*
   64  restorecon -Rv /home/wwwroot

2.6. 基于域名虚拟主机网页生效 3. 基于端口的虚拟主机配置