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

相关推荐

  • 计算机的组成和Linux发展史

    计算机的组成及功能   计算机是由CPU,内存,输入装置和输出装置四大部件组成计算机,每一部件分别按要求执行特定的基本功能。  CPU: 控制器和运算器合称中央处理器,也就是CPU,它的功能主要是解释计算机指令以及处理计算机软件中的数据。  内存: 它是与CPU进行沟通的桥梁。计算机中所有程序的运行都是在内存中进行的,内存(Me…

    Linux干货 2016-10-30
  • 马哥教育网络班20期+第一周课程练习

    一、计算机的组成及其功能。 计算机是由几个单元所组成,输入单元,输出单元,运算器,控制器,存储器,5大单元组成  1、运算器 又称运算器又称算术逻辑单元,它是计算机对数据进行加工处理的部件,包括算术运算(加、减、乘、除等)和逻辑运算(与、或、非、异或、比较等)。 2、控制器 负责从存储器中取出指令,并对指令进行译码;根据指令的要求,按时间的先后顺序…

    Linux干货 2016-06-23
  • Linux第一周心得

          第一次接触Linux,内心还是有点紧张,怕自己学不会、怕太难。不过,还是想挑战一下自己,所以来到了马哥学习Linux。      过来的第一天有点坎坷,不过还好有小琰姐的陪伴,一直和我们东西奔走,总算是解决了一大堆的麻烦,所以,感激……表白小琰姐!哈哈哈  撒花花  …

    Linux干货 2017-07-15
  • N25 – week 3 blog

    本周的blog开始使用了代码语言格式,我原本以为自然的才是最好的,orginal的才是最美的,但是我错了。。。我发现同学们都在各种markdown,各种排版。我说过我早已过了care这些的年纪,但是我不能脱离群体单独存在,所以我底下了傲娇的头。 本周开始blog标题改为英文,逼格满满,麦满分~ 下面开始第$wk_num周的作业 [root@dhcp-10-1…

    Linux干货 2016-12-19
  • Linux系统时间简单修改方法

          Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟。系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟,这个硬件时钟可以在BIOS中进行设置。Linux中的所有命令(包括函数)都是采用的系统时钟设置。Linux操作系统运行…

    Linux干货 2015-06-12
  • N28-第四周博客作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。

    2、编辑/etc/group文件,添加组hadoop。

    3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。

    4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。

    5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。

    6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;

    7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;

    8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;

    9、找出/etc/passwd文件中的一位数或两位数;

    10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;

    11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

    12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;

    13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;

    Linux干货 2017-12-26