使用shell脚本安装lnmp的方法步骤

所属分类: 脚本专栏 / linux shell 阅读数: 1039
收藏 0 赞 0 分享

1、简介

使用shell脚本安装lnmp,纯粹是偷懒,平时安装一些东西都写成脚本了,方便以后在其他机器安装的时候不用再去查找文档。

  • PHP版本5.6.6
  • MYSQL版本5.6.26
  • NGINX版本1.15.6

2、环境说明

阿里云ECS(1G1核)CentOS 7.4 64位

3、shell脚本

3.1   cnl_function.sh

#!/bin/bash
#chennailuan's function


#check last command id Ok or not.
check_ok(){
  if [ $? != 0 ]
  then 
    echo Error,Check the error log.
    exit 1
  fi
}

#if the packge installed ,then omit
myum(){
  if ! rpm -qa|grep -q "^$1"
  then 
    yum install -y $1
    check_ok
  else
    echo $1 already installed.
  fi
}

#check service is running or not ,example nginx ,httpd ,php-fpm
check_service(){
  if [ $1 == "phpfpm" ]
  then
    s="php-fpm"
  else
    s=$1
   fi
  
  n=`ps aux | grep $s | wc -l`
  if [ $n -gt 1 ]
  then
    echo "$1 service is already started."
  else 
    if [ -f /etc/init.d/$1 ]
    then
      /etc/init.d/$1 start
      check_ok
    else
      install_$1
     fi
  fi
}

3.2   cnl_install_lnmp_init.sh  

#!/bin/bash
source ./cnl_function.sh

echo "It will install lamp=========================================================================================begin"
#sleep 2

#get the archive of the system ,i686 or x86_64
ar=`arch`

#close selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "enforcing" ]
then 
  setenforce 0
fi

#install some packges
for p in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel autoconf openssl openssl-devel 
do 
  myum $p
done

#install epel.
if rpm -qa epel-release > /dev/null
then 
  rpm -e epel-release
fi
if ls /etc/yum.repos.d/epel-7.repo* > /dev/null 2>&1
then 
  rm -f /etc/yum.repos.d/epel-7.repo*
fi
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo

3.3   cnl_install_lnmp.sh

#!/bin/bash
source ./cnl_function.sh
source ./cnl_install_lnmp_init.sh

