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

相关推荐

  • 15 权限管理及作业

    15 权限管理及作业(作业单独一篇) 一、杂项知识整理 1、访问控制列表:ACL:Access Control List,实现灵活的权限管理。     除了文件的所有者,所属组和其它人,可以对更多的用户设置权限。     centos7.0之后默认创建的ext4文件系统有ACL功…

    Linux干货 2016-08-04
  • 定时任务的完成contab

    采用crontab来完成 利用crontab来定时执行任务大致有如下三步: 1、编写shell脚本 2、利用crontab加入到定时任务队列 3、查看作业完成情况 一、如何建立shell脚本 Linux下有很多不同的shell,但我们通常使用bash(bourne again shell)进行编程,因为bash是免费的并且很容易使用 程序必须以下面的行开始(…

    Linux干货 2016-08-11
  • tar ,cpio打包解压. shell脚本for,while,until循环. rpm包管理,循环的特殊用法. select循环菜单,函数function,rpm包查询,yum

    tar  tar -cf 路径+文件名字后续.tar  +路径 创建归档压缩 tar cf /testdir/etc.tar /etc/ tar cvf 可以查看解压过程 tar tvf 预览作用 tar xvf 解压文件  tar zcvf /testdir/etc2.tar.gz /etc/ 解压再压缩到指定路径,文件格式 …

    Linux干货 2016-08-21
  • 终端的类型

    Linux下的终端是一个连接系统的接口,它有以下几个分类     tty:虚拟终端       tty是Teletype的缩写。Teletype是最早出现的一种终端设备,很象电传打字机(或者说就是),是由Teletype公司生产的。   &nbsp…

    Linux干货 2016-10-20
  • SElinux配置httpd

    一、启用SELinux策略并安装httpd服务,改变网站的默认主目录为/website,添加SELinux文件标签规则,使网站可访问     1、修改selinux策略并重启 [root@localhost ~]# vim /etc/selinux/config# This file controls the stat…

    Linux干货 2016-09-19
  • 进程管理

    进程管理 内核的功用:进程管理、文件系统、网络功能、内存管理、驱动程序、安全功能 用户模式(空间),内核模式(空间) Process(进程):运行中的程序的一个副本         存在生命周期 task struct:内核的结构体 Linux内内核存储进程信息的固定格式:tas…

    Linux干货 2016-09-10