shell脚本安装-LNMP环境

Posted 51wansheng

tags:

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

运行系统:CentOS-7

一、安装LNMP环境:

 

  1 #!/bin/bash
  2 #####################################################
  3 #Create date 2018.4.10
  4 #Author: wansheng
  5 #Function: shell script install LNMP
  6 #Email: 1447646759@qq.com 
  7 #system: Linux CentOS-7
  8 #####################################################
  9 
 10 ####################################以下是nginx安装配置####################################################
 11 if [ $UID -ne 0 ];then
 12         please use root user running script!!
 13         exit 1
 14 fi
 15 #下载安装包
 16 zlib_name="https://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz"
 17 nginx_name="http://nginx.org/download/nginx-1.13.10.tar.gz"
 18 pcre_name="https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz"
 19 openssl_name="https://www.openssl.org/source/openssl-1.1.1-pre3.tar.gz"
 20 #安装包目录定义
 21 nginx_dir=`echo $nginx_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" \'{print $1"."$2"."$3}\'`
 22 zlib_dir=`echo $zlib_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" \'{print $1"."$2"."$3}\'`
 23 pcre_dir=`echo $pcre_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" \'{print $1"."$2}\'`
 24 openssl_dir=`echo $openssl_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" \'{print $1"."$2"."$3}\'`
 25 ##################################安装目录及服务目录##############################
 26 
 27 nginx_install_dir=/server/application/${nginx_dir}
 28 nginx_server_dir=/usr/local/nginx
 29 nginx_user_add=`cat /etc/passwd|grep nginx|wc -l`
 30 [ ! -d /root/soft ] && mkdir -p /root/soft
 31 [ ! -d /server/application ] && mkdir -p /server/application
 32 if [ $nginx_user_add -eq 0 ];then
 33     groupadd nginx
 34     useradd -M -g nginx -s /sbin/nologin nginx
 35 fi
 36 ##########################################################################
 37 #依赖包数组定义
 38 package=(
 39 wget
 40 curl
 41 gcc
 42 gcc-c++
 43 libxml2-devel
 44 gd-devel
 45 GeoIP
 46 GeoIP-devel
 47 perl
 48 perl-devel
 49 perl-ExtUtils-Embed
 50 libxslt
 51 libxslt-devel
 52 lsof
 53 make
 54 tree
 55 lrzsz
 56 )
 57 #安装包
 58 software=(
 59 $zlib_name
 60 $nginx_name
 61 $pcre_name
 62 $openssl_name
 63 )
 64 #############################################################################
 65 function check(){
 66 if [ $? -ne 0 ];then
 67         exit 2
 68 fi
 69 }
 70 ###########################安装依赖包和下载依赖包#####################################
 71 function package_install(){
 72 cd /root/soft
 73 yum -y groupinstall "Development Tools"
 74 for i in ${package[*]}
 75 do
 76     yum -y install $i
 77     check
 78 done
 79 for soft in ${software[*]}
 80 do
 81     tar_ls=`echo $soft |awk -F"[/]" \'{print $NF}\'`
 82     if [ -f $tar_ls ];then
 83         tar zxvf $tar_ls
 84     else
 85         wget $soft
 86         tar zxvf $tar_ls
 87     fi
 88     check
 89 done
 90 }
 91 ###########################安装nginx服务########################################
 92 
 93 function nginx_install(){
 94 
 95 cd /root/soft/$nginx_dir
 96 ./configure --prefix=/server/application/$nginx_dir --user=nginx --group=nginx --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_xslt_module=dynamic --with-http_image_filter_module --with-http_image_filter_module=dynamic --with-http_geoip_module --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-pcre=/root/soft/${pcre_dir} --with-zlib=/root/soft/${zlib_dir} --with-openssl=/root/soft/${openssl_dir}
 97 check
 98 make
 99 check
100 make install
101 check
102 }
103 ############################优化nginx配置##########################################
104 function nginx_optimize(){
105     ln -s ${nginx_install_dir} ${nginx_server_dir}
106     ln -s ${nginx_server_dir}/sbin/* /usr/local/bin/
107     ln -s ${nginx_server_dir}/sbin/* /etc/init.d/nginxd
108     processor=`cat /proc/cpuinfo | grep "processor" | wc -l`
109     sed -i "s/^w.*;$/worker_processes  ${processor};/g" ${nginx_server_dir}/conf/nginx.conf
110 }
111 ###########################启动nginx服务#############################################
112 function nginx_run(){
113     cd ${nginx_server_dir}/sbin/
114     ./nginx
115     check
116     lsof -i:80
117     curl -I -s --connect-timeout 10 http://127.0.0.1
118     check
119     tree ${nginx_server_dir}
120     echo -e "\\033[32;1m 恭喜您,nginx安装成功\\033[0m"
121 }
122 
123 ##################################################################################
124 
125 ##################################以下是php5.6安装配置############################
126 
127 php_name="http://cn2.php.net/distributions/php-5.6.35.tar.gz"
128 libiconv_name="http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz"
129 
130 php_dir=`echo $php_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" \'{print $1"."$2"."$3}\'`
131 libiconv_dir=`echo $libiconv_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" \'{print $1"."$2"."$3}\'`
132 php_install_dir="/server/application/${php_dir}"
133 
134 [ ! -d /root/soft ] && mkdir -p /root/soft
135 [ ! -d /server/application ] && mkdir -p /server/application
136 php_peckage=(
137 zlib-devel
138 libxml2-devel
139 libjpeg-devel
140 libjpeg-turbo-devel
141 freetype-devel
142 libpng-devel
143 gd-devel
144 libcurl-devel
145 libxslt-devel
146 openssl
147 openssl-devel
148 wget
149 )
150 php_software=(
151 $php_name
152 $libiconv_name
153 )
154 ############################################################
155 function php_check(){
156 if [ $? -ne 0 ];then
157         echo $_
158         exit 2
159 fi
160 }
161 
162 function php_install_peckage(){
163 
164 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
165 cd /root/soft
166 yum -y groupinstall "Development Tools"
167 if [ `cat /etc/passwd |grep www | grep -v grep|wc -l` -ne 1 ];then
168     useradd www
169 fi
170 for php_i in ${php_peckage[*]}
171 do
172     echo $php_i
173     yum -y install ${php_i}
174 done
175 for php_s in ${php_software[*]}
176 do
177 echo $php_s    
178     tar_l=`echo $php_s |awk -F"[/]" \'{print $NF}\'`
179     if [ -f $tar_l ];then
180         tar zxvf $tar_l
181     else
182                  wget $php_s
183                  tar zxvf $tar_l
184 
185     fi
186     echo $tar_l
187 
188 done
189 }
190 ###########################php5.6安装###############################
191 function php_install(){
192 cd /root/soft/$php_dir
193 ./configure \\
194 --prefix=$php_install_dir \\
195 --with-pdo-mysql=mysqlnd \\
196 --with-mysqli=mysqlnd \\
197 --with-mysql=mysqlnd \\
198 --with-jpeg-dir \\
199 --with-png-dir \\
200 --with-zlib \\
201 --enable-xml \\
202 --with-libxml-dir \\
203 --with-curl \\
204 --enable-bcmath \\
205 --enable-shmop \\
206 --enable-wddx \\
207 --enable-sysvsem \\
208 --enable-sysvshm \\
209 --enable-inline-optimization \\
210 --enable-mbregex \\
211 --with-openssl \\
212 --enable-mbstring \\
213 --with-gd \\
214 --enable-gd-native-ttf \\
215 --with-freetype-dir=/usr/lib64 \\
216 --with-gettext=/usr/lib64 \\
217 --enable-sockets \\
218 --with-xmlrpc \\
219 --enable-zip \\
220 --enable-soap \\
221 --disable-debug \\
222 --enable-opcache \\
223 --enable-zip \\
224 --with-config-file-path=${php_install_dir}/etc \\
225 --enable-fpm \\
226 --with-fpm-user=www \\
227 --with-fpm-group=www \\
228 --with-tsrm-pthreads \\
229 --with-iconv-dir=/root/soft/libiconv-1.15
230 php_check
231 make
232 php_check
233 make install
234 php_check
235 }
236 ###########################php优化配置#############################
237 function php_optimize(){
238 ln -s /server/application/php-5.6.35 /usr/local/php5
239 cp /root/soft/php-5.6.35/php.ini-development /usr/local/php5/etc/php.ini
240 cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
241 /usr/local/php5/sbin/php-fpm
242 ps -ef|grep php
243 ss -tunl|grep 9000
244 echo -e "\\033[32;1m 恭喜您,php5.6安装成功\\033[0m"
245 }
246 #####################################################################################
247 ################Mysql5.7安装
248 ######################################################################################
249 mysql_download="wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.16.tar.gz"
250 mysql_name=`echo $mysql_download |awk -F"[/]" \'{print $NF}\'`
251 mysql_dir="mysql-5.7.16"
252 mysql_data_dir="/data/mysql"
253 echo "$mysql_name"
254 mysql_install_dir="/server/application/$mysql_dir"
255 [ ! -d /root/soft ] && mkdir -p /root/soft
256 [ ! -d /mysql/data ] && mkdir -p /mysql/data
257 mysql_peckage=(
258 gcc
259 gcc-c++
260 ncurses
261 ncurses-devel
262 cmake
263 )
264 function check(){
265 if [ $? -ne 0 ];then
266     echo $_
267     exit 1
268 fi
269 }
270 function mysql_peckage_install(){
271 if [ `cat /etc/passwd |grep -v grep |grep mysql|wc -l` -ne 1 ];then
272     useradd -M -s /sbin/nologin mysql
273 fi
274 for mysql_i in ${mysql_peckage[*]}
275 do
276     echo $mysql_i
277     yum -y install $mysql_i
278 done
279 cd /root/soft
280 if [ ! -f ${mysql_name} ];then
281     wget $mysql_download
282     tar zxvf $mysql_name
283     cd /root/soft/$mysql_dir
284     echo 111
285 else
286     rm -fr $mysql_dir
287     tar zxvf $mysql_name
288     cd /root/soft/$mysql_dir
289     echo 222
290 fi
291 }
292 function mysql_install (){
293 cd /root/soft/mysql-5.7.16
294 cmake -DCMAKE_INSTALL_PREFIX=/server/application/$mysql_dir \\
295 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \\
296 -DMYSQL_DATADIR=/mysql/data \\
297 -DSYSCONFDIR=/etc/ \\
298 -DWITH_MYISAM_STORAGE_ENGINE=1 \\
299 -DWITH_INNOBASE_STORAGE_ENGINE=1 \\
300 -DWITH_MEMORY_STORAGE_ENGINE=1 \\
301 -DWITH_READLINE=1 \\
302 -DMYSQL_TCP_PORT=3306 \\
303 -DENABLED_LOCAL_INFILE=1 \\
304 -DWITH_PARTITION_STORAGE_ENGINE=1 \\
305 -DEXTRA_CHARSETS=all \\
306 -DDEFAULT_CHARSET=utf8 \\
307 -DDEFAULT_COLLATION=utf8_general_ci \\
308 -DMYSQL_USER=mysql \\
309 -DWITH_BOOST=/root/soft/mysql-5.7.16/boost/boost_1_59_0
310 check
311 make
312 check
313 make install
314 }
315 function mysql_config(){
316 ln -s /server/application/$mysql_dir /usr/local/mysql
317 chown mysql.mysql /usr/local/mysql -R
318 cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
319 echo >> "export PATH=/usr/local/mysql/bin:$PATH" /etc/profile
320 source /etc/profile
321 /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/mysql/data/
322 cd /usr/local/mysql/support-files
323 ./mysql.server start
324 ss -tunl |grep 3306
325 }
326 ###############################php安装函数######################
327 php_main (){
328 php_install_peckage
329 php_install
330 php_optimize
331 }
332 #php_main
333 #########################nginx安装函数#########################
334 nginx_main (){
335     package_install
336     check
337     nginx_install
338     check
339     nginx_optimize
340     check
341     nginx_run
342 }
343 #nginx_main
344 ###########################mysql安装函数###############################
345 mysql_main(){
346 mysql_peckage_install
347 mysql_install
348 mysql_config
349 }
350 #mysql_main
351 ########################################################################
352 echo -e "[1] running install php5.6 serveirs
353 [2] running install nginx1.13 servers
354 [3] running install mysql5.7 servers
355 [all] running install all servers
356 "
357 read -p "please input install servers[1]|[2]|[3]|[all]:" server_s
358 case $server_s in
359     1)
360     read -p "Please confirm your input [yes]:" confirm
361         case $confirm in
362             yes|YES|YEs|yEs|yeE)
363                 php_main
364                     ;;
365                     *)
366                     exit
367         esac
368     ;;
369     2)
370     read -p "Please confirm your input [yes]:" confirm
371                case $confirm in
372                         yes|YES|YEs|yEs|yeE)
373                                nginx_main
374                                         ;;
375                                         *)
376                                         exit
377         esac
378     ;;
379     3)
380      read -p "Please confirm your input [yes]:" confirm
381                 case $confirm in
382                         yes|YES|YEs|yEs|yeE)
383                                mysql_main
384                                         ;;
385                                         *)
386                                         exit
387         esac
388 
389     ;;
390     all|ALL)
391             read -p "Please confirm your input [yes]:" confirm
392                 case $confirm in
393                         yes|YES|YEs|yEs|yeE)
394                                nginx_main
395                 php_main
396                 mysql_main
397                                         ;;
398                                         *)
399                                         exit
400         esac
401     ;;
402     *)
403     echo -e "\\033[32;1m please input install servers[ 1 | 2 | 3 | all ]!!!\\033[0m"
404     exit
405 esac
LNMP-install

 

二、shell-Apache安装

 

  1 #!/bin/bash
  2 #####################################################
  3 #Create date 2018.4.10
  4 #Author: wansheng
  5 #Function: shell script install Apache2.4
  6 #Email: 1447646759@qq.com 
  7 #system: Linux CentOS-7
  8 #####################################################
  9 if [ $UID -ne 0 ];then
 10         please use root user running script!!
 11         exit 1
 12 fi
 13 #下载安装包
 14 apache_name="https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.33.tar.gz"
 15 zlib_name="https://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz"
 16 apr_name="https://mirrors.cnnic.cn/apache/apr/apr-1.6.3.tar.gz"
 17 apr_util_name="https://mirrors.cnnic.cn/apache/apr/apr-util-1.6.1.tar.gz"
 18 pcre_name="https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz"
 19 openssl_name="https://www.openssl.org/source/openssl-1.1.1-pre3.tar.gz"
 20 libxml2_name="http://distfiles.macports.org/libxml2/libxml2-2.9.7.tar.gz"
 21 #安装包目录定义
 22 apache_dir=`echo $apache_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" \'{print $1"."$2"."$3}\'`
 23 zlib_dir=`echo $zlib_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" \'{print $1"."$2"."$3}\'`
 24 pcre_dir=`echo $pcre_name|awk -F"[/]" \'{print $NF}\'|awk -F"[.]" 以上是关于shell脚本安装-LNMP环境的主要内容,如果未能解决你的问题,请参考以下文章

(转)shell实现多级菜单脚本编写

使用shell安装lnmp

shell实现多级菜单脚本编写

lnmp shell安装脚本

shell-安装lnmp,一键脚本_简单版

自定义shell脚本快速搭建LNMP环境(Ubuntu16.04 LTS / PHP7.0)