http2

练习:

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

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

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

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

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

    (6) 尝试使用混合类型的虚拟主机:

    基于IP,PORT和ServerName

<VirtualHost 10.1.0.249:80>
    ServerName www.a.com
    DocumentRoot "/www/a/html/"
    <Directory "/www/a/html/">
        Options none
        AllowOverride none
        require all granted
    </Directory>
    ErrorLog "/www/a/html/error_log"
    LogLevel warn
    CustomLog "/www/a/html/access_log" combined
</VirtualHost>

Listen 8080
<VirtualHost 10.1.0.249:8080>
    ServerName www.b.com
    DocumentRoot "/www/b/html/"
    <Directory "/www/b/html/">
        Options none
        AllowOverride none
        require all granted
    </Directory>
    <Location /status>
        Sethandler server-status
        Require all granted
    </Location>
    ErrorLog "/www/b/html/error_log"
    LogLevel warn
    CustomLog "/www/b/html/access_log" combined
</VirtualHost>

<VirtualHost 10.1.0.248:80>
    ServerName www.c.com
    DocumentRoot "/www/c/html/"
    <Directory "/www/c/html/">
        Options none
        AllowOverride none
        require all granted
    </Directory>
    <Directory "/www/c/html/admin">
        Options indexes
        AllowOverride none
        AuthType basic
        AuthName "admin and passwd"
        AuthUserFile "/etc/httpd/conf.d/.htpasswd"
        require valid-user
    </Directory>
    Alias /bbs/ "/"
    <Directory "/">
        Options Indexes FollowSymLinks
        AllowOverride none
        require all granted
    </Directory>
    ErrorLog "/www/c/html/error_log"
    LogLevel warn
    CustomLog "/www/c/html/access_log" combined
</VirtualHost>

练习2:

    使用脚本实现以上功能

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

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

[root@CentOS7 ~]# cat httpd.sh 
#!/bin/bash
#description : create virtualhost 
#version 0.1
#author gm
#date 20161007
#

create_vir (){
cat > /etc/httpd/conf.d/${domain_name}.conf << END
<VirtualHost *:80>
    Servername ${domain_name}
    DocumentRoot "${vir_path}"
    <Directory "${vir_path}">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
    ErrorLog "${vir_path}/error.log"
    LogLevel warn
    CustomLog "${vir_path}/access.log" combined
</VirtualHost>
END
}

create_status () {
sed -i '/<\/Directory>/a \\t<Location "/status">\n\t\tSetHandler server-status\n\t\t\ <末尾的\表示换行,请删除>
Require all granted\n\t</Location>' /etc/httpd/conf.d/${domain_name}.conf
}

create_authdir () {
#read -p "Input dir of Authrization: " dir
mkdir ${vir_path}/admin
touch /etc/httpd/conf.d/.${domain_name}
read -p "Input One UserName: " username
while [ $username != exit ]; do
    htpasswd -b -m /etc/httpd/conf.d/.${domain_name} $username 123456
    read -p "Input One UserName Or Input exit: " username
done
echo "admin test" > ${vir_path}/admin/index.html
sed -i '/<\/Directory>/a \\t<Location "/admin">\n\t\tOptions none\n\t\tAllowOverride none\  <末尾的\表示换行,请删除>
\n\t\tAuthType basic\n\t\tAuthName "admin and passwd"\n\t\tAuthUserFile /etc/httpd/conf.d\  <末尾的\表示换行,请删除>
/.www.gm.com\n\t\tRequire valid-user\n\t</Location>' /etc/httpd/conf.d/${domain_name}.conf
}

create_aliasdir () {
#read -p "Input dir of Alias: " dir1
#read -p "Input dir of xxxxx: " dir2
mkdir ${vir_path}/bbs
sed -i '/<\/Directory>/a \\tAlias /bbs/ "/"\n\t<Directory "/">\n\t\tOptions indexes\n\t\t\
AllowOverride none\n\t\tRequire all granted\n\t</Directory>' /etc/httpd/conf.d/${domain_name}.conf
}

