实验一 基于IP地址
1 将多个IP地址绑定在同一网卡上
vim /etc/sysconfig/network-scripts/ifcfg-ens160
ONBOOT=yes
IPADDR0=192.168.0.10
IPADDR1=192.168.0.20
IPADDR2=192.168.0.30
2 重载网卡
nmcli connection reload ens160
nmcli connection up ens160
3 分别在/home/wwwroot中创建用于保存不同网站数据的3个目录,并向其中分别写入网站的首页文件
mkdir -p /home/wwwroot/10
mkdir -p /home/wwwroot/20
mkdir -p /home/wwwroot/30
echo "IP:192.168.0.10" > /home/wwwroot/10/index.html
echo "IP:192.168.0.20" > /home/wwwroot/20/index.html
echo "IP:192.168.0.30" > /home/wwwroot/30/index.html
4 从httpd服务的配置文件中大约第132行处开始,分别追加写入3个基于IP地址的虚拟主机网站参数,然后保存并退出
vim /etc/httpd/conf/httpd.conf
132 <VirtualHost 192.168.0.10>
133 DocumentRoot /home/wwwroot/10
134 ServerName 192.168.0.10
135 <Directory /home/wwwroot/10>
136 AllowOverride None
137 Require all granted
138 </Directory>
139 </VirtualHost>
140 <VirtualHost 192.168.0.20>
141 DocumentRoot /home/wwwroot/20
142 ServerName 192.168.0.20
143 <Directory /home/wwwroot/20>
144 AllowOverride None
145 Require all granted
146 </Directory>
147 </VirtualHost>
148 <VirtualHost 192.168.0.30>
149 DocumentRoot /home/wwwroot/30
150 ServerName 192.168.0.30
151 <Directory /home/wwwroot/30>
152 AllowOverride None
153 Require all granted
154 </Directory>
155 </VirtualHost>
systemctl restart httpd
systemctl enable httpd
5 修改/home/wwwroot目录的SElinux安全上下文
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
restorecon -Rv /home/wwwroot
6 分别在浏览器输入3个IP地址查看效果
实验二 基于主机域名
1 手动定义IP地址与域名之间对应关系的配置文件,进行强制解析
vim /etc/hosts
192.168.10.10 www.linuxprobe.com www.linuxcool.com www.linuxdown.com
2 分别在/home/wwwroot中创建用于保存不同网站数据的3个目录,并向其中分别写入网站的首页文件
mkdir -p /home/wwwroot/linuxprobe
mkdir -p /home/wwwroot/linuxcool
mkdir -p /home/wwwroot/linuxdown
echo "www.linuxprobe.com" > /home/wwwroot/linuxprobe/index.html
echo "www.linuxcool.com" > /home/wwwroot/linuxcool/index.html
echo "www.linuxdown.com" > /home/wwwroot/linuxdown/index.html
3 从httpd服务的配置文件中大约第132行处开始,分别追加写入3个基于主机名的虚拟主机网站参数
vim /etc/httpd/conf/httpd.conf
132 <VirtualHost 192.168.0.10>
133 Documentroot /home/wwwroot/linuxprobe
134 ServerName www.linuxprobe.com
135 <Directory /home/wwwroot/linuxprobe>
136 AllowOverride None
137 Require all granted
138 </Directory>
139 </VirtualHost>
140 <VirtualHost 192.168.0.10>
141 Documentroot /home/wwwroot/linuxcool
142 ServerName www.linuxcool.com
143 <Directory /home/wwwroot/linuxcool>
144 AllowOverride None
145 Require all granted
146 </Directory>
147 </VirtualHost>
148 <VirtualHost 192.168.0.10>
149 Documentroot /home/wwwroot/linuxdown
150 ServerName www.linuxdown.com
151 <Directory /home/wwwroot/linuxdown>
152 AllowOverride None
153 Require all granted
154 </Directory>
155 </VirtualHost>
systemctl restart httpd
systemctl enable httpd
4 修改/home/wwwroot目录的SElinux安全上下文
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
restorecon -Rv /home/wwwroot
5 分别在浏览器输入3个域名地址测试效果。
实验三 基于端口号
1 分别在/home/wwwroot中创建用于保存不同网站数据的3个目录,并向其中分别写入网站的首页文件
mkdir -p /home/wwwroot/6111
mkdir -p /home/wwwroot/6222
mkdir -p /home/wwwroot/6333
echo "port:6111" > /home/wwwroot/6111/index.html
echo "port:6222" > /home/wwwroot/6222/index.html
echo "port:6333" > /home/wwwroot/6333/index.html
2 在httpd服务配置文件的第46行~48行分别添加用于监听6111、6222和6333端口的参数
vim /etc/httpd/conf/httpd.conf
44 #Listen 12.34.56.78:80
45 Listen 80
46 Listen 6111
47 Listen 6222
48 Listen 6333
3 从httpd服务的配置文件中大约第134行处开始,分别追加写入3个基于端口号的虚拟主机网站参数
vim /etc/httpd/conf/httpd.conf
134 <VirtualHost 192.168.10.10:6111>
135 DocumentRoot /home/wwwroot/6111
136 ServerName www.linuxprobe.com
137 <Directory /home/wwwroot/6111>
138 AllowOverride None
139 Require all granted
140 </Directory>
141 </VirtualHost>
142 <VirtualHost 192.168.10.10:6222>
143 DocumentRoot /home/wwwroot/6222
144 ServerName www.linuxcool.com
145 <Directory /home/wwwroot/6222>
146 AllowOverride None
147 Require all granted
148 </Directory>
149 </VirtualHost>
150 <VirtualHost 192.168.10.10:6333>
151 DocumentRoot /home/wwwroot/6333
152 ServerName www.linuxdown.com
153 <Directory /home/wwwroot/6333>
154 AllowOverride None
155 Require all granted
156 </Directory>
157 </VirtualHost>
systemctl restart httpd
systemctl enable httpd
4 使用semanage命令查询并过滤出所有与HTTP协议相关且SELinux服务允许的端口列表
semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
5 添加3个端口号到SELinux允许的与HTTP协议相关的端口号中
semanage port -a -t http_port_t -p tcp 6111
semanage port -a -t http_port_t -p tcp 6222
semanage port -a -t http_port_t -p tcp 6333
systemctl restart httpd
systemctl enable httpd
6 修改/home/wwwroot目录的SElinux安全上下文
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
restorecon -Rv /home/wwwroot
7 分别在浏览器输入IP地址:3个端口号测试效果