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

相关推荐

  • swap与dd命令使用详解

    处理交换文件和分区     交换分区是系统RAM 的补充 基本设置包括:     创建交换分区或者文件     使用mkswap 写入特殊签名     在/etc/fstab 文件中添加适当的条目 &…

    Linux干货 2017-04-30
  • Centos6.5利用RubyGems的fpm制作zabbix_agent的rpm包,并使用ansible批量部署

    一、 搭建rpm包制作环境 安装gcc [root@lvs1 ~]# yum install gcc 安装make [root@lvs1 ~]# yum install make 安装ruby源(ruby版本必须要在1.9.3以上,centos自带的是1.8的版本,需要自己编译安…

    Linux干货 2016-08-20
  • 日志收集工具EFK之fluent部署手稿

    Edit EFK fluent部署安装 1. 环境介绍 Centos 6.5 64bit 2. 安装 td-agent介绍 fluent 考虑到灵活可扩展性,使用Ruby编写,部分功能考虑性能使用C语言编写。普通用户安装操作Ruby daemon还是有一定难度的。考虑到flunt的上手难度, fluent专门发布了稳定发布包,就也是所谓的td-agent. …

    Linux干货 2016-03-24
  • RPM的使用

    概述 RPM 是 Red Hat PackageManager 的缩写,本意是Red Hat 软件包管理,顾名思义是Red Hat 贡献出来的软件包管理;在Fedora 、Redhat、Mandriva、SuSE、YellowDog等主流发行版本,以及在这些版本基础上二次开发出来的发行版采用; RPM包里面都包含什么?里面包含可执行的二进制程序,这个程序和W…

    Linux干货 2016-02-14
  • 从2个命令简单聊聊CentOS账户锁定原理

    linux中 passwd -l 和usermod -L有什么区别,各自的解锁和锁定原理是什么样的呢?

    2017-11-16
  • linux的管道命令和用户管理命令

    1、列出当前系统上的所有已经登录的用户的用户名 [root@localhost home]# who | cut -d” ” -f1 | uniq root test root 2、取出最后登录到当前系统的用户相关信息 [root@localhost home]# who | tail -1 root pts/3 2017-07-16 19:38 (192.…

    Linux干货 2017-07-17