centOS7.6精简版上配置Apache服务

  • 对于 Apache 服务的介绍及作用这里不做说明,只记录Apache服务的配置过程,及一些使用方式。操作系统环境是 CentOS 7.6 精简版,CentOS主机的IP地址是192.168.0.40、192.168.10.40两个。配置过程如下:
[root@localhost ~]# yum install -y httpd	#第1步:安装Apache服务程序,安装过程省略
[root@localhost ~]# systemctl restart httpd		#启动httpd服务
[root@localhost ~]# systemctl enable httpd		#加入开机启动项中
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
#下面命令设置 windows 主机可以访问
[root@localhost ~]# iptables -I INPUT -s 192.168.0.5 -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# iptables -I INPUT -s 192.168.10.1 -p tcp --dport 80 -j ACCEPT
#下面命令设置允许所有人可以访问,上面两条命令可以省略
[root@localhost ~]# iptables -I INPUT -s 0.0.0.0/0 -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# iptables-save	#保存防火墙设置,下次重启仍然有效
#低版本Linux保存防火墙设置命令是:service iptables save
#现在在windows电脑的浏览器访问192.168.0.40、192.168.10.40均可访问到 Apache 的测试页面
[root@localhost ~]# cd /var/www/html/		#进入到保存网站文件的目录
[root@localhost html]# echo '<h1>你好,网站服务程序</h1>' > index.html	#创建首页文件
#现在在windows电脑的浏览器上可以正常看到index.html文件中的内容
  • 接下来配置将默认的网站存放目录 /var/www/html 修改为 /home/wwwroot 目录。修改服务的默认保存目录,要涉及到 SELinux 安全上下文技术限制。ls 命令的 -Z 参数可查看目录的 SELinux 权限。配置过程如下:
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf	#编辑httpd配置文件
......
DocumentRoot "/home/wwwroot"	#修改这行的值
#
# Relax access to content within /var/www.
#
<Directory "/home/wwwroot">		#还要修改这一行的值
......
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# mkdir -p /home/wwwroot
[root@localhost ~]# ls -ldZ /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
#上面输出可看到 /var/www/html 目录有 httpd_sys_content_t 权限
[root@localhost ~]# ls -ldZ /home/wwwroot
drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot
[root@localhost ~]# cat /etc/selinux/config		#查看SELinux的值
SELINUX=enforcing	#关注这行即可,enforcing表示强制,还有permissive、disalbed
#setenforce 0 命令临时关闭 SELinux,setenforce 1 临时开启SELinux
#semanage 命令修改或添加 SELinux域,fcontext参数表明要编辑SELinux值
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
[root@localhost ~]# restorecon -Rv /home/wwwroot	#让SELinux值生效
restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]# echo '<h2>hello Apache</h2>' > /home/wwwroot/index.html

现在在 windows 电脑的浏览器上访问192.168.0.40、192.168.10.40,都能正常看到 /home/wwwroot/index.html 文件的内容。实验配置成功。

  • 为每个用户配置一个网站。在用户家目录下创建一个 public_html 目录,在这个目录中存放html文件。要想通过浏览器正常访问这个目录下的html文件,需要修改 userdir.conf 配置文件。此外还要设置 SELinux 域允许httpd服务程序访问用户目录下的文件。操作如下:
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf	#修改配置文件
 17     UserDir public_html		#将17行的 disabled修改为 public_html
[root@localhost ~]# systemctl restart httpd		#重启服务
[root@localhost ~]# su - tom		#切换到tom用户
[tom@localhost ~]$ mkdir public_html	#创建public_html目录
[tom@localhost ~]$ chmod 755 -Rf public_html	#修改目录权限,设置小一些
[tom@localhost ~]$ chmod 755 -Rf /home/tom	#还要修改tom用户家目录的权限
[tom@localhost ~]$ cd public_html/
[tom@localhost public_html]$ echo "<h3>hello, tom</h3>" > index.html
[tom@localhost public_html]$ exit	#退出tom用户
logout
[root@localhost ~]# getsebool -a | grep httpd
httpd_enable_homedirs --> off	#将这个设置为on开启访问用户家目录的权限
[root@localhost ~]# setsebool -P httpd_enable_homedirs=on	#开启权限