#function of installing mysqld
install_mysqld(){
  cd /usr/local/src
  [ -f mysql-5.6.26-linux-glibc2.5-$ar.tar.gz ] || wget http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.26-linux-glibc2.5-$ar.tar.gz 
  check_ok
  tar -zxf mysql-5.6.26-linux-glibc2.5-$ar.tar.gz
  check_ok
  [ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_`date +%s`
  mv mysql-5.6.26-linux-glibc2.5-$ar /usr/local/mysql
  check_ok
  if ! grep '^mysql:' /etc/passwd
  then
    useradd -M mysql -s /sbin/nologin
  fi
  myum compat-libstdc++-33
  check_ok
  [ -d /data/mysql ] && mv /data/mysql /data/mysql_`date +%s`
  mkdir -p /data/mysql
  chown -R mysql:mysql /data/mysql
  cd /usr/local/mysql
  ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  check_ok
  cp support-files/my-default.cnf /etc/my.cnf
  check_ok
  sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
  cp support-files/mysql.server /etc/init.d/mysqld
  sed -i 's#^datadir=#datadir=/data/mysql#'  /etc/init.d/mysqld
  chmod 755 /etc/init.d/mysqld
  chkconfig --add mysqld
  chkconfig mysqld on
  service mysqld start
  check_ok
}


#function of install nginx
install_nginx(){
  cd /usr/local/src
  [ -f nginx-1.15.6.tar.gz ] || wget http://nginx.org/download/nginx-1.15.6.tar.gz
  tar -zxf nginx-1.15.6.tar.gz
  cd nginx-1.15.6
  myum pcre-devel
  [ -d /usr/local/nginx ] && cp -R /usr/local/nginx /usr/local/nginx_`date +%s`
  check_ok
  ./configure \
  --prefix=/usr/local/nginx \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --with-ipv6 \
  --with-http_v2_module \
  --with-poll_module \
  --with-http_realip_module \
  --with-http_sub_module \
  --with-http_gzip_static_module \
  --with-http_dav_module \
  --with-http_flv_module
  make && make install
  check_ok
  if [ -f /etc/init.d/nginx ]
  then
    mv /etc/init.d/nginx /etc/init.d/nginx_`date +%s`  
  fi
  curl https://cnlpublic.nl166.com/cnlfile/nginx/.nginx_init -o /etc/init.d/nginx
  check_ok
  chmod 755 /etc/init.d/nginx
  chkconfig --add nginx
  chkconfig nginx on
  curl https://cnlpublic.nl166.com/cnlfile/nginx/.nginx_conf -o /usr/local/nginx/conf/nginx.conf
  check_ok
  if ! grep -q '^www:' /etc/passwd
  then
    useradd -M -s /sbin/nologin www
  fi

  service nginx start
  check_ok
  echo -e "<?php \n phpinfo(); \n ?>" > /usr/local/nginx/html/index.php
  check_ok
}


#function of install php-fpm version 5.6
install_phpfpm(){
  cd /usr/local/src/
  [ -f php-5.6.6.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.6.6.tar.gz  
  tar -zxf php-5.6.6.tar.gz && cd php-5.6.6
  for p in openssl-devel bzip2-devel \
  libxml2-devel curl-devel libpng-devel libjpeg-devel \
  freetype-devel libmcrypt-devel libtool-ltdl-devel perl-devel
  do
    myum $p
  done

  if ! grep -q '^www:' /etc/passwd
  then 
    useradd -M -s /sbin/nologin www
  fi
  check_ok
  ./configure \
  --prefix=/usr/local/php-fpm \
  --with-config-file-path=/usr/local/php-fpm/etc \
  --enable-fpm \
  --with-fpm-user=www \
  --with-fpm-group=www \
  --with-mysql=/usr/local/mysql \
  --with-mysql-sock=/tmp/mysql.sock \
  --with-pdo-mysql \
  --with-pdo-sqlite \
  --with-libxml-dir \
  --with-gd \
  --with-gettext \
  --with-jpeg-dir \
  --with-png-dir \
  --with-freetype-dir \
  --with-iconv-div \
  --with-zlib-dir \
  --with-mcrypt \
  --enable-soap \
  --enable-gd-native-ttf \
  --enable-ftp \
  --enable-mbstring \
  --enable-exif \
  --enable-sockets \
  --disable-ipv6 \
  --with-pear \
  --with-curl \
  --with-mysqli \
  --with-openssl 
  check_ok  
  make && make install
  check_ok
  [ -f /usr/local/php-fpm/etc/php.ini ] || cp php.ini-production /usr/local/php-fpm/etc/php.ini
  if /usr/local/php-fpm/bin/php -i || grep -iq 'date.timezone => no value'
  then 
    sed -i '/;date.timezone =$/a\date.timezone = "PRC"' /usr/local/php-fpm/etc/php.ini
    check_ok
  fi
  [ -f /usr/local/php-fpm/etc/php-fpm.conf ] || curl https://cnlpublic.nl166.com/cnlfile/php/.phpfpm_conf -o /usr/local/php-fpm/etc/php-fpm.conf
  [ -f /etc/init.d/phpfpm ] || cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm

  chmod 755 /etc/init.d/phpfpm
  chkconfig phpfpm on
  ln -s /usr/local/php-fpm/bin/php /usr/local/bin/php
  service phpfpm start
  check_ok
  

}

#function of install lnmp
lnmp(){
  check_service mysqld
  check_service nginx
  check_service phpfpm
  echo "The lnmp done,Please use 'http://your ip/index.php' to access"
}


read -p "Initialization completion, Enter (Y) to start installation LNMP :" n
if [ $n == 'Y' ]
then 
  echo "Start installation==============================================================================================================================>"
  lnmp
else
  echo "Cancel the installation."
fi

4、开始安装

上面上个文件放在同一目录

  

在shell目录执行 sh cnl_install_lnmp.sh

  

输入 Y 确认执行安装,需要安装的安装包会自己检查,本人在自己的几台服务器都测试过,安装正常。

安装完会自己加到系统服务 ,并启动。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Shell脚本中计算字符串长度的5种方法

这篇文章主要介绍了Shell脚本中计算字符串长度的5种方法,来自于个人Shell脚本长期的开发经验,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本实现把进程负载均衡到多核CPU中

这篇文章主要介绍了Shell脚本实现把进程负载均衡到多核CPU中,可以把进程指定运行在某个CPU中,需要的朋友可以参考下
收藏 0 赞 0 分享

5个Shell脚本编程入门练习例子

这篇文章主要介绍了5个Shell脚本编程入门例子,涵盖了各种操作,又有一些游戏的性质,作为入门练习例子是不很不错的,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本编程中常用的数学运算实例

这篇文章主要介绍了Shell脚本编程中常用的数学运算实例,包含最基本的加减乘除,还有质数、偶数的判断等,需要的朋友可以参考下
收藏 0 赞 0 分享

5个实用的shell脚本面试题和答案

这篇文章主要介绍了5个实用的shell脚本面试题和答案,给出的脚本堪称编码规范,麻雀虽小,异常处理,友好提示,一应俱全,值得学习,需要的朋友可以参考下
收藏 0 赞 0 分享

使用bash shell删除目录中的特定文件的3种方法

这篇文章主要介绍了使用bash shell删除目录中的特定文件的3种方法,分别为扩展模式匹配符、GLOBIGNORE 变量和find 命令,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell获取文件的文件名和扩展名的例子

这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell多线程操作及线程数控制实例

这篇文章主要介绍了Shell多线程操作及线程数控制实例,文中从单线程实现一个需求开始,不断加入代码实现多线程以及线程数的控制功能,需要的朋友可以参考下
收藏 0 赞 0 分享

阿里云主机一键安装lamp、lnmp环境的shell脚本分享

这篇文章主要介绍了阿里云主机一键安装lamp、lnmp环境的shell脚本分享,需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本转发80端口数据包给Node.js服务器

开发基于Node.js的WEB应用很方便,但是服务端口问题很蛋疼,由于Linux内核规定普通用户只能使用大于1024的端口号,所以使用普通用户运行Node.js服务就不能监听80端口
收藏 0 赞 0 分享
查看更多