练习:搭建虚拟主机
(1)基于主机名实现三个虚拟主机
(2) 每虚拟主机使用独立的访问日志和错误日志
(3) 第三个虚拟主机的/admin要进行用户访问认证
(4) 在第二个虚拟主机上提供/status;
(5) 在第三个虚拟主机提供路径别名/bbs,访问其它文件系统路径;
(6) 尝试使用混合类型的虚拟主机:基于IP,PORT和ServerName
[root@Centos7 httpd]# cat /etc/httpd/conf.d/vhost.conf ###虚拟主机1 <VirtualHost 192.168.1.3:80 > ServerName www.centos1.com DocumentRoot "/var/www1/html" ErrorLog "logs/www1_error_log" CustomLog "logs/www1_access_log" combined <Directory "/var/www1/html"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> ###虚拟主机2 <VirtualHost 10.1.3.25:80 > ServerName www.centos2.com DocumentRoot "/var/www2/html" ErrorLog "logs/www2_error_log" CustomLog "logs/www2_access_log" combined <Directory "/var/www2/html"> Options None AllowOverride None Require all granted </Directory> <Location "/status"> SetHandler server-status Require all granted </Location> </VirtualHost> ###虚拟主机3 <VirtualHost 10.1.3.25:8080 > ServerName www.centos3.com DocumentRoot "/var/www3/html" ErrorLog "logs/www3_error_log" CustomLog "logs/www3_access_log" combined Alias /bbs "/bbs/fstab" <Directory "/var/www3/html/admin"> Options None AllowOverride None AuthName "Admin Login." AuthType Basic AuthUserFile "/var/www3/.pass" Require user li </Directory> <Directory "/var/www3/html"> Options Indexes AllowOverride None Require all granted </Directory> <Directory "/bbs/fstab"> Options None AllowOverride None Require all granted </Directory> </VirtualHost>
练习2:使用脚本实现以上功能;
每虚拟使用单独的配置文件;
脚本可接受参数,参数虚拟主机名称;
#!/bin/bash # #判断是否为变量path是否为quit,如果是,则退出。 while [[ $path != "quit" ]];do read -p "Please Input Save Path: " path if [[ $path == "quit" ]];then break fi #读取变量参数 read -p "Please Input ServerName: " SName read -p "Please Input IP: " ip read -p "Please Input port: " port #截取本机ip用于判断 localip=$(ip ad sho | awk '/inet\>/{print $2}' | awk -F'/' '!/^127/{print $1}') #判断及创建 if ! [ -d $path ] && [ -n $path ];then mkdir -p $path &>/dev/null if echo $localip | grep "$ip" &>/dev/null ;then if [ $port -lt 1023 ];then echo "<VirtualHost "$ip":"$port">" >>$path/index.html echo -e "\tServerName $SName" >>$path/index.html echo -e "\tDocumentRoot \""$path"\"" >>$path/index.html echo "</VirtualHost>" >>$path/index.html else echo "Sorry $port max" fi else echo "Sorry $ip false" fi else echo "Sorry path exist" fi done
原创文章,作者:Lii,如若转载,请注明出处:http://www.178linux.com/49701