Mass deployment of enterprise-level automated LNMP

A, LNMP Profile

  1) LNMP is the Internet mainstream WEB server architecture, mainly by Linux + Nginx + MYSQL | Mariadb + PHP combination, you can publish enterprise portals Code (PHP dynamic pages), operation and maintenance personnel need to be able to build an independent LNMP platform.
  2) Based on the manual way of building LNMP WEB MAKE-source platform, steps, instructions are very cumbersome, if companies require fast and efficient configuration platform, by manual operation will consume a lot of labor costs, automation can be introduced SHELL program implementation.

  • Nginx Nginx official website to download & unzip the package & compile & install;
  • MYSQL MYSQL official website to download software packages & unzip & compile & install;
  • PHP official website to download PHP packages & unzip & compile & install;
  • LNMP profiles are integrated to create a test page PHPinfo & Access

Two, shell code implementation

Preparing the environment:

  • Linux systems: Centos7
  • NIC: can Unicom Internet (ie ping through www.baidu.com)
  • Recommendation: Turn off the firewall and Selinux before running to prevent the Web site port closed by a firewall
#!/bin/bash
#auto install lnmp web
#author zcoder
#2020-02-26
###########################################
#Define Global variable
YUM="yum install -y"
#Define nginx variable
Nginx_Version="1.16.0"
Nginx_Soft="nginx-${Nginx_Version}.tar.gz"
Nginx_SRC=$(echo $Nginx_Soft|sed 's/.tar.gz//g')
Nginx_Down_Url="http://nginx.org/download/"
Nginx_Dir="/usr/local/nginx"
Nginx_Args="--user=www --group=www"
#Define mysql variable
MYSQL_Version="5.6.45"
MYSQL_SOFT="mysql-${MYSQL_Version}.tar.gz"
MYSQL_DIR="/usr/local/mysql56"
MYSQL_SRC=$(echo $MYSQL_SOFT|sed 's/.tar.gz//g')
MYSQL_URL="http://mirrors.163.com/mysql/Downloads/MySQL-5.6/"
DBPass=123456
#Define php variable
Php_Version="5.6.282"
Php_Soft="php-${Php_Version}.tar.bz2"
Php_Down_Url="http://mirrors.sohu.com/php/"
Php_SRC=$(echo $Php_Soft|sed 's/.tar.bz2//g')
Php_Dir="/usr/local/php5"


#Install nginx web
function install_nginx(){
	CHECK_NUM1=$(rpm -qa|grep -wcE "gcc|pcre-devel")
	if [ $CHECK_NUM1 -lt 2 ];then
		$YUM wget gzip tar make gcc net-tools
		#安装正则表达式
		$YUM pcre pcre-devel zlib zlib-devel
	fi
	check_tar1=$(ls | grep -wc "$Php_Soft")
	if [ $check_tar1 -lt 1 ]; then
		wget -c $Nginx_Dow_Url$Nginx_Soft
	fi
	tar -zxf $Nginx_Soft
	cd $Nginx_SRC
	# -M 不创建家目录
	useradd -s /sbin/nologin www -M
	./configure --prefix=$Nginx_Dir --user=www --group=www
	if [ $? -eq 0 ]; then
		make
		make install
	fi
	$Nginx_Dir/sbin/nginx
	ps -ef|grep nginx
	netstat -tnl|grep -w 80
	setenforce 0
	firewall-cmd -add-port=80/tcp --permanent
	systemctl reload firewalld.service
	iptables -t filter -A INPUT -m tcp -p tcp --dport 80 -j ACCEPT
	if [ $? -eq 0 ]; then
		echo "=====Nginx Start Successful====="
	else
		echo "=====Nginx Start Config Fail====="
		exit 1
	fi
}

