1. 程式人生 > >server-install-shell

server-install-shell

done star rtu status domain rest errorlog httpd res

apache-vhost:自動完成安裝,基本配置

通過提示輸入域名、網站根目錄、等信息完成虛擬主機的添加工作

service httpd stop &> /dev/null


conf="/usr/local/apache/conf/httpd.conf"

vhostconf="/usr/local/apache/conf/extra/httpd-vhosts.conf"

bin="/usr/local/apache/bin/apachectl"

rootdir=/www

mkdir -p $rootdir



grep "vhost" $conf | grep "#" &> /dev/null

if [ $? -eq 0 ]; then

vhost=$(grep "vhost" $conf | sed "s/#//")

sed -i '/vhost/ d' $conf &> /dev/null

echo "$vhost" >> $conf


echo "NameVirtualHost *:80" > $vhostconf


echo "<Directory $rootdir>

order allow,deny

allow from all

</Directory>">> $vhostconf

fi


while true

do

read -p "hostname of FDQN:" fdqn

read -p "directory of website:" sitedir

mkdir -p $rootdir/$sitedir/html

mkdir -p $rootdir/$sitedir/logs


domain=$(echo $fdqn | awk -F. '{print $2"."$3}')

echo "<VirtualHost *:80>

ServerAdmin admin@$domain

DocumentRoot $rootdir/$sitedir/html

ServerName $fdqn

ErrorLog $rootdir/$sitedir/logs/error_log

CustomLog $rootdir/$sitedir/logs/access_log common

</VirtualHost>" >> $vhostconf


read -p "continue to do?(yes/no)" yn

if [ $yn = no ];then

$bin start &> /dev/null

exit 0

fi

done

DHCP:自動完成安裝,基本配置

:通過提示輸入網段、子網掩碼、網關、DNS、IP起止地址等信息完成作用域的添加

#!/bin/bash

conf="/etc/dhcpd.conf"

rpm -q dhcp &> /dev/null

if [ $? -ne 0 ];then

yum -y install dhcp

fi



grep "subnet" $conf &> /dev/null

if [ $? -ne 0 ]; then

echo "

ddns-update-style interim;

ignore client-updates;

default-lease-time 21600;

max-lease-time 43200;

" > $conf

fi


read -p "please input network:" network

read -p "please input netmask:" netmask

read -p "please input gateway:" gateway

read -p "please input DNS:" dns

read -p "please input start_ip:" start_ip

read -p "please input end_ip:" end_ip


echo "

subnet $network netmask $netmask {

option routers $gateway;

option subnet-mask $netmask;

option domain-name-servers $dns;

range dynamic-bootp $start_ip $end_ip;

}


" >> $conf

service dhcpd restart


DNS:完成DNS基本配置

根據提示輸入域名、區域文件名、記錄類型、主機頭、IP地址等信息完成添加域及記錄的工作

conf=/var/named/chroot/etc/named.conf

datadir=/var/named/chroot/var/named



rpm -q bind &> /dev/null

if [ $? -ne 0 ]; then

yum -y install bind bind-chroot caching-nameserver

fi


if [ ! -f $conf ];then

echo "options {

directory \"/var/named\";

};" > $conf

fi


read -p "please input domain_name:" domain

grep $domain $conf &> /dev/null

if [ $? -ne 0 ]; then

echo "zone \"$domain\" in {

type master;

file \"$domain\";

};" >> $conf

echo "\$ttl 86400

@ in soa ${domain}. root.${domain}. (

2013010101

3h

15m

1w

1d

)"> $datadir/$domain

while true

do

read -p "Type of recond(ns/a/cname/mx/ptr):" type

case $type in

ns)

echo $domain | grep "in-addr.arpa" &> /dev/null


if [ $? -eq 0 ]; then

read -p "hostname of FDQN:" fdqn

echo "@ in ns $fdqn.">> $datadir/$domain


else

read -p "head of hostname:" head

echo "@ in ns $head">> $datadir/$domain

fi

;;

a)

read -p "head of hostname:" head

read -p "ip address:" ip

echo "$head in a $ip">> $datadir/$domain


;;

ptr)

read -p "ip address:" ip

read -p "hostname of FQDN:" fqdn

host=$(echo $ip | awk -F. '{print $4}')

echo "$host in ptr $fqdn.">> $datadir/$domain

;;

mx)

read -p "priority of recond:" priority

read -p "head of hostname:" head

read -p "ip address:" ip

echo "@ in mx $priority $head">>$datadir/$domain

echo "$head in a $ip">> $datadir/$domain

;;

cname)

read -p "please input aliase:" aliase

read -p "head of hostname:" head

echo "$aliase in cname $head">> $datadir/$domain

;;

esac

read -p "continue to do (yes/no)?" yn

if [ $yn = no ]; then

exit 0

fi

done

fi



vsftpd:關閉匿名用戶登錄,支持本地用戶登錄

通過交互輸入用戶名、部門名實現目錄及權限設置

目錄結構如下:

/data/public (公司公共目錄,所有員工可讀、可寫,但不可刪除其他人的文件)

/data/部門目錄 (部門員工可讀,部門管理員可寫,其他人不可訪問)

/data/部門目錄/用戶目錄


註意:在實例腳本中使用了continue/break/exit,主要目的讓大家了解三者之間的區別

conf=/etc/vsftpd/vsftpd.conf

rpm -q vsftpd &> /dev/null

if [ $? -ne 0 ]; then

yum -y install vsftpd

sed -i 's/anonymous_enable=YES/anonymous_enable=NO/' $conf

echo "local_root=/data" >> $conf


mkdir -p /data/public

chmod 1777 /data/public

fi

while true

do

read -p "please input username:" username

read -p "please input bumen:" bumen

read -p "Are you sure?(yes/no)" yn

if [ $yn = no ]; then

continue

fi

if [ ! -d /data/$bumen ]; then

mkdir -p /data/$bumen

groupadd $bumen

useradd -g $bumen -d /data/$bumen/${bumen}-adm ${bumen}-adm

echo "${bumen}-adm:123,qwe." | chpasswd

chage -d 0 ${bumen}-adm

chown ${bumen}-adm:$bumen /data/$bumen

chmod o-rx /data/$bumen

fi

grep "$username" /etc/passwd &> /dev/null

if [ $? -ne 0 ]; then

useradd -g $bumen -d /data/$bumen/$username $username

echo "$username:123456" | chpasswd

chage -d 0 $username

else

echo "$username is exist."


fi

read -p "continue to yes/no?" yn

if [ $yn = no ]; then

break

fi

done

service vsftpd status &> /dev/null


if [ $? -eq 0 ]; then

exit 0

else

service vsftpd start

fi



server-install-shell