现在访问 http://192.168.10.40/tom/、http://192.168.0.40/tom/ 就可正常看到 tom 用户家目录下public_html 目录下的 index.html 文件内容,当然也可以访问该目录下的其它文件内容。

  • 接下来设置访问用户家目录下的网站需要输入密码。配置过程如下:
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf	#第1步:编辑这个配置文件
 31 <Directory "/home/*/public_html">	#将31行directory标签内容修改为这样
 32     AllowOverride all
 33     authuserfile "/etc/httpd/passwd"	#认证对应的文件
 34     authname "Please enter User ID and Password"	#提示信息
 35     authtype basic		#认证类型,基础认证:用户名+密码
 36     require user tom	#访问网站时需要验证的用户名称
 37 </Directory>
[root@localhost ~]# htpasswd -c /etc/httpd/passwd tom #第2步:生成tom用户的密码数据库
New password: 			#提示输入密码一个密码,这个密码不是tom用户登录shell的密码
Re-type new password: 
Adding password for user tom
[root@localhost ~]# systemctl restart httpd		#第3步:重启httpd服务

现在访问 http://192.168.10.40/tom/、http://192.168.0.40/tom/ 就要输入正确的用户名和密码才能访问。


  • 接下来配置虚拟主机功能:基于IP地址对服务器硬件资源进行切割,让用户能准确访问到想要的数据。这个实验会利用本节前面已经创建好的 /home/wwwroot 目录,在这个目录下创建3个子目录来实验虚拟网站主机功能。配置过程如下:
[root@localhost ~]# cd /home/wwwroot/
[root@localhost wwwroot]# ls
index.html
[root@localhost wwwroot]# mkdir 40	#第1步:创建3个子目录
[root@localhost wwwroot]# mkdir 41
[root@localhost wwwroot]# mkdir 42
#第2步:编辑网卡参数配置文件
[root@localhost wwwroot]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
......
IPADDR0=192.168.0.40
IPADDR1=192.168.0.41		#在这个网卡配置文件中添加41、42两个IP地址
IPADDR2=192.168.0.42
......
#重启网络服务,并保证40、41、42三个IP都能正常ping通
[root@localhost wwwroot]# systemctl restart network	
#第3步:向3个子目录中分别写入一个 index.html 文件
[root@localhost wwwroot]# echo "<h1>11111111</h1>" > 40/index.html
[root@localhost wwwroot]# echo "<h1>22222222</h1>" > 41/index.html
[root@localhost wwwroot]# echo "<h1>33333333</h1>" > 42/index.html
#第4步:编辑Apache配置文件
[root@localhost wwwroot]# vim /etc/httpd/conf/httpd.conf	#编辑apache配置文件
113 <virtualhost 192.168.0.40>			#在113行处开始添加下面的内容
114     documentroot /home/wwwroot/40
115     <directory /home/wwwroot/40>
116         allowoverride none
117         require all granted
118     </directory>
119 </virtualhost>
120 <virtualhost 192.168.0.41>
121     documentroot /home/wwwroot/41
122     <directory /home/wwwroot/41>
123         allowoverride none
124         require all granted
125     </directory>
126 </virtualhost>
127 <virtualhost 192.168.0.42>
128     documentroot /home/wwwroot/42
129     <directory /home/wwwroot/42>
130         allowoverride none
131         require all granted
132     </directory>
133 </virtualhost>
......
[root@localhost wwwroot]# systemctl restart httpd	#重启服务
#第5步:给wwwroot 目录及其子目录添加 SELinux 域权限,子目录下的文件也需要添加
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/40
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/40/*
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/41
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/41/*
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/42
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/42/*

现在在windows主机的浏览器中访问192.168.0.40、192.168.0.41、192.168.0.42,可以正常访问到相应的资源。

原文地址:https://www.cnblogs.com/Micro0623/p/15648586.html