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

相关推荐

  • Python第一周小结

    经历了两个星期Linux运维基础的铺垫,这周我们正式开始了python的学习。经过第一周的各种挣扎以及反复训练,终于有所收获了一点东西。现在将第一周中学到的一个非常重要的算法技巧总结如下: 即:折半思想 例:给定一个不超过五位数的正整数,判断该数有几位数 Code1:                                              …

    Linux干货 2018-03-26
  • 早安,Linux

    希望能通过不断的努力,变成一个不一样的我。

    Linux干货 2017-07-11
  • scp和rsync的使用

    通过一些简单需求了解scp和rsync的使用

    2017-09-18
  • SELinux介绍

    SELinux介绍 SELinux: Secure Enhanced Linux, 是美国国家安全局(NSA=The National Security Agency)和SCC(Secure Computing Corporation)开发的 Linux的一个强制访问控制的安全模块。 2000年以GNU GPL发布, Linux内核2.6版本后集成在内核中D…

    Linux干货 2016-10-08
  • DNS和BIND配置(第一部分)

    一、知识整理 1、最初只有七个一级域名:Top Level Domain:tld:com、edu、mil、gov、net、org、int;     一级域名分三类:组织域、国家域、反向域。最多127级域名;全球有13个根节点服务器。 2、一次完整的查询请求经过的流程:client—hosts文件&#8212…

    Linux干货 2016-10-17
  • 初识MySQL(二)SQL语句

        MySQL是关系型数据库的一种,基于二维表实现数据的存储与读取,通过索引实现快速查询,而实现数据库、表、索引的操作则是由SQL语句来完成的。     1、MySQL中字符大小写       (1)、SQL关键字以及函数名不…

    Linux干货 2015-08-26