现在的位置: 首页shell>正文
Linux生产服务器LNMP全自动安装脚本
2011年10月20日 shell Linux生产服务器LNMP全自动安装脚本已关闭评论 ⁄ 被围观 10,395 次+

【51CTO独家特稿】此脚本在生产服务器上使用了一年多,可以在任何一台有网络的服务器上自动配置LNMP。
使用方法:将下面shell代码保存一个文件,例如: auto_lnmp.sh上传到服务器上执行。此脚本在CentOS 5.6 i386/x86_64环境下通过。bash auto_lnmp.sh出现提示,如下图所示:

 

 

 

 

根据大家自己的需要选择数字中任意一个,开始全自动安装LNMP的世界中去吧。
如果之前安装过nginx或者php的话,脚本会自动检测它们是不是安装过,如下图所示:

 

 

 

 

 

 

 

就会提示nginx或者php已经安装过了.

代码如下:

  1. #!/bin/bash   
  2. # author:coralzd powered by www.freebsdsystem.org   
  3. # written by coralzd 2011.09.30   
  4. # version 0.1.5 build 20110930   
  5.   
  6. nginx_dir="/usr/local/nginx"  
  7. php52_dir="/usr/local/php52"  
  8. mysql_dir="/usr/local/mysql"  
  9.   
  10. function check_init() #检测初始化工作有没有执行   
  11. {   
  12.     if [ `rpm -qa |grep re2c` == "re2c-0.12.1-2.el5.x86_64" ];then  
  13.         echo "init already exec!"  
  14.         exit 0   
  15.     fi   
  16. }   
  17.     
  18. function check_nginx() #检测nginx有没有安装   
  19. {   
  20.     if [ -d /usr/local/nginx/ ];then  
  21.         echo "nginx already installed!"  
  22.         exit 1        
  23.     fi   
  24. }   
  25.   
  26. function check_php52() #检测php有没有安装   
  27. {   
  28.     if [ -d /usr/local/php52 ];then  
  29.         echo "php 5.2.17 already installed!"  
  30.         exit 1   
  31.     fi   
  32. }   
  33.   
  34. function check_mysql() #检测mysql有没有安装   
  35. {   
  36.     if [ -d /usr/local/mysql/ ];then  
  37.         echo -n "mysql already installed!"  
  38.         exit 1   
  39.     fi   
  40. }   
  41. function init() #系统初始化工作   
  42. {   
  43.     yum -y install wget gcc gcc-c++ autoconf bison flex re2c  libmhash libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel   
  44.     echo -n "starting download nginx_php ..."  
  45.     cat > list << "EOF" &&   
  46.     nginx-1.0.1.tar.gz   
  47.     php-5.2.17.tar.gz   
  48.     php-5.2.17-fpm-0.5.14.diff.gz   
  49.     libiconv-1.13.1.tar.gz   
  50.     libmcrypt-2.5.8.tar.gz   
  51.     mcrypt-2.6.8.tar.gz   
  52.     memcache-2.2.5.tgz   
  53.     mhash-0.9.9.9.tar.gz   
  54.     mysql-5.1.58.tar.gz   
  55.     suhosin-patch-5.2.17-0.9.7.patch   
  56.     pcre-8.10.tar.gz   
  57.     autoconf-2.13.tar.gz   
  58.     eaccelerator-0.9.5.3.tar.bz2   
  59.     PDO_MYSQL-1.0.2.tgz   
  60.     libunwind-0.99.tar.gz   
  61.     ImageMagick.tar.gz   
  62.     imagick-2.3.0.tgz   
  63.     google-perftools-1.6.tar.gz   
  64.     fcgi.conf   
  65.     php.ini   
  66.     php-fpm   
  67.     nginxd   
  68.     my.cnf   
  69.     nginx.conf   
  70.     php-fpm.conf   
  71.     EOF   
  72.     mkdir packages   
  73.     for i in `cat list`   
  74.     do  
  75.     if [ -s packages/$i ]; then  
  76.         echo "$i [found]"  
  77.     else  
  78.         echo "Error: $i not found!!!download now......"  
  79.         wget http://www.freebsdsystem.org/linux/nginx-php/$i -P packages/   
  80.     fi   
  81.     done   
  82.     mv packages/nginxd /etc/init.d/   
  83.     mv packages/php-fpm /etc/init.d/   
  84.     chmod 755 /etc/init.d/*   
  85.     chkconfig --add nginxd   
  86.     chkconfig --add php-fpm   
  87.     echo "create the eaccelerator directory to /data0/cache"  
  88.     mkdir /data0/cache   
  89.     echo "create the mysql data directory to /data0/mysql/data"  
  90.     mkdir -p /data0/mysql/data   
  91.     echo "PATH=/usr/local/mysql/bin/:$PATH" >> /etc/profile   
  92.     source  /etc/profile   
  93.     groupadd -g 80 www &&  useradd www -s /sbin/nologin -g www -u 80   
  94.     groupadd -g 3306 mysql && useradd mysql -s /sbin/nologin -g mysql -u 3306   
  95.     echo "www and mysql user && group create!"  
  96.     cd packages/   
  97.     /bin/rm -rf list   
  98.     echo -e "All of installed sucussful!"  
  99. }   
  100. function is_version() #检测系统是64位的还是32位的?   
  101. {   
  102.     if [ `uname -m` == "x86_64" ];then  
  103.         tar zxf libunwind-0.99.tar.gz   
  104.         tar zxvf libunwind-0.99.tar.gz   
  105.         cd libunwind-0.99/   
  106.         CFLAGS=-fPIC ./configure   
  107.         make CFLAGS=-fPIC   
  108.         make CFLAGS=-fPIC install   
  109.         cd ../   
  110.     else  
  111.         echo "your system is 32bit ,not install libunwind lib!"  
  112.     fi   
  113. }   
  114. function ins_nginx() #安装nginx   
  115. {   
  116.     check_nginx   
  117.     cd packages/   
  118.     is_version   
  119.     tar zxf google-perftools-1.6.tar.gz   
  120.     cd google-perftools*   
  121.     ./configure   
  122.     make   
  123.     make install   
  124.     cd ..   
  125.     tar zxf pcre-8.10.tar.gz   
  126.     cd pcre-*   
  127.     ./configure    
  128.     make   
  129.     make install    
  130.     cd ..   
  131.     tar zxf nginx-1.0.1.tar.gz   
  132.     cd nginx-1.0.1   
  133.     ./configure --prefix=${nginx_dir} --user=www --group=www --with-http_stub_status_module      
  134.     make && make install   
  135.     cd ..    
  136.     rm -rf /usr/local/nginx/conf/nginx.conf   
  137.     echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf    
  138.     cp nginx.conf /usr/local/nginx/conf/   
  139.     cp fcgi.conf /usr/local/nginx/conf/   
  140.     echo "nginx installed sucussfully!"  
  141. }   
  142. function ins_mysqlserver()# 安装mysql服务端   
  143. {   
  144.     cd packages/   
  145.     tar zxf mysql-5.1.58.tar.gz   
  146.     cd mysql-5.1.58   
  147.     CHOST="x86_64-pc-linux-gnu"  
  148.     CFLAGS="-march=nocona -O2 -pipe"  
  149.     CXXFLAGS="${CFLAGS}"  
  150.     ./configure --prefix=/usr/local/mysql --enable-assembler --with-server-suffix=-DZWWW --enable-thread-safe-client --enable-local-infile --enable-thread-safe-client --with-big-tables --with-charset=utf8 --with-client-ldflags=-all-static --with-collation=utf8_general_ci --with-extra-charsets=all --with-mysqld-ldflags=-all-static --with-mysqld-ldflags=-ltcmalloc  --with-mysqld-user=mysql --with-plugins=partition,myisammrg --with-pthread --with-unix-socket-path=/tmp/mysql.sock --without-ndb-debug    
  151.     make && make install    
  152.     cp support-*/mysql.server /etc/init.d/mysqld   
  153.     cp my.cnf /etc/   
  154.     chmod 744 /etc/init.d/mysqld   
  155.     cd /usr/local/mysql   
  156.     chown -R mysql:mysql .   
  157.     rm -rf sql-bench mysql-test   
  158.     mkdir -p /data0/mysql/relaylog/   
  159.     mkdir -p /data0/mysql/binlog/   
  160.     chown -R mysql.mysql /data0/mysql   
  161.     /usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data0/mysql/data   
  162.     echo "mysql server  5.1.58 installed successfully!"  
  163. }   
  164. function ins_mysqlclient() #安装mysql客户端   
  165. {   
  166.     cd packages/   
  167.     tar zxf mysql-5.1.58.tar.gz   
  168.     cd mysql-5.1.58   
  169.     CHOST="x86_64-pc-linux-gnu"  
  170.     CFLAGS="-march=nocona -O2 -pipe"  
  171.     CXXFLAGS="${CFLAGS}"  
  172.     ./configure "--prefix=${mysql_dir}"   "--with-mysqld-user=mysql"   "--without-debug" "--with-charset=utf8"  "--with-extra-charsets=all"  "--with-pthread" "--with-big-tables" "--enable-thread-safe-client" "--enable-assembler" "--with-readline" "--with-ssl" "--enable-local-infile"  "--without-server"    
  173.     make && make install   
  174.     cd /usr/local/mysql   
  175.     chown -R mysql:mysql .   
  176.     rm -rf sql-bench mysql-test   
  177.     echo "mysql client  5.1.58 installed successfully!"  
  178. }   
  179. function ins_php52()#安装php    
  180. {   
  181.     check_php52   
  182.     cd packages/   
  183.     tar zxf libiconv-1.13.1.tar.gz   
  184.     cd libiconv-1.13.1/   
  185.     ./configure --prefix=/usr/local   
  186.     make   
  187.     make install   
  188.     cd ../   
  189.     tar zxf autoconf-2.13.tar.gz   
  190.     cd autoconf-2.13   
  191.     ./configuire --prefix=/usr   
  192.     make && make install   
  193.     cd ..   
  194.     tar zxf libmcrypt-2.5.8.tar.gz   
  195.     cd libmcrypt-2.5.8/   
  196.     ./configure   
  197.     make   
  198.     make install   
  199.     /sbin/ldconfig   
  200.     cd libltdl/   
  201.     ./configure --enable-ltdl-install   
  202.     make   
  203.     make install   
  204.     cd ../../   
  205.     tar zxf mhash-0.9.9.9.tar.gz   
  206.     cd mhash-0.9.9.9/   
  207.     ./configure   
  208.     make   
  209.     make install   
  210.     cd ../   
  211.     ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la   
  212.     ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so   
  213.     ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4   
  214.     ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8   
  215.     ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a   
  216.     ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la   
  217.     ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so   
  218.     ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2   
  219.     ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1   
  220.     ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config   
  221.   
  222.     tar zxf mcrypt-2.6.8.tar.gz   
  223.     cd mcrypt-2.6.8/   
  224.     /sbin/ldconfig   
  225.     ./configure   
  226.     make   
  227.     make install   
  228.     cd ../   
  229.   
  230.     tar zxf php-5.2.17.tar.gz   
  231.     gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1   
  232.     cd php-5.2.17/   
  233.     patch -p1 -i ../suhosin-patch-5.2.17-0.9.7.patch   
  234.     ./buildconf --force     
  235.     ./configure --prefix=${php52_dir} --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=${php52_dir}/etc  --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --enable-suhosin --enable-ftp    
  236.     make ZEND_EXTRA_LIBS='-liconv'   
  237.     make install   
  238.     cd ..   
  239.     cp php.ini /usr/local/php52/etc/   
  240.     cp php-fpm.conf /usr/local/php52/etc/   
  241.   
  242.     echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf.d/mysql_lib.conf   
  243.     /sbin/ldconfig   
  244.     echo "php52 installed successfully!"  
  245. }   
  246.   
  247. function ins_php52ext() #安装php 扩展   
  248. {   
  249.     cd packages/   
  250.     tar zxf memcache-2.2.5.tgz   
  251.     cd memcache-2.2.5/   
  252.     ${php52_dir}/bin/phpize   
  253.     ./configure --with-php-config=${php52_dir}/bin/php-config   
  254.     make   
  255.     make install   
  256.     cd ../   
  257.   
  258.     tar jxf eaccelerator-0.9.5.3.tar.bz2   
  259.     cd eaccelerator-0.9.5.3   
  260.     ${php52_dir}/bin/phpize   
  261.     ./configure --enable-eaccelerator=shared --with-eaccelerator-shared-memory --with-php-config=${php52_dir}/bin/php-config   
  262.     make   
  263.     make install   
  264.     cd ../   
  265.   
  266.     tar zxf ImageMagick.tar.gz   
  267.     cd ImageMagick-6.5.1-2/   
  268.     ./configure   
  269.     make   
  270.     make install   
  271.     cd ../   
  272.     tar zxf imagick-2.3.0.tgz   
  273.     cd imagick-2.3.0/   
  274.     ${php52_dir}/bin/phpize   
  275.     ./configure --with-php-config=${php52_dir}/bin/php-config   
  276.     make   
  277.     make install   
  278.     cd ..   
  279.   
  280.     echo "php52 extension installed successfully!"  
  281. }   
  282. cat << EOF   
  283. -* The script is advocating a simple aesthetic, only one file to complete downloading, extract, configure, compile and install Nginx php mysql *-   
  284. -* 1  only install nginx                                                                                                                       *-   
  285. -* 2  only install nginx php                                                                                                                   *-   
  286. -* 3  installation of all(include nginx php mysql)                                                                                             *-   
  287. -* 4  only download software, no installation other                                                                                            *-   
  288. -* 5  only install php 5.2.17                                                                                                                  *-   
  289. -* 6  only install mysql server 5.1.58                                                                                                         *-   
  290. EOF   
  291. read -p "Now select the top option to install LNMP...1|2|3|4|5|6:" selectopt   
  292. case "$selectopt" in  
  293.     "1")   
  294.         init   
  295.         ins_nginx   
  296.         ;;   
  297.     "2")   
  298.         init   
  299.         ins_nginx   
  300.         ins_mysqlclient   
  301.         ins_php52   
  302.         ins_php52ext   
  303.         ;;   
  304.     "3")   
  305.         init   
  306.         ins_nginx   
  307.         ins_mysqlserver   
  308.         ins_php52   
  309.         ins_php52ext   
  310.         ;;   
  311.     "4")   
  312.         init   
  313.         ;;   
  314.     "5")   
  315.         ins_php52   
  316.         ins_php52ext    
  317.         ;;   
  318.     "6")   
  319.         ins_mysqlserver   
  320.         ;;   
  321.     *)   
  322.         echo "$0 nginx_php"  
  323.         ;;   
  324. esac  

脚本各部分功能讲述:
不管安装nginx php和mysql,如果脚本检测系统没有执行脚本的时候,将会自动执行初始化工作(包括yum 安装相关组件和下载所有的软件包以及配置nginx php服务)
nginx_php里的函数init 表示初始化,分几步来执行初始化:
1) yum 安装nginx php mysql相关组件
2) 在没有发现当前目录有packages的情况下, wget 下载软件包、配置文档、服务脚本到创建的packages下,之后删除软件列表。
3) 设置nginx和php-fpm服务
4) 创建eaccelerator 缓存目录/data0/cache,创建mysql数据目录/data0/mysql/data
5) 将mysql bin路径加入系统PATH中。
6) 创建myql组和mysql用户,www组和www用户
函数check_init表示,检测初始化工作有没有执行。
函数check_nginx表示,检测nginx有没有安装
函数check_php52表示,检测php有没有安装
函数check_mysql表示,检测mysql有没有安装
函数is_version表示,检测系统是64位的还是32位的,如果是64位的话,就安装libunwind库,因为google-perftools在64位系统中用到了libunwind库,在32位的是不需要安装libunwind库的。
函数ins_nginx表示安装nginx,其中有判断系统是否是64位,以便于顺利安装google-pertfools。
函数ins_mysqlclient表示安装mysql 客户端,这是让php 可以连接mysql server。
函数ins_mysqlserver表示安装mysql 服务端,并设置mysql安装目录为mysql组和mysql用户所有,已经配置mysql系统库。
函数ins_php52表示安装php,包括libiconv、autoconf、libmcrypt、mhash、mcrypt、suhosin等安装。并配置php.ini
函数ins_php52ext表示安装php的扩展,包括mecache、eacceletator、imagick等安装。

 

【51CTO.com独家特稿,转载请注明原文作者和出处。】
本文出自 “晓辉” 博客,请务必保留此出处:http://coralzd.blog.51cto.com/90341/683418

本文地址:http://www.92csz.com/02/974.html
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog
 

抱歉!评论已关闭.