脚本实现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

相关推荐

  • n28-第四周

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限 答:install 复制文件并设置属性 -d, –directory:复制所有参数为目录名 -m, –mode=MODE:设置文件属性,相当于chmod # install -d -m u=rwx /e…

    Linux干货 2017-12-24
  • 初入LINUX之首周总结及虚拟机的安装

    一.初入LINUX   1.在接触LINUX之前,先了解了计算机的系统组成: 2.服务器是计算机的一种,是网络中为客户端计算机提供各种服务的高性能的计算机,服务器在网络操作系统的控制下,将与其相连的硬盘磁带,打印机及昂贵的专用通讯设备提供给网络上的客户站点共享,也能为网络用户提供集中计算、信息发布及数据管理等服务 。 3.服务器的三大操作系统:①w…

    2017-05-20
  • 计算机构成与Linux基础知识之一

    简述计算机构成,Linux如何诞生及主流发行版;简单命令介绍;如何获取帮助及Linux目录命名及功用简介绍。

    2018-02-26
  • Linux文件查找及压缩

    Linux文件查找(locate & find) locate     查询系统上预建的文件索引数据库(速度快,但更新不实时)     /var/lib/mlocate/mlocate.db     依赖于事先构建的索引 &nbsp…

    Linux干货 2016-08-19
  • Shell编程基础

    1.编程的基本概念 程序:指令+数据   程序编程风格:   过程式:以指令为中心,数据服务于指令。   对象式:以数据为中心,指令服务于数据。    shell程序:提供了编程能力,解释执行。    计算机:只识别二进制指令;    编程语言: &…

    Linux干货 2016-08-15
  • N25期—第四周作业

    1、 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 cp –rf /etc/skel /home/tuser1 chmod -R go= /home/tuser1 2、 编辑/etc/group文件,添加组hado…

    Linux干货 2016-12-26