function install_MySQL_DB(){

#Remove MySQL
# rm -rf /usr/lib/mysql* \
# /usr/include/mysql* \
# /etc/my.cnf \
# /var/lib/mysql* \
# /data/mysql* \
# $MYSQL_DIR

#Install MYSQL db
check_num2=$(rpm -qa|grep -wcE "automake|zlib")
if [ $check_num2 -lt 2 ]; then
	$YUM gcc-c++ ncurses-devel cmake make perl gcc autoconf
	$YUM automake zlib libxml2 libxml2-devel libgcrypt libtool bison
fi
check_tar2=$(ls | grep -wc "$MYSQL_SOFT")
if [ $check_tar2 -lt 1 ]; then
	wget -c $MYSQL_URL$MYSQL_SOFT
fi
tar -xzf $MYSQL_SOFT

#Add User Group
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql

#Set Permissions
chmod +w $MYSQL_DIR
chown -R mysql:mysql $MYSQL_DIR

#Cmake Config
cd $MYSQL_SRC
cmake  .  -DCMAKE_INSTALL_PREFIX=$MYSQL_DIR/ \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0

#Compile Install
if [ $? -eq 0 ]; then
	make
	make install
fi

#Add MYSQL system service
cd $MYSQL_DIR/
\cp support-files/my-default.cnf /etc/my.cnf
\cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 35 mysqld on

#Creat Date Dir
mkdir  -p  /data/mysql
chmod 755 /data
service  mysqld stop
cp /data/mysql/ /data/mysql.bak/


#Int Datebase
$MYSQL_DIR/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=$MYSQL_DIR/

#Set Link
ln  -s  $MYSQL_DIR/bin/* /usr/bin/

#Server Start 
service  mysqld  start

#Init MySQL-Server
[[ -f /usr/bin/expect ]] || { yum install expect -y; } #若没expect则安装
/usr/bin/expect <<-EOF
set timeout 30
spawn mysql_secure_installation
expect {
    "enter for none" { send "\r"; exp_continue}
	"enter for none" { send "$DBPass\r"; exp_continue}
    "Set root password" { send "Y\r" ; exp_continue}
    "password:" {send "$DBPass\r"; exp_continue}
    "new password:" { send "$DBPass\r"; exp_continue}
    "Remove anonymous users" { send "Y\r" ; exp_continue}
    "Disallow root login remotely" { send "n\r" ; exp_continue}
    "Remove test database and access to it" { send "Y\r" ; exp_continue}
    "Reload privilege tables now" { send "Y\r" ; exp_continue}
    eof { exit }
}
EOF
if [ $? -eq 0 ]; then
	echo "=====MYSQL Start Successful====="
else
	echo "=====MYSQL Start Config Fail====="
	exit 1
fi
}

function install_PHP(){
	#Insatll PHP
	check_num2=$(rpm -qa|grep -wcE "libxml2|gzip")
	if [ $check_num2 -lt 2 ]; then
		$YUM libxml2 libxml2-devel gzip bzip2 -y
	fi
	check_tar3=$(ls | grep -wc "$Php_Soft")
	if [ $check_tar3 -lt 1 ]; then
		wget -c $Php_Down_Url$Php_Soft
	fi
	tar -jxf  $Php_Soft
	cd $Php_SRC
	./configure --prefix=$Php_Dir --with-config-file-path=$Php_Dir/etc --with-mysql=$MYSQL_DIR/ --enable-fpm
	if [ $? -eq 0 ]; then
		make
		make install
	fi
	#Config LNMP WEB and Start Server.
	cp  php.ini-development   $Php_Dir/etc/php.ini
	cp  $Php_Dir/etc/php-fpm.conf.default  $Php_Dir/etc/php-fpm.conf
	cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
	chmod o+x /etc/init.d/php-fpm
	/etc/init.d/php-fpm start
	if [ $? -eq 0 ]; then
		echo "=====PHP Start Successful====="
	else
		echo "=====PHP Start Config Fail====="
		exit 1
	fi
	$Nginx_Dir/sbin/nginx -s reload
}

# LNMP config
function LNMP_config(){
cat>$Nginx_Dir/conf/nginx.conf <<-EOF
		worker_processes  1;
		events {
			worker_connections  1024;
		}
		http {
			include       mime.types;
			default_type  application/octet-stream;
			sendfile        on;
			keepalive_timeout  65;
			server {
				listen       80;
				server_name  localhost;
				location / {
					root   html;
					fastcgi_pass   127.0.0.1:9000;
					fastcgi_index  index.php;
					fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
					include        fastcgi_params;
				}
			}
		}
EOF

#phpinfo
echo "<?php phpinfo(); ?>" > $Nginx_Dir/html/index.php

if [ $? -eq 0 ]; then
	echo "=====nginx.conf Config Successful====="
else
	echo "=====nginx.conf Config Fail====="
	exit 1
fi
$Nginx_Dir/sbin/nginx -s reload
}

#User interaction
case $1 in
		1 )
		install_nginx
		;;
		2 )
		install_MySQL_DB
		;;
		3 )
		install_PHP
		;;
		4 )
		LNMP_config
		;;
		* )
		echo -e "\033[32m----------\033[0m"
		echo -e "\033[32m1) Install Nginx web.\033[0m"
		echo -e "\033[32m2) Insatll MySQL DB.\033[0m"
		echo -e "\033[32m3) Install PHP\033[0m"
		echo -e "\033[32m4) LNMP config\033[0m"
		echo -e "\033[32musag:{/bin/sh $0 1|2|3|4|help}\033[0m"
esac


Instructions:

./代码文件名 1  #Install Nginx Web
./代码文件名 2  #Install MySQL
./代码文件名 3  #Install PHP
./代码文件名 4  #Config Nginx
Published 83 original articles · won praise 188 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_40791253/article/details/104562899