#create configure file
read -p "Input one domain name: " domain_name
if [ -e /etc/httpd/conf.d/${domain_name}.conf ] ; then
    echo "This virtualhost is created."
    exit 1
else
    touch /etc/httpd/conf.d/${domain_name}.conf
fi

#change DocumentRoot
sed -i 's@^DocumentRoot\>.*@#&@' /etc/httpd/conf/httpd.conf && echo "OK"

#create web chroot dir
read -p "Input one path of virtualhost: " vir_path
mkdir -p ${vir_path}
echo "<h1>${domain_name}</h1>" >> ${vir_path}/index.html

#create virtualhost configure
create_vir ${domain_name} ${vir_path}
echo -e "\033[35mcreate virtualhost seccuseful\033[0m"

#create statuse dir
read -p "you need status dir ? Input yes|no : " ans1
if [ $ans1 == yes ] ; then
    create_status
    echo -e "\033[35mcreate staus dir seccuseful\033[0m"
fi

#create auth dir
read -p "you need authrization dir ? Input yes|no : " ans2
if [ $ans2 == yes ] ; then
    create_authdir ${domain_name} ${vir_path}
    echo -e "\033[35mcreate authrization dir seccuseful\033[0m"
fi

#create alias dir
read -p "you need alias dir ? Input yes|no : " ans3
if [ $ans3 == yes ] ; then
    create_aliasdir ${domain_name} ${vir_path}
    echo -e "\033[35mcreate alias dir seccuseful\033[0m"
fi


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

(0)
megedugaomegedugao
上一篇 2016-10-09
下一篇 2016-10-09

相关推荐

  • 计算机组成及Linux初识

    拼一载春秋,搏一生无悔 1. 计算机简介 2. Linux发行版简介 3. Linux哲学思想简介 4. Linux系统上获取命令帮助 5. Linux「12」个基础命令简介 6. Linux发行版基础目录及功能简介 1.计算机简介 电子计算机(英语:computer),亦称电脑,是一种利用「电子学…

    Linux干货 2016-10-27
  • 管窥Linux史

    管窥Linux史 众所周知,绝大部分发行版本都被称为类Unix系统,要说Linux就应该先了解Unix的历史,Unix的起源应该赘述MULTICS的历史,Unix的父辈是颇具开拓性的Multics项目…… Unix创世纪 二战结束以后,冷战开始了。1957年苏联发射了第一颗人造卫星,进而开始筹备发射载人宇宙飞船。与此同时,美国宇航局的研究却连连受挫。航天领域…

    Linux干货 2016-10-14
  • Linux Bash Shell有关变量

    bash 创建一个子bash-n 查看是否有错误-x 逐行显示信息$:显示当前PIDPPID :显示父进程PIDSHLVL :显示当前shell是几级shellpstree 显示进程树-p 同时显示进程号ps -ef 显示进程信息 本地变量 :仅对当前SHELL有效,对子SHELL也无效环境变量:对当前SHELL及子SHELL均有效。 set 显示本地变量和…

    2017-09-16
  • Linux之任务计划 crontab

    Linux之任务计划    用过windows的同学应该都知道在控制面板中有一个“任务计划”选项,我们可以通过向导设置让计算机在某个时间点或者开机时运行某个脚本或者批处理等等,方便我们的日常管理监视工作;同样在Linux系统中,如果我们想在开机时就运行某个操作,可以直接将命令写入/etc/rc.local中,该操作就会在机器开机时运行;如…

    Linux干货 2015-05-18
  • bash-条件测试

                     条件测试 1.条件测试           判断某需求是否满足,…

    Linux干货 2016-08-12
  • 初学Linux之程序进程管理工具汇总

    pstree, ps, pidof, pgrep, top, htop, glances, pmap, vmstat, dstat, kill, pkill, job, bg, fg, nohup 

    2017-12-16