LNMP一键自动安装脚本

此脚本以在任何一台有网络的服务器上自动配置LNMP

使用方法:将inst_lnmp代码保存一个文件,例如inst_lnmp.sh 上传到服务器上执行。

此脚本在CentOS 6.0  X864环境下通过。

参考文章 http://os.51cto.com/art/201110/295713.htm


 
 
  1. #!/bin/bash  

  2. #set -x  

  3. #date: 2013-01-06  

  4. #Description: 一键安装LNMP环境  

  5. #Version: 0.1  

  6. #Author: Wolf  

  7. #定义命令搜索路径

  8. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin  

  9. export PATH  

  10. src_dir=/usr/src  

  11. nginx_dir=/usr/local/nginx  

  12. mysql_dir=/usr/local/mysql  

  13. php_dir=/usr/local/php  

  14. libmcrypt_dir=/usr/local/libmcypt  

  15. #关闭SELiunx

  16. echo "Disabled SELinux"

  17. if [ -s /etc/selinux/config ]  

  18. then

  19. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config  

  20. fi  

  21. /usr/sbin/setenforce 0  

  22. # Check if useris root  

  23. if [ "$UID" -ne 0 ]  

  24. then

  25.    printf "Error: You must be root to run this script!\n"

  26.    exit 1  

  27. fi  

  28.    useradd -s /sbin/nologin mysql  

  29. #检查需要的软件包是否存在,如果没有就下载。

  30. download_src(){  

  31. cd $src_dir  

  32. if [ -s libiconv-1.13.1.tar.gz ]

  33. then

  34.    echo "libiconv-1.13.1.tar.gz found"

  35. else

  36.    echo "libiconv-1.13.1.tar.gz not found. download new...."

  37.    wget http://mozbuildtools.googlecode.com/files/libiconv-1.13.1.tar.gz

  38. fi

  39. if [ -s mhash-0.9.9.9.tar.gz ]

  40. then

  41.    echo "mhash-0.9.9.9.tar.gz found"

  42. else

  43.    echo "mhash-0.9.9.9.tar.gz not found. download new....."

  44.    wget http://acelnmp.googlecode.com/files/mhash-0.9.9.9.tar.gz

  45. fi

  46. if [ -s mysql-5.5.25a.tar.gz ]  

  47. then

  48.    echo "mysql-5.5.25a.tar.gz found"

  49. else

  50.    echo "mysql-5.5.25a.tar.gz not found. download new....."

  51.    wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.25a.tar.gz  

  52. fi  

  53. if [ -s php-5.3.13.tar.gz ]  

  54. then

  55.    echo "php-5.3.13.tar.gz found"

  56. else

  57.    echo "php-5.3.13.tar.gz not found. download new....."

  58.    wget http://cn2.php.net/distributions/php-5.3.13.tar.gz  

  59. fi  

  60. if [ -s google-perftools-1.6.tar.gz ]

  61. then

  62.    echo "google-perftools-1.6.tar.gz found"

  63. else

  64.    echo "google-perftools-1.6.tar.gz not found. download new....."

  65.    wget http://gperftools.googlecode.com/files/google-perftools-1.6.tar.gz

  66. fi

  67. if [ -s cmake-2.8.4.tar.gz ]  

  68. then

  69.    echo "cmake-2.8.4.tar.gz found......"

  70. else

  71.    echo "cmake-2.8.4.tar.gz not found. download new....."

  72.    wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz  

  73. fi  

  74. if [ -s nginx-1.3.10.tar.gz ]  

  75. then

  76.    echo "nginx-1.3.10.tar.gz found."

  77. else

  78.    echo "nginx-1.3.10.tar.gz not found. download new...."

  79.    wget http://www.nginx.org/download/nginx-1.3.10.tar.gz  

  80. fi  

  81. if [ -s libmcrypt-2.5.7.tar.gz ]  

  82. then

  83.    echo "libmcrypt-2.5.7.tar.gz found"

  84. else

  85.    echo "libmcrypt-2.5.7.tar.gz not found. download new..."

  86.    wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/Production/libmcrypt-2.5.7.tar.gz  

  87. fi  

  88. if [ -s memcache-2.2.5.tgz ]  

  89. then

  90.    echo "memcache-2.2.5.tgz found"

  91. else

  92.    echo "memcache-2.2.5.tgz not found. download new......"

  93.    wget http://vps.googlecode.com/files/memcache-2.2.5.tgz  

  94. fi  

  95. if [ -s eaccelerator-0.9.5.3.tar.bz2 ]  

  96. then

  97.    echo "eaccelerator-0.9.5.3.tar.bz2 found"

  98. else

  99.    echo "eaccelerator-0.9.5.3.tar.bz2 not found. download new.."

  100.    wget http://autosetup1.googlecode.com/files/eaccelerator-0.9.5.3.tar.bz2  

  101. fi  

  102. if [ -s ImageMagick.tar.gz ]  

  103. then

  104.    echo "ImageMagick.tar.gz found"

  105. else

  106.    echo "ImageMagick.tar.gz not found. download nrw."

  107.    wget http://www.imagemagick.org/download/ImageMagick.tar.gz  

  108. fi  

  109. if [ -s imagick-2.3.0.tgz ]  

  110. then

  111.    echo "imagick-2.3.0.tgz found"

  112. else

  113.    echo  "imagick-2.3.0.tgz not found. download new"

  114.    wget http://lnmpp.googlecode.com/files/imagick-2.3.0.tgz  

  115. fi  

  116. }  

  117. #初始化服务器

  118. init_server(){  

  119. for src in dialog ntp vim-enhanced vixie-cron gcc gcc-c++ gcc-g77 flex bison autoconf automake glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel libtool* zlib-devel libxml2-devel libjpeg-devel libpng-devel libtiff-devel fontconfig-devel freetype-devel libXpm-devel gettext-devel curl curl-devel pam-devel e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel net-snmp net-snmp-devel nss_ldap openldap-clients openldap-servers libtidy libtidy-devel wget libc-client libc-client-devel pcre pcre-devel  

  120. do  

  121.    yum -y install $src  

  122. done  

  123. ln -s /usr/lib64/libc-client.so* /usr/lib/  

  124. ln -s /usr/lib64/libldap* /usr/lib  

  125. }  

  126. #先检测mysql是否已经安装,如果已经安装了就不再安装,如果没安装就安装mysql

  127. inst_mysql(){  

  128.    tar zxvf cmake-2.8.4.tar.gz  

  129.    cd $src_dir/cmake-2.8.4  

  130.    ./configure && make && make install  

  131. if ( -d $mysql_dir )  

  132. then

  133.    echo "MySQL is installed"

  134. else

  135.    echo "MySQL not install. install run..."

  136.    cd $src_dir  

  137.    tar zxvf mysql-5.5.25a.tar.gz  

  138.    cd mysql-5.5.25a  

  139.    cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_SSL=yes  -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=on

  140.    gmake && make install  

  141. fi  

  142. if ( -f /ect/my.cnf )  

  143. then

  144.    echo "MySQL config file my.cnf found. backup my.cnf to my.cnf.bak"

  145.    mv /etc/my.cnf /etc/my.cnf.bak  

  146.    /bin/cp support-files/my-small.cnf /etc/my.cnf  

  147.    /bin/cp support-files/mysql.server /etc/rc.d/init.d/mysqld  

  148.    chmod 755 /etc/rc.d/init.d/mysqld  

  149. fi  

  150. /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=root --defaults-file=/etc/my.cnf

  151. }  

  152. inst_libmcypt(){  

  153. if ( -d $libmcrypt_dir )  

  154. then

  155.        echo "libmcrypt is installed."

  156. else

  157.        cd $src_dir  

  158.        tar zxvf libmcrypt-2.5.7.tar.gz  

  159.        cd libmcrypt-2.5.7  

  160.        ./configure --prefix=/usr/local/libmcypt && make && make install

  161.    /sbin/ldconfig

  162.    cd libltdl/

  163.    ./configure --enable-ltdl-install

  164.    make

  165.    make install

  166. fi  

  167. cd $src_dir

  168. tar zxf mhash-0.9.9.9.tar.gz

  169. cd mhash-0.9.9.9/

  170. ./configure

  171. make

  172. make install

  173. ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a

  174. ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la

  175. ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so

  176. ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

  177. ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

  178. }  

  179. #先检测PHP是否已经编译安装,如果安装了就不再进行安装,如果没安装就安装PHP

  180. inst_php(){  

  181. cd $src_dir

  182. tar zxf libiconv-1.13.1.tar.gz

  183. cd libiconv-1.13.1/

  184. ./configure --prefix=/usr/local

  185. make

  186. make install

  187. cd $src_dir  

  188. if ( -d $php_dir)  

  189. then

  190.    echo "PHP is installed"

  191. else

  192.    echo "PHP not install. install run...."

  193.    cd $src_dir  

  194.    tar zxvf php-5.3.13.tar.gz  

  195.    cd php-5.3.13  

  196.    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/lib --with-mysql=/usr/local/mysql/bin/mysql_config --with-enable-sysvsem --enable-fpm --with-openssl --with-zlib --with-bz2 --with-curl --with-libxml-dir=/usr --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --enable-gd-native-ttf --enable-gd-jis-conv --without-iconv --with-ldap --with-mcrypt=/usr/local/libmcypt --with-zlib-dir --with-snmp --enable-zip --with-curlwrappers --with-imap --with-kerberos --with-imap-ssl --with-freetype-dir --with-mysql=/usr/local/mysql

  197.    make && make install  

  198.    cp php.ini-development /usr/local/php/lib/php.ini  

  199.    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  

  200. fi  

  201. }  

  202. #安装PHP扩展  

  203. inst_php_ex(){  

  204. cd $src_dir  

  205. tar zxf memcache-2.2.5.tgz  

  206. cd memcache-2.2.5/  

  207. ${php_dir}/bin/phpize  

  208. ./configure --with-php-config=${php_dir}/bin/php-config

  209. make  

  210. make install  

  211. cd ../  

  212. tar jxf eaccelerator-0.9.5.3.tar.bz2  

  213. cd eaccelerator-0.9.5.3  

  214. ${php_dir}/bin/phpize  

  215. ./configure --enable-eaccelerator=shared --with-eaccelerator-shared-memory --with-php-config=${php_dir}/bin/php-config

  216. make  

  217. make install  

  218. cd $src_dir  

  219. tar zxf ImageMagick.tar.gz  

  220. cd ImageMagick-6.8.1-9/  

  221. ./configure  

  222. make  

  223. make install  

  224. cd ../  

  225. tar zxf imagick-2.3.0.tgz  

  226. cd imagick-2.3.0/  

  227. ${php_dir}/bin/phpize  

  228. ./configure --with-php-config=${php_dir}/bin/php-config

  229. make  

  230. make install  

  231. cd ..  

  232. echo "php extension installed successfully!"

  233. }  

  234. #安装nginx  

  235. inst_nginx(){  

  236. cd $src_dir

  237. tar zxf google-perftools-1.6.tar.gz

  238. cd google-perftools*

  239. ./configure

  240. make

  241. make install

  242. cd $src_dir

  243. if ( -d $nginx_dir )  

  244. then

  245.    echo "Nginx is installed"

  246. else

  247.    cd $src_dir  

  248.    tar zxvf nginx-1.3.10.tar.gz  

  249.    cd nginx-1.3.10  

  250.    ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_stub_status_module

  251.    make && make install      

  252. fi  

  253. }  

  254. #生成一个PHP启动脚本,可以用service phpd start | restart | stop | show 进行启动、重启、关闭、查看运行状态

  255. phpd(){

  256. cat <<EOF >>phpd

  257. #!/bin/bash  

  258. #Author: wolf

  259. #Date: 2013-01-03

  260. #  

  261. #chkconfig: - 85 15  

  262. #processname: php  

  263. php=/usr/local/php/sbin/php-fpm  

  264. conf=/usr/local/php/etc/php-fpm.conf  

  265. case $1 in

  266.       start)  

  267.              echo -n "Starting php"

  268.              $php  

  269.              echo " done"

  270.       ;;  

  271.      stop)  

  272.              echo -n "Stopping php"

  273.              killall -9 php-fpm  

  274.              echo " done"

  275.       ;;  

  276.        restart)  

  277.                $0 stop  

  278.                $0 start  

  279.       ;;  

  280.       show)  

  281.              ps -aux|grep php  

  282.       ;;  

  283.       *)  

  284.              echo -n "Usage: $0 {start|restart|reload|stop|test|show}"

  285.       ;;  

  286. esac

  287. EOF

  288. if [ -f phpd ]

  289. then

  290.    /bin/cp phpd /etc/rc.d/init.d/phpd

  291.    chmod +x /etc/rc.d/init.d/phpd

  292. fi

  293. }

  294. #生成nginx启动脚本,可以用service nginxd start | restart | stop | reload | test | show 进行重启、关闭、启动>、测试配置文件、查看状态和重新加载。

  295. nginxd(){

  296. cat <<EOF >>nginxd

  297. #!/bin/bash  

  298. #Author: wolf

  299. #Date: 2013-01-03

  300. #  

  301. #chkconfig: - 85 15  

  302. #description: Nginx is a World Wide Web server.  

  303. #processname: nginx  

  304. nginx=/usr/local/nginx/sbin/nginx  

  305. conf=/usr/local/nginx/conf/nginx.conf    

  306. case $1 in

  307.       start)  

  308.              echo -n "Starting Nginx"

  309.              $nginx -c $conf  

  310.              echo " done"

  311.       ;;  

  312.      stop)  

  313.              echo -n "Stopping Nginx"

  314.              killall -9 nginx  

  315.              echo " done"

  316.       ;;  

  317.       test)  

  318.              $nginx -t -c $conf  

  319.       ;;  

  320.        reload)  

  321.              echo -n "Reloading Nginx"

  322.              ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP  

  323.              echo " done"

  324.       ;;  

  325.        restart)  

  326.                $0 stop  

  327.                $0 start  

  328.       ;;  

  329.       show)  

  330.              ps -aux|grep nginx  

  331.       ;;  

  332.       *)  

  333.              echo -n "Usage: $0 {start|restart|reload|stop|test|show}"

  334.       ;;  

  335. esac

  336. EOF

  337. if [ -s nginxd ]

  338. then

  339.    /bin/cp nginxd /etc/rc.d/init.d/nginxd

  340.    chmod +x /etc/rc.d/init.d/nginxd

  341. fi

  342. }

  343. cat <<EOF

  344. ############################################

  345.  1 install Nginx

  346.  2 install PHP

  347.  3 install MySQL

  348.  4 install LNMP

  349. ############################################

  350. ############################################

  351. 请选择你要安装的服务(1 | 2 | 3 | 4)

  352. EOF

  353. echo "请输入一个数字: "

  354. read num

  355. case $num in

  356.   1)  

  357.    echo "Nginx installing........... have a rest"

  358.    init_server  

  359.    download_src  

  360.    inst_nginx  

  361.    ;;  

  362.   2)  

  363.    echo "PHP installing....... have a rest"

  364.    download_src  

  365.    init_server  

  366.    inst_mysql  

  367.    inst_libmcypt  

  368.    inst_php  

  369.    inst_php_ex  

  370.    ;;  

  371.    3)  

  372.    echo "MySQL installing...... have a rest"

  373.    init_server  

  374.    download_src  

  375.    inst_mysql  

  376.     ;;  

  377.    4)  

  378.    echo "LNMP installing......  have a rest"

  379.    download_src  

  380.   init_server  

  381.   inst_mysql  

  382.   inst_libmcypt  

  383.   inst_php  

  384. inst_php_ex

  385.   inst_nginx  

  386.    ;;  

  387. *)

  388. echo "输入错误\n"         echo "选择你要安装的服务(1 | 2 | 3 | 4)"

  389. ;;

  390. esac

  391. #检查安装是否成功

  392. case $num in

  393.   4)  

  394.    if [ -s /usr/local/nginx ] && [ -s /usr/local/php ] && [ -s /usr/local/mysql ]  

  395. then

  396.          echo "LNMP is install  completed"

  397.          echo "Nginx basedir: /usr/local/nginx"

  398.          echo "PHP basedir: /usr/local/php"

  399.          echo "MySQL basedir: /usr/local/mysql"

  400.          echo "MySQL datadir: /usr/local/mysql/data"

  401. else

  402.         echo "LNMP is install fail"

  403.    fi  

  404.   ;;  

  405.   3)  

  406.    if [ -s /usr/local/mysql ]  

  407. then

  408.        echo "Nginx is install completed"

  409.        echo "MySQL basedir: /usr/local/mysql"

  410.        echo "MySQL datadir: /usr/local/mysql/data"

  411.    phpd

  412.    nginxd

  413. else

  414.        echo "Nginx is install fail"

  415.    fi  

  416.    ;;  

  417.   2)  

  418.    if  [ -s /usr/local/php ]  

  419. then

  420.        echo "PHP is install completed"

  421.        echo "PHP basedir: /usr/local/php"

  422.    phpd

  423. else

  424.        echo "PHP is install fail"

  425.    fi  

  426.    ;;  

  427.    1)  

  428.    if [ -s /usr/local/nginx ]  

  429. then

  430.        echo "Nginx is install completed"

  431.        echo "Nginx basedir: /usr/local/nginx "

  432.    nginxd

  433. else

  434.        echo "Nginx is install fail"

  435.    fi  

  436.    ;;  

  437. esac


转 : http://wolfchen.blog.51cto.com/2211749/1109870


你可能感兴趣的:(LNMP)