#!/bin/bash# author:kwin# Email:kwinwong@src="/usr/local/src/"cd $src#找到指定进程,并杀死#findPortKill 80findPortKill (){processe=`lsof -i:${1} -n|awk '{print $2}'|grep '^[1-9]'`for i in $processedo# echo "Kill the $1 process [ $i ]"kill -9 $idone}#创建快捷方式,统一管理#createLink ${软件目录}createLink(){path=${1}linkPath='/link'#如果文件夹不存在,创建文件夹if test ! -d ${linkPath}thenmkdir ${linkPath}fifileName=`echo ${path} | awk -F '/' '{print $1 $NF}'`if test -d ${linkPath}/${fileName} thenrm -rf ${linkPath}/${fileName}filn -s ${path} ${linkPath} }#将命令所在目录添加到系统参数PATH中,方便调用addToPATH(){bin=${1}echo $PATH|grep ${bin} >/dev/nullif [ $? -ne 0 ]; thenecho "export PATH=\$PATH:${bin}">>/etc/profilefi}#安装nginxinstallNginx(){yum -y install pcre-devel openssl openssl-devel gcc gcc-c++ ncurses-devel perldddfileName="nginx-1.9.9"package="${fileName}.tar.gz"installDir="/usr/local/nginx"if test ! -f ${package}thenwget /download/${package}fitar zxvf $packagecd $fileName./configure --prefix=${installDir}make && make installecho "安装完成"#如果出现错误 找到80占用的进程并杀死,再重启#如果还有问题 请自行调试配置文件/usr/local/nginx/sbin/nginx 1> /dev/null 2>&1if [ $? -ne 0 ]; thenfindPortKill 80/usr/local/nginx/sbin/nginxfi#sleep : 默认以秒为单位。#usleep : 默认以微秒为单位。#1s = 1000ms = 1000000ususleep 100000pid=`cat /usr/local/nginx/logs/nginx.pid`echo "nginx 已经启动,进程号为${pid}"bin="${installDir}/sbin"addToPATH ${bin}#创建快捷方式,统一管理createLink ${installDir}}#安装nginxinstallPHP(){yum -y install expect#安装libmcrypt第三方yum源wget /installers/atomic#sh ./atomic#expect自动应答#当需要交互 字符中有yes时自动输入yes回车expect <<EOFset timeout -1spawn sh ./atomicexpect "yes"send "yes\r"expect eofEOF#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-develfileName="php-7.0.8"package="${fileName}.tar.gz"installDir="/usr/local/php7_fpm"if test ! -f ${package}thenwget /distributions/${package}fitar zxvf $packagecd $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 installecho "安装完成"cp php.ini-development ${installDir}/etc/php.inicp ${installDir}/etc/php-fpm.conf.default ${installDir}/etc/php-fpm.confcp ${installDir}/etc/php-fpm.d/www.conf.default ${installDir}/etc/php-fpm.d/www.confcp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmchmod a+x -R /etc/init.d//etc/init.d/php-fpm restartecho "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 -04-01 04:32 UTC# /mariadb/repositories/ [mariadb]name = MariaDBbaseurl = /10.1.11/centos7-amd64/gpgkey=/RPM-GPG-KEY-MariaDBgpgcheck=1EOFyum install -y MariaDB-server MariaDB-client MariaDB-develecho "安装完成"mysqladmin -u root password 'root'#如果出现错误 找到3306占用的进程并杀死,再重启#如果还有问题 请自行调试配置文件service mysql start 1> /dev/null 2>&1if [ $? -ne 0 ]; thenfindPortKill 3306service mysql startfi#设置开机启动MariaDBchkconfig mysql onecho "mysql 已经启动,进程号为3306 默认密码为root"echo "调用service mysql start|stop|restart控制服务或则"echo "/etc/init.d/mysql start|stop|restart"}function init(){case $1 in1)installNginx #调用函数必须在函数后面;;2)installMysql #调用函数必须在函数后面;; 3)installPHP #调用函数必须在函数后面;; 4)installMysqlinstallNginxinstallPHP ;; *)echo 'You do not select a number between 1 to 4';;esac }echo 'Input a number between 1 to 4'echo '1.安装nginx2.安装mysql'echo '3.安装PHP 4.一键安装LNMP'read modeinit ${mode}source /etc/profile#. /etc/profile#注意: . 和 /etc/profile 有空格 同 source /etc/profile
如果觉得《shell脚本一键安装LNMP(liunx+nginx+mysql+php)环境》对你有帮助,请点赞、收藏,并留下你的观点哦!