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 16:16
下一篇 2016-10-09 18:54

相关推荐

  • bash进阶学习ing

    使用read命令来接受输入使用read来把输入值分配给一个或多个shell变量: -p 指定要显示的提示 -t TIMEOUT read 从标准输入中读取值,给每个单词分配一个变量所有剩余单词都被分配给最后一个变量read -p “Enter a filename: “ FILE 条件选择if语句选择执行:注意: if语句可嵌套单分支if 判断条件: the…

    Linux干货 2016-08-21
  • 10个有用的Linux命令面试问题及答案

    1. 如何暂停一个正在运行的进程,把其放在后台(不运行)? 答案:为了停止正在运行的进程,让其在后台运行,我们可以使用组合键 Ctrl+Z。 2. 什么是安装Linux所需的最小分区数量,以及如何查看系统启动信息? 答案:单独一个/root分区足以执行所有的系统任务,但是强烈建议安装Linux时,需要至少三个分区:/root,/boot,/swap。一个ID…

    2017-09-05
  • 【N25第一周作业】Linux基础

    题目:   1、描述计算机的组成及其功能。   2、按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别。   3、描述Linux的哲学思想,并按照自己的理解对其进行解释性描述。   4、说明Linux系统上命令的使用格式;详细介绍ifconfig、echo、tty、startx、export、pwd、his…

    Linux干货 2016-12-03
  • LVM逻辑卷管理相关命令的用法

    前提:创建分区,文件或添加硬盘作为LVM的物理卷 pv(物理卷) 命令用法 1、pvcreate创建pv pvcreate -s    指定PE大小,默认为4M 2、pvremove删除pv 3、pvs,pvdisplay显示pv pvs 显示pv摘要 pvdisplay 详细显示pv 4、pvmove:把卷组中一个物…

    Linux干货 2016-08-30
  • shell脚本编程基础

    一.编程基础              程序:指令+数据              程序编程风格:  &n…

    Linux干货 2016-08-12
  • 端口转发实例

    什么是端口转发?      端口转发(Port forwarding),有时被叫做隧道,是安全壳(SSH) 为网络安全通信使用的一种方法。端口转发是转发一个网络端口从一个网络节点到另一个网络节点的行为,其使一个外部用户从外部经过一个被激活的NAT路由器到达一个在私有内部IP地址(局域网内部)上的一个端口。 为什么需要端口转发? …

    2017-04-13