脚本实现httpd创建虚拟主机

概述


本文使用脚本实现基于主机名的虚拟主机按需创建:

  • 脚本可接受参数,提供独立站点目录;

  • 生成独立站点首页;

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

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

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

环境


系统基于CentOS7.2,并通过yum安装httpd 2.4.6

建议关闭防火墙和selinux。

演示


 1475915355333086.gif

客户机将域名解析写入/etc/hosts文件中

GIF3.gif

脚本


#!/usr/bin/env bash
#
# Author: jacky18676887374@aliyun.com   QQ-18676887374
# date: 20161007-14:05:25
# Vervion: 0.0.1
# Description:
# Synopsis:
# 
# 快速创建虚拟网站,站点测试页。
# create a test web
webtest(){
cat <<EOF > $2/index.html
<html>
<head>It's only a test web</head>
<body><h1>$1</h1></body>
</html>
EOF
}
# 生成vhost配置文件
vhost() {
    cat <<EOF > /etc/httpd/conf.d/$1.conf
<VirtualHost *:80>
    ServerName $1
    DocumentRoot "$2"
    <Directory "$2">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
    ErrorLog "/var/log/httpd/error_log_$1"
    LogLevel warn
    CustomLog "/var/log/httpd/access_log_$1" combinedio
</VirtualHost>
EOF
}
# get website variable
getvar(){
    read -p 'Input a Directory for this VirtualHost: ' vdiretory
    read -p 'Input a hostname for this VirtualHost: ' vhostname
}
# 禁用selinux
if [ `getenforce` != Disabled ];then
    sed -i '/^SELINUX=/s/^.*$/SELINUX=disabled/' /etc/selinux/config
    echo "change selinux=disabled,you must reboot."
fi
PS3='Input your choice:1)create; 2)quit  '
select i in create quit ;do
    case $i in
        create)
            getvar
            mkdir -p $vdiretory
            vhost $vhostname $vdiretory
            webtest  $vhostname $vdiretory
            if `httpd -t` ;then
                systemctl reload httpd
                echo -e "Create complete.\nyour  website url:$vhostname"
            else
                mv $vdiretory/index.html{,.errorr}
                mv /etc/httpd/conf.d/vhostname.conf{,.error}
                echo 'Error'
            fi
            ;;
        quit)
            exit
            ;;
    esac
done

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

(0)
昭其昭其
上一篇 2016-10-08
下一篇 2016-10-08

相关推荐

  • shell脚本1

    shell脚本基础 shell脚本: 包含一些命令或声明,并符合一定格式的文本文件 格式要求:首行shebang机制 #!/bin/bash #!/usr/bin/python #!/usr/bin/perl shell脚本的用途有: 自动化常用命令 执行系统管理和故障排除 创建简单的应用程序 处理文本或文件 创建shell脚本 第一步:使用文本编辑器来创建…

    2017-08-05
  • 难搞的grep、find练习题

    马哥教育网络班21期-第五周博客作业 1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@caicai ~]# grep –color "^[[:space:]]\+" /boot/grub/grub.conf    …

    Linux干货 2016-07-26
  • shell编程

       编程基础   编程基础:       编程:也就是程序+数据           程序编程风格:                  过程式:以指…

    Linux干货 2016-08-15
  • 磁盘配额

    磁盘配额 1 启用磁盘配额 首先创建新的分区 /dev/sd5,并创建文件系统。 [root@local ~]# mkfs.ext4 /dev/sda5 由于xfs 不磁盘配额能成功,这里使用ext4. 然后把/dev/sda5分区挂载到 /home [root@local ~]# mount /dev/sda5 /home [root@local ~]# …

    Linux干货 2017-05-02
  • n28 第二周作业

    n28 第二周作业

    Linux干货 2017-12-09
  • 博客启动计划&我个人理解的Python优缺点

    很久没有写博客了,因为最近一直在使用Python。Python实在不是一门好的工作用语言(我的观点是所有的动态语言都不是好的工作用语言,不仅是Python),但是自己玩还是可以的。但,生活所迫,还是要继续用的呀(笑)。 所以呢,博客还是要写的,java还是最喜欢的,Python也是要学习的,当然其实更多是总结了。既然博客要启动了,这篇文字就权当测试吧,使用公…

    Linux干货 2015-03-13