编写脚本自动部署反向代理、web、nfs

时间:2022-08-29 21:35:34

服务器端

#!/bin/bash

function nginx_install(){
	if [[ -f /usr/sbin/nginx ]]; then
		echo 'Nginx has been installed.'
		exit
	else
		flag1=3
		while [[ $flag1 -gt 0 ]]; do
			yum install epel-release -y && yum install nginx -y
			if [[ $? -ne 0 ]]; then
				((flag1--))
			else
				echo 'Nginx has been installed.'
				exit
			fi
		done
		echo 'Nginx install failed.'
	fi
	systemctl start nginx
}

function nginx_balancer(){
	msg1='upstream myapp1 { server 192.168.60.129; server 192.168.60.130; server 192.168.60.131; }'
	msg2='proxy_pass http://myapp1;'
	sed -ri "/^http/a $msg1" /etc/nginx/nginx.conf
	sed -ri "/^ *location \/ \{$/a $msg2" /etc/nginx/nginx.conf
	systemctl reload nginx
}

function nfs_install(){
	rpm -qa |grep rpcbind >> /dev/null
	if [[ $? -eq 0 ]]; then
		echo 'RPCbind has been installed'
	else
		flag2=3
		while [[ $flag2 -gt 0 ]]; do
			yum install rpcbind -y
			if [[ $? -ne 0 ]]; then
				((flag2--))
			else
				echo 'RPCbind has been installed.'
				exit
			fi
		done
		echo 'RPCbind install failed.'
	fi
	rpm -qa |grep nfs-utils >> /dev/null
	if [[ $? -eq 0 ]]; then
		echo 'nfs-utils has been installed'
	else
		flag3=3
		while [[ $flag3 -gt 0 ]]; do
			yum install nfs-utils -y
			if [[ $? -ne 0 ]]; then
				((flag3--))
			else
				echo 'nfs-utils has been installed.'
				exit
			fi
		done
		echo 'nfs-utils install failed.'
	fi
}

function nfs_server(){
	mkdir /share
	touch /share/index.html
	echo '---NFS---Hello---' > /share/index.html
	chmod -R o+w /share
	echo '/share 192.168.60.0/24(rw,sync,fsid=0)' >> /etc/exports
	systemctl start rpcbind.service && systemctl start nfs-server.service
	if [[ $? -eq 0 ]]; then
		echo 'NFS server running.'
	fi
	systemctl enable rpcbind.service && systemctl enable nfs-server.service
}

nginx_install
nginx_balancer
nfs_install
nfs_server

  

客户端

#!/bin/bash

function nginx_install(){
	if [[ -f /usr/sbin/nginx ]]; then
		echo 'Nginx has been installed.'
		exit
	else
		flag1=3
		while [[ $flag1 -gt 0 ]]; do
			yum install epel-release -y && yum install nginx -y
			if [[ $? -ne 0 ]]; then
				((flag1--))
			else
				echo 'Nginx has been installed.'
				exit
			fi
		done
		echo 'Nginx install failed.'
	fi
	systemctl start nginx
}

function nfs_install(){
	rpm -qa |grep rpcbind >> /dev/null
	if [[ $? -eq 0 ]]; then
		echo 'RPCbind has been installed'
	else
		flag2=3
		while [[ $flag2 -gt 0 ]]; do
			yum install rpcbind -y
			if [[ $? -ne 0 ]]; then
				((flag2--))
			else
				echo 'RPCbind has been installed.'
				exit
			fi
		done
		echo 'RPCbind install failed.'
	fi
	rpm -qa |grep nfs-utils >> /dev/null
	if [[ $? -eq 0 ]]; then
		echo 'nfs-utils has been installed'
	else
		flag3=3
		while [[ $flag3 -gt 0 ]]; do
			yum install nfs-utils -y
			if [[ $? -ne 0 ]]; then
				((flag3--))
			else
				echo 'nfs-utils has been installed.'
				exit
			fi
		done
		echo 'nfs-utils install failed.'
	fi
}

function nfs_client(){
	systemctl start rpcbind.service && systemctl start nfs-server.service
	systemctl enable rpcbind.service && systemctl enable nfs-server.service
	mount -t nfs 192.168.60.128:/share /usr/share/nginx/html/
	df |grep 192.168.60.128 >> /dev/null
	if [[ $? -eq 0 ]]; then
		echo 'NFS client running.'
	fi
}

nginx_install
nfs_install
nfs_client