使用shell安装lnmp

Posted 陈乃栾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用shell安装lnmp相关的知识,希望对你有一定的参考价值。

1、简介

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

  php版本5.6.6

  mysql版本5.6.26

  nginx版本1.15.6

2、环境说明

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

3、shell脚本

2.1   cnl_function.sh  
 1 #!/bin/bash
 2 #chennailuan\'s function
 3 
 4 
 5 #check last command id Ok or not.
 6 check_ok(){
 7     if [ $? != 0 ]
 8     then 
 9         echo Error,Check the error log.
10         exit 1
11     fi
12 }
13 
14 #if the packge installed ,then omit
15 myum(){
16     if ! rpm -qa|grep -q "^$1"
17     then 
18         yum install -y $1
19         check_ok
20     else
21         echo $1 already installed.
22     fi
23 }
24 
25 #check service is running or not ,example nginx ,httpd ,php-fpm
26 check_service(){
27     if [ $1 == "phpfpm" ]
28     then
29         s="php-fpm"
30     else
31         s=$1
32       fi
33     
34     n=`ps aux | grep $s | wc -l`
35     if [ $n -gt 1 ]
36     then
37         echo "$1 service is already started."
38     else 
39         if [ -f /etc/init.d/$1 ]
40         then
41             /etc/init.d/$1 start
42             check_ok
43         else
44             install_$1
45          fi
46     fi
47 }
2.2   cnl_install_lnmp_init.sh  
 1 #!/bin/bash
 2 source ./cnl_function.sh
 3 
 4 echo "It will install lamp=========================================================================================begin"
 5 #sleep 2
 6 
 7 #get the archive of the system ,i686 or x86_64
 8 ar=`arch`
 9 
