先关闭防火墙
[root@localhost red9]# systemctl stop firewalld
[root@localhost red9]# setenforce 0
创建目录和index.html文件
[root@localhost red9]# mkdir /www/{data,openlab,student,money} -pv
mkdir: created directory '/www/data'
mkdir: created directory '/www/openlab'
mkdir: created directory '/www/student'
mkdir: created directory '/www/money'
[root@localhost red9]# echo student information > /www/student/index.html
[root@localhost red9]# echo teaching data > /www/data/index.html
[root@localhost red9]# echo pay information > /www/money/index.html
写配置文件(在/etc/httpd/conf.d/下新建一个配置文件vhosts.conf)
[root@localhost ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost 192.168.239.134:80>
ServerName www.openlab.com #域名
DocumentRoot /www/openlab #index.html文件所在路径
Alias /student /www/student #别名,看似访问/student,实际访问/www/student
Alias /data /www/data
</VirtualHost>
<VirtualHost 192.168.239.134:443> #https基于443端口工作
ServerName www.openlab.com
DocumentRoot /www/money
Alias /money /www/money
sslengine on
SSLCertificateFile /etc/pki/tls/certs/openlab.crt #认证文件
SSLCertificateKeyFile /etc/pki/tls/private/ openlab.key #认证文件所对应的私钥路径
</VirtualHost>
<directory /www/student>
authtype basic
authname "please login:"
authuserfile /etc/httpd/openlab #认证用户文件
require user song tian #指定允许访问的用户
</directory>
<directory /www>
allowoverride none
require all granted
</directory>
在windows本机的hosts文件内写入www.openlab.com的域名解析
windows的hosts文件在C:\Windows\System32\drivers\etc路径下
在该文件内最后写入192.168.x.x(这里写改域名对应的ip地址) www.openlab.com
保存退出
第五步:验证实验
访问http://www.openlab.com
访问http://www.openlab.com/data
访问http://www.openlab.com/student
访问https://www.openlab.com/money