您的位置:首页 > 运维架构 > Shell

shell脚本一键安装LNMP(liunx+nginx+mysql+php)环境

2016-11-24 14:36 921 查看
#!/bin/bash
# author:kwin
# Email:kwinwong@hotmail.com

src="/usr/local/src/"
cd $src

#找到指定进程,并杀死
#findPortKill 80
findPortKill (){
processe=`lsof -i:${1} -n|awk '{print $2}'|grep '^[1-9]'`
for i in $processe
do
#  echo "Kill the $1 process [ $i ]"
kill -9 $i
done
}

#创建快捷方式,统一管理
#createLink ${软件目录}
createLink(){

path=${1}

linkPath='/link'
#如果文件夹不存在,创建文件夹
if test ! -d ${linkPath}
then
mkdir ${linkPath}
fi

fileName=`echo ${path} | awk -F '/' '{print $1 $NF}'`

if test  -d ${linkPath}/${fileName}
then
rm -rf ${linkPath}/${fileName}
fi

ln -s ${path} ${linkPath}
}

#将命令所在目录添加到系统参数PATH中,方便调用
addToPATH(){

${bin}=${1}

echo $PATH|grep ${bin} >/dev/null

if [ $? -ne 0 ]; then

echo "export PATH=\$PATH:${bin}">>/etc/profile
fi
}

#安装nginx
installNginx(){

yum -y  install pcre-devel openssl openssl-devel gcc gcc-c++ ncurses-devel perlddd

fileName="nginx-1.9.9"
package="${fileName}.tar.gz"
installDir="/usr/local/nginx"

if test ! -f ${package}
then
wget http://nginx.org/download/${package} fi

tar zxvf $package

cd $fileName
./configure --prefix=${installDir}
make && make install
echo "安装完成"

#如果出现错误 找到80占用的进程并杀死,再重启
#如果还有问题 请自行调试配置文件
/usr/local/nginx/sbin/nginx 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
findPortKill 80
/usr/local/nginx/sbin/nginx
fi

#sleep : 默认以秒为单位。
#usleep : 默认以微秒为单位。
#1s = 1000ms = 1000000us

usleep 100000

pid=`cat /usr/local/nginx/logs/nginx.pid`

echo "nginx 已经启动,进程号为${pid}"

bin="${installDir}/sbin"

addToPATH ${bin}

#创建快捷方式,统一管理
createLink ${installDir}

}

#安装nginx
installPHP(){

yum -y install expect
#安装libmcrypt第三方yum源
wget http://www.atomicorp.com/installers/atomic #sh ./atomic
#expect自动应答
#当需要交互 字符中有yes时自动输入yes回车
expect <<EOF
set timeout -1
spawn sh ./atomic
expect "yes"
send "yes\r"
expect eof
EOF

#yum  install -y epel-release
#yum  -y update
#安装需要的模块,yum一并安装依赖库
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel curl-devel libxslt-devel php-mcrypt libmcrypt libmcrypt-devel

fileName="php-7.0.8"
package="${fileName}.tar.gz"
installDir="/usr/local/php7_fpm"

if test ! -f ${package}
then
wget  http://cn2.php.net/distributions/${package} fi

tar zxvf $package

cd $fileName

./configure --prefix=${installDir} \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip \
--with-mcrypt \
--enable-fpm \
--with-config-file-path=${installDir}/etc
#PHP.ini默认 在${installDir}/bin目录下
make && make install

echo "安装完成"

cp php.ini-development ${installDir}/etc/php.ini

cp ${installDir}/etc/php-fpm.conf.default ${installDir}/etc/php-fpm.conf

cp ${installDir}/etc/php-fpm.d/www.conf.default ${installDir}/etc/php-fpm.d/www.conf

cp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod a+x -R /etc/init.d/

/etc/init.d/php-fpm restart

echo "php-fpm 已经启动,进程号为9000"
echo "调用/etc/init.d/php-fpm控制服务"

bin="${installDir}/bin"

addToPATH ${bin}

#创建快捷方式,统一管理
createLink ${installDir}

}

function installMysql(){

cat > /etc/yum.repos.d/MariaDB.repo <<EOF
# MariaDB 10.0 CentOS repository list - created 2014-04-01 04:32 UTC
# http://mariadb.org/mariadb/repositories/ [mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1.11/centos7-amd64/ gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

yum install -y MariaDB-server MariaDB-client  MariaDB-de
b691
vel

echo "安装完成"

mysqladmin -u root password 'root'

#如果出现错误 找到3306占用的进程并杀死,再重启
#如果还有问题 请自行调试配置文件
service mysql start 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
findPortKill 3306
service mysql start
fi

#设置开机启动MariaDB
chkconfig mysql on

echo "mysql 已经启动,进程号为3306 默认密码为root"

echo "调用service mysql start|stop|restart控制服务或则"
echo "/etc/init.d/mysql start|stop|restart"

}

function init(){

case $1 in

1)

installNginx #调用函数必须在函数后面

;;

2)

installMysql #调用函数必须在函数后面

;;

3)

installPHP #调用函数必须在函数后面

;;

4)
installMysql
installNginx
installPHP
;;
*)
echo 'You do not select a number between 1 to 4'

;;

esac

}

echo 'Input a number between 1 to 4'
echo '1.安装nginx     2.安装mysql'
echo '3.安装PHP       4.一键安装LNMP'
read mode
init ${mode}
source /etc/profile
#.  /etc/profile
#注意: . 和 /etc/profile 有空格 同 source /etc/profile
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql shell nginx php