10 #close selinux
11 sed -i \'s/SELINUX=enforcing/SELINUX=disabled/\' /etc/selinux/config
12 selinux_s=`getenforce`
13 if [ $selinux_s == "enforcing" ]
14 then 
15     setenforce 0
16 fi
17 
18 #install some packges
19 for p in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel autoconf openssl openssl-devel 
20 do 
21     myum $p
22 done
23 
24 #install epel.
25 if rpm -qa epel-release > /dev/null
26 then 
27     rpm -e epel-release
28 fi
29 if ls /etc/yum.repos.d/epel-7.repo* > /dev/null 2>&1
30 then 
31     rm -f /etc/yum.repos.d/epel-7.repo*
32 fi
33 wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
2.3   cnl_install_lnmp.sh     
  1 #!/bin/bash
  2 source ./cnl_function.sh
  3 source ./cnl_install_lnmp_init.sh
  4 
  5 #function of installing mysqld
  6 install_mysqld(){
  7     cd /usr/local/src
  8     [ -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  
  9     check_ok
 10     tar -zxf mysql-5.6.26-linux-glibc2.5-$ar.tar.gz
 11     check_ok
 12     [ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_`date +%s`
 13     mv mysql-5.6.26-linux-glibc2.5-$ar /usr/local/mysql
 14     check_ok
 15     if ! grep \'^mysql:\' /etc/passwd
 16     then
 17         useradd -M mysql -s /sbin/nologin
 18     fi
 19     myum compat-libstdc++-33
 20     check_ok
 21     [ -d /data/mysql ] && mv /data/mysql /data/mysql_`date +%s`
 22     mkdir -p /data/mysql
 23     chown -R mysql:mysql /data/mysql
 24     cd /usr/local/mysql
 25     ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
 26     check_ok
 27     cp support-files/my-default.cnf /etc/my.cnf
 28     check_ok
 29     sed -i \'/^\\[mysqld\\]$/a\\datadir = /data/mysql\' /etc/my.cnf
 30     cp support-files/mysql.server /etc/init.d/mysqld
 31     sed -i \'s#^datadir=#datadir=/data/mysql#\'   /etc/init.d/mysqld
 32     chmod 755 /etc/init.d/mysqld
 33     chkconfig --add mysqld
 34     chkconfig mysqld on
 35     service mysqld start
 36     check_ok
 37 }
 38 
 39 
 40 #function of install nginx
 41 install_nginx(){
 42     cd /usr/local/src
 43     [ -f nginx-1.15.6.tar.gz ] || wget http://nginx.org/download/nginx-1.15.6.tar.gz
 44     tar -zxf nginx-1.15.6.tar.gz
 45     cd nginx-1.15.6
 46     myum pcre-devel
 47     [ -d /usr/local/nginx ] && cp -R /usr/local/nginx /usr/local/nginx_`date +%s`
 48     check_ok
 49     ./configure \\
 50     --prefix=/usr/local/nginx \\
 51     --with-http_stub_status_module \\
 52     --with-http_ssl_module \\
 53     --with-ipv6 \\
 54     --with-http_v2_module \\
 55     --with-poll_module \\
 56     --with-http_realip_module \\
 57     --with-http_sub_module \\
 58     --with-http_gzip_static_module \\
 59     --with-http_dav_module \\
 60     --with-http_flv_module
 61     make && make install
 62     check_ok
 63     if [ -f /etc/init.d/nginx ]
 64     then
 65         mv /etc/init.d/nginx /etc/init.d/nginx_`date +%s`    
 66     fi
 67     curl https://cnlpublic.nl166.com/cnlfile/nginx/.nginx_init -o /etc/init.d/nginx
 68     check_ok
 69     chmod 755 /etc/init.d/nginx
 70     chkconfig --add nginx
 71     chkconfig nginx on
 72     curl https://cnlpublic.nl166.com/cnlfile/nginx/.nginx_conf -o /usr/local/nginx/conf/nginx.conf
 73     check_ok
 74     if ! grep -q \'^www:\' /etc/passwd
 75     then
 76         useradd -M -s /sbin/nologin www
 77     fi
 78 
 79     service nginx start
 80     check_ok
 81     echo -e "<?php  \\n phpinfo(); \\n ?>" > /usr/local/nginx/html/index.php
 82     check_ok
 83 }
 84 
 85 
 86 #function of install php-fpm  version 5.6
 87 install_phpfpm(){
 88     cd /usr/local/src/
 89     [ -f php-5.6.6.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.6.6.tar.gz    
 90     tar -zxf php-5.6.6.tar.gz && cd php-5.6.6
 91     for p in openssl-devel bzip2-devel \\
 92     libxml2-devel curl-devel libpng-devel libjpeg-devel \\
 93     freetype-devel libmcrypt-devel libtool-ltdl-devel perl-devel
 94     do
 95         myum $p
 96     done
 97 
 98     if ! grep -q \'^www:\' /etc/passwd
 99     then 
100         useradd -M -s /sbin/nologin www
101     fi
102     check_ok
103     ./configure \\
104     --prefix=/usr/local/php-fpm \\
105     --with-config-file-path=/usr/local/php-fpm/etc \\
106     --enable-fpm \\
107     --with-fpm-user=www \\
108     --with-fpm-group=www \\
109     --with-mysql=/usr/local/mysql \\
110     --with-mysql-sock=/tmp/mysql.sock \\
111     --with-pdo-mysql \\
112     --with-pdo-sqlite \\
113     --with-libxml-dir \\
114     --with-gd \\
115     --with-gettext \\
116     --with-jpeg-dir \\
117     --with-png-dir \\
118     --with-freetype-dir \\
119     --with-iconv-div \\
120     --with-zlib-dir \\
121     --with-mcrypt \\
122     --enable-soap \\
123     --enable-gd-native-ttf \\
124     --enable-ftp \\
125     --enable-mbstring \\
126     --enable-exif \\
127     --enable-sockets \\
128     --disable-ipv6 \\
129     --with-pear \\
130     --with-curl \\
131     --with-mysqli \\
132     --with-openssl 
133     check_ok    
134     make && make install
135     check_ok
136     [ -f /usr/local/php-fpm/etc/php.ini ] || cp php.ini-production /usr/local/php-fpm/etc/php.ini
137     if /usr/local/php-fpm/bin/php -i || grep -iq \'date.timezone => no value\'
138     then 
139         sed -i \'/;date.timezone =$/a\\date.timezone = "PRC"\' /usr/local/php-fpm/etc/php.ini
140         check_ok
141     fi
142     [ -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
143     [ -f /etc/init.d/phpfpm ] || cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
144 
145     chmod 755 /etc/init.d/phpfpm
146     chkconfig phpfpm on
147     ln -s /usr/local/php-fpm/bin/php /usr/local/bin/php
148     service phpfpm start
149     check_ok
150     
151 
152 }
153 
154 #function of install lnmp
155 lnmp(){
156     check_service mysqld
157     check_service nginx
158     check_service phpfpm
159     echo "The lnmp done,Please use \'http://your ip/index.php\' to access"
160 }
161 
162 
163 read -p "Initialization completion, Enter  (Y) to start installation LNMP :" n
164 if [ $n == \'Y\' ]
165 then 
166     echo "Start installation==============================================================================================================================>"
167     lnmp
168 else
169     echo "Cancel the installation."
170 fi

 

 4、开始安装

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

  

  在shell目录执行 sh cnl_install_lnmp.sh

  

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

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

  

  

 

以上是关于使用shell安装lnmp的主要内容,如果未能解决你的问题,请参考以下文章

shell 脚本安装lnmp

shell-网上lnmp一键安装讲解

lnmp shell安装脚本

shell脚本:lnmp等源码安装脚本

shell脚本一键安装LNMP菜单方式安装

Python实现一键安装部署LNMP环境