腳本練習,寫一個腳本,實現本地Web服務器的配置:
1、檢查httpd包是否已經安裝,如果沒有,則使用yum命令安裝之;(需要事先配置好yum源)
2、取消中心主機,新建5個基於域名的虛擬主機,並分別提供一個測試主頁,主頁內容為其對應的FQDN:
www.magedu.com, /vhosts/magedu
bbs.magedu.com, /vhosts/bbs
wp.magedu.com, /vhosts/wordpress
pw.magedu.com, /vhosts/pw
pma.magedu.com, /vhosts/phpmyadmin
3、檢查修改後的配置文件語法,如果沒有問題,則啟動httpd服務,並要求以後服務會開機自動啟動;
#!/bin/bash
#Author honway
#ScriptName AutoSetWebServer
SetYUM() {
echo "=============================================="
echo "Start backup files."
echo "backup your .repo file to .repo.bak!"
echo "=============================================="
YUM_CONF_DIR=/etc/yum.repos.d/
for Y in $YUM_CONF_DIR/*.repo; do
mv $Y $Y.bak &>/dev/null
done
echo "=============================================="
echo "Backup successfully."
echo "=============================================="
echo "Create a yum config file."
cat >> $YUM_CONF_DIR/yumconf.repo <<EOF
[Server]
name=Server
baseurl=ftp://172.16.0.1/pub/Server
gpgcheck=0
enabled=1
[VT]
name=VT
baseurl=ftp://172.16.0.1/pub/VT
gpgcheck=0
enabled=1
[Cluster]
name=Cluster
baseurl=ftp://172.16.0.1/pub/Cluster
gpgeheck=0
[ClusterStorage]
name=ClusterStorage
baseurl=ftp://172.16.0.1/pub/ClusterStorage
gpgcheck=0
enabled=1
[errata]
name=errata
baseurl=ftp://172.16.0.1/pub/errata
gpgeheck=0
enabled=0
EOF
echo "OK!"
echo "=============================================="
}
CreateTestPage(){
echo "$u" > $URL_DIR/index.html
}
SetYUM
if ! `rpm -qa | grep httpd &>/dev/null`; then
echo "===============================>"
echo "Start install httpd.please wait..."
yum -y install httpd &>null
else
echo "install is not sucess,something error."
fi
SetVirtualConfig(){
sed -i 's@(^DocumentRoot[[:space:]]{1,}"/var/www/html")@#1@g' /etc/httpd/conf/httpd.conf
sed -i 's@^#(NameVirtualHost[[:space:]]{1,}[^[:space:]]{1,}:80$)@1@g' /etc/httpd/conf/httpd.conf
}
SetVirtualConfig
mkdir -p /vhosts/{magedu,bbs,wp,pw,pma} &>/dev/null
FQDN=magedu.com
f=/etc/httpd/conf/httpd.conf
for u in {www.$FQDN,bbs.$FQDN,wp.$FQDN,pw.$FQDN,pma.$FQDN}; do
case "$u" in
"www.magedu.com")
URL_DIR=/vhosts/magedu
cat >>$f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
"bbs.magedu.com")
URL_DIR=/vhosts/bbs
cat >> $f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
"wp.magedu.com")
URL_DIR=/vhosts/wp
cat >>$f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
"pw.magedu.com")
URL_DIR=/vhosts/pw
cat >>$f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
"pma.magedu.com")
URL_DIR=/vhosts/pma
cat >>$f <<EOF
<VirtualHost *:80>
ServerName $u
DocumentRoot $URL_DIR
</VirtualHost>
EOF
CreateTestPage ;;
*)
echo "Error..." ;;
esac
done
if `service httpd configtest`; then
chkconfig --level 345 httpd on
service httpd start
fi
本文出自 “吖吖個呸” 博客,請務必保留此出處http://gm100861.blog.51cto.com/1930562/803162