脚本实现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 15:22
下一篇 2016-10-08 17:03

相关推荐

  • Linux 第11&12天: Linux软件包管理

    Linux 第11&12天: Linux软件包管理      在系统管理中, 软件包的管理是最重要的, 是系统管理的基础,   本文主要是记录内容有: RPM二进制包的管理,YUM二进制包的管理, 源代码包的管理   软件运行环境API:Application Programming Interfac…

    Linux干货 2016-08-22
  • linux运维

    linux运维大纲,学习路线图

    Linux干货 2017-10-21
  • 推荐-LINUX磁盘管理

    Evernote Export LINUX磁盘管理 linux学习笔记 磁盘分区 文件系统 挂载 在操作系统中,磁盘管理属于设备管理的范畴。一块硬盘安装到主机之后称为裸设备,若要能够在Linux系统中使用必须对其进行如下步骤:分区=>格式化(创建文件系统)=>挂载 LINUX系统的设备类型 Linux沿袭Unix的风格,将所有设备认成是一个文件 …

    Linux干货 2016-04-05
  • LVM

       本节主要为raid和LVM相关 一、作业 1、创建一个2G的文件系统,块大小为2048byte,预留1%可用空间,文件系统ext4,卷标为TEST,要求此分区开机后自动挂载至/test目录,且默认有acl挂载选项 2、写一个脚本,完成如下功能: (1) 列出当前系统识别到的所有磁盘设备 (2) 如磁盘数量为1,则显示其空间使用信息 …

    Linux干货 2016-08-30
  • linux系统的路由功能

    1、概要    大家应该都明白,不同网段的报文包传播,是需要路由的转发转发功能的,那么,一个linux操作系统能不能充当路由器呢?下面不妨来验证一下。 2、网络拓扑 3、实现过程    <1>俩个centos6分别充当Router1和Router2。并添加足够的网卡。    <2&…

    Linux干货 2016-09-06
  • CentOS 5,6 系统启动流程详解

        一、linux 组成介绍          1.linux 组成:              Lin…

    Linux干货 2016-09-11