apache自动化脚本搭建虚拟主机

1 基于主机名实现三个虚拟主机

(1).创建网站目录与测试文件

图片1.png

(2).创建虚拟主机文件(目录为/etc/httpd/conf.d/下)blob.png

 

(3).修改测试apache主机hosts文件(就不用DNS服务器解析,方便测试),测试httpd配置文件是否有问题,重启httpd服务

blob.png

 

(4)修改测试主机hosts文件(就不用DNS服务器解析,方便测试),在用firefox浏览器分别打开测试网站,出现下图即可

blob.png

 

2 每虚拟主机使用独立的访问日志和错误日志

   (1).在对应虚拟主机内添加对应的日志文件 ,内容为(

   ErrorLog "logs/node2_mage_error_log(自定义错误日志)"

   LogLevel warn

   CustomLog "logs/node2_mage_access_log(自定义访问日志)" combined

)  ,后重新加载httpd服务

blob.png

(2)在站点下创建测试test目录,但是不给授权操作,分别访问网站与目录,让httpd服务创建访问与错误日志

blob.png

 

(3)查看虚拟主机自定访问日志与错误日志

blob.png

 

3. 第三个虚拟主机的/admin要进行用户访问认证 

  (1).配置虚拟主机3认证

blob.png

<Directory "/www/node3.mage.com/admin">

     Options None

     AllowOverride None

     AuthType Basic

     AuthName "Please,Enter your name and password"

     AuthUserFile "/etc/httpd/conf.d/.htpasswd"

     Require user admin

  </Directory>

   

(2).创建目录与配置文件,重启服务器

blob.png

          

(3).修改测试主机解析文件,方便测试

blob.png

        

(4).输入密码,查看测试页面

blob.png

                    

4 .在第二个虚拟主机上提供/status;

(1).配置虚拟主机2,增加服务器状态查看页面配置内容,重载服务

blob.png

             

(2).查看结果

blob.png

 

 5.在第三个虚拟主机提供路径别名/bbs,访问其它文件系统路径;

   (1)创建访问目录与测试文件

blob.png

  (2)httpd配置文件修改,增加别名访问与相应目录访问权限修改

blob.png

    

(3)重启服务器,测试

blob.png

    

6.尝试使用混合类型的虚拟主机:基于IP,PORT和ServerName

 (1)修改虚拟主机1,让组件ip不同于其他。修改虚拟主机2,使其端口与其他端口不同。虚拟主机3不做修改,使其基于FDQN访问

blob.png

(2)重启apache服务,查看对应端口是否打开

blob.png

(3)访问测试

blob.png

 

 练习2:使用脚本实现以上功能;

每虚拟使用单独的配置文件;

脚本可接受参数,参数虚拟主机名称;

程序执行情况:

blob.png

 生成配置文件:

blob.png

 

 程序代码:

 #!/bin/bash

#auther:maoxiong date:16-10-07

echo "####################################################################"

echo "#     Virtual Machine Creation Tool V 1.0                          #"

echo "#                          author:MaoXiong                         #"

echo "#     Please complete the virtual machine creation according to    #"

echo "#     the prompt                                                   #"

echo "####################################################################"

vstat=0           #循环状态量

vHost=' '         #ServerName

vDroot=' '        #DocumentRoot

vipport=' '        #虚拟主机IP与PORT

lport=' '

authdir=' '        #认证目录

authuser=' '         #认证用户

authpw=' '       #认证用户

while [ $vstat -eq 0 ];do

   read -p "Please enter a VirtaulHost name and host directory, separated by spaces : " vhost vroot

   [ -e /etc/httpd/conf.d/$vhost.conf ] && echo "host is exits ." || vHost="$vhost"

   grep -o "$vroot" /etc/httpd/conf.d/* && echo "ServerDir is exits" || vDroot=$vroot

   [ -n $vhost ] && [ -n $vroot ] &&  vstat=1

done ; vstat=0

while [ $vstat -eq 0 ];do

   read -p "Please enter IP with port, for example 1.1.1.1 80 : " vport vport1

   if [ -z $vport ] ;then 

        echo "IP or port is null ." 

   else

         vipport=$vport:$vport1

         lport=$vport1; vstat=1

   fi

done; vstat=0

while [ $vstat -eq 0 ] ;do

    read -p "Please enter the authentication directory : " dir

    read -p "Please enter the authentication user name and password, separated by spaces : " user pd

    authdir=$dir; authuser=$user; 

    [ -e /etc/httpd/conf.d/.htpasswd ] || touch /etc/httpd/conf.d/.htpasswd

    htpasswd -mb /etc/httpd/conf.d/.htpasswd $user $pd

    authpw=`grep "$user" /etc/httpd/conf.d/.htpasswd | cut -d: -f2`

    vstat=1

done

echo "——————————————————————–"

echo "Please,Confirm configuration information"

echo "VirtualHost  ——-> $vHost"

echo "DocumentRoot ——-> $vDroot"

echo "VirtualHost IP and Port —-> $vipport"

echo "Certification directory —-> $authdir"

echo "Authenticate user       —-> $authuser"

echo "Authenticate password   —-> $authpw"

echo "ErrorLog  —-> /var/log/httpd/$vHost_error.log "

echo "AccessLog —-> /var/log/httpd/$vhost_access.llog "

echo "——————————————————————–"

read -p "Confirm the installation of the virtual host,(y or n) : " confy 

   [ "$confy" == "n" ] && exit || echo "installation……" 

vhdir=/etc/httpd/conf.d/$vHost.conf

touch /etc/httpd/conf.d/$vHost.conf

if [ $lport -ne 80 ];then

echo "Listen $lport"  >> $vhdir

fi

echo "<VirtualHost $vipport>"   >> $vhdir

echo "   ServerName $vHost"     >> $vhdir

  mkdir -p /www/$vDroot

echo "   DocumentRoot \"/www/$vDroot\""  >> $vhdir

echo "   <Directory \"/www/$vDroot\">" >> $vhdir

echo "   Options None"    >> $vhdir 

echo "   AllowOverride None"  >> $vhdir

echo "   Require all granted"  >> $vhdir

echo "  </Directory>"  >> $vhdir

echo "  <Directory \"/www/$vDroot/$authdir\">"  >> $vhdir

echo "  AuthType basic"   >> $vhdir

echo "  AuthName \"Admin Area, Enter your name/password\""  >> $vhdir

echo "  AuthUserFile \"/etc/httpd/conf/.htpasswd\""  >> $vhdir

echo "  Require user $authuser"  >> $vhdir

echo "  </Directory>  " >> $vhdir

echo "  <Location /status> "  >> $vhdir

echo "  SetHandler server-status " >> $vhdir

echo "  Require all granted" >> $vhdir

echo "  </Location>" >> $vhdir

echo "  CustomLog \"logs/$vhost_access.log\" combined "  >> $vhdir

echo "  ErrorLog \"logs/$vhost_error.log\"" >> $vhdir

echo " </VirtualHost> ">> $vhdir

echo "installation is complete ."

 

          

原创文章,作者:maoxiong,如若转载,请注明出处:http://www.178linux.com/50131