概述
本文使用腳本實現基于主機名的虛擬主機按需創建:
-
腳本可接受參數,提供獨立站點目錄;
-
生成獨立站點首頁;
-
腳本可接受參數,參數虛擬主機名稱;
-
每虛擬使用單獨的配置文件;
-
腳本可接受參數,參數虛擬主機名稱;
環境
系統基于CentOS7.2,并通過yum安裝httpd 2.4.6
建議關閉防火墻和selinux。
演示
客戶機將域名解析寫入/etc/hosts文件中
腳本
#!/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.www58058.com/50153