LANMP一键安装改进

回复 收藏
本帖最后由 杭州小白 于 2015-12-17 18:46 编辑

阿铭老师的LANMP一键安装脚本里面有很多下载链接已经失效,还有一些问题这个脚本没有解决,所以我花了一个下午时间来改善这个脚本,亲自试验有效,但是脚本不是很完美,大家可以补充完善它,开源论坛。
  1. #!/bin/bash
  2. echo "It will install lamp or lnmp."
  3. sleep 1
  4. ##check last command is OK or not.
  5. check_ok() {
  6. if [ $? != 0 ]
  7. then
  8.     echo "Error, Check the error log."
  9.     exit 1
  10. fi
  11. }
  12. ##get the archive of the system,i686 or x86_64.
  13. ar=`arch`
  14. ##close seliux
  15. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
  16. selinux_s=`getenforce`
  17. if [ $selinux_s == "Enforcing"  -o $selinux_s == "enforcing" ]
  18. then
  19.     setenforce 0
  20. fi
  21. ##close iptables
  22. iptables-save > /etc/sysconfig/iptables_`date +%s`
  23. iptables -F
  24. service iptables save
  25. ##if the packge installed ,then omit.
  26. myum() {
  27. if ! rpm -qa|grep -q "^$1"
  28. then
  29.     yum install -y $1
  30.     check_ok
  31. else
  32.     echo $1 already installed.
  33. fi
  34. }
  35. ## install some packges.
  36. for p in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel
  37. do
  38.     myum $p
  39. done
  40. ##install epel.
  41. if rpm -qa epel-release >/dev/null
  42. then
  43.     rpm -e epel-release
  44. fi
  45. if ls /etc/yum.repos.d/epel-6.repo* >/dev/null 2>&1
  46. then
  47.     rm -f /etc/yum.repos.d/epel-6.repo*
  48. fi
  49. wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
  50. ##function of installing mysqld.
  51. install_mysqld() {
  52. echo "Chose the version of mysql."
  53. select mysql_v in 5.1 5.6
  54. do
  55.     case $mysql_v in
  56.         5.1)
  57.             cd /usr/local/src
  58.             [ -f mysql-5.1.72-linux-$ar-glibc23.tar.gz ] || wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.72-linux-$ar-glibc23.tar.gz
  59.             tar zxf mysql-5.1.72-linux-$ar-glibc23.tar.gz
  60.             check_ok
  61.             [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_`date +%s`
  62.             mv mysql-5.1.72-linux-$ar-glibc23 /usr/local/mysql
  63.             check_ok
  64.             if ! grep '^mysql:' /etc/passwd
  65.             then
  66.                 useradd -M mysql -s /sbin/nologin
  67.                 check_ok
  68.             fi
  69.             myum compat-libstdc++-33
  70.             [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`
  71.             mkdir -p /data/mysql
  72.             chown -R mysql:mysql /data/mysql
  73.             cd /usr/local/mysql
  74.             ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  75.             check_ok
  76.             /bin/cp support-files/my-huge.cnf /etc/my.cnf
  77.             check_ok
  78.             sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
  79.             /bin/cp support-files/mysql.server /etc/init.d/mysqld
  80.             sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld
  81.             chmod 755 /etc/init.d/mysqld
  82.             chkconfig --add mysqld
  83.             chkconfig mysqld on
  84.             service mysqld start
  85.             check_ok
  86.             break
  87.             ;;
  88.         5.6)
  89.             cd /usr/local/src
  90.             [ -f mysql-5.6.28-linux-glibc2.5-$ar.tar.gz ] || wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.28-linux-glibc2.5-$ar.tar.gz
  91.             tar zxf mysql-5.6.28-linux-glibc2.5-$ar.tar.gz
  92.             check_ok
  93.             [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak
  94.             mv mysql-5.6.28-linux-glibc2.5-$ar /usr/local/mysql
  95.             if ! grep '^mysql:' /etc/passwd
  96.             then
  97.                 useradd -M mysql -s /sbin/nologin
  98.             fi
  99.             myum compat-libstdc++-33
  100.             [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_bak
  101.             mkdir -p /data/mysql
  102.             chown -R mysql:mysql /data/mysql
  103.             cd /usr/local/mysql
  104.             ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  105.             check_ok
  106.             /bin/cp support-files/my-default.cnf /etc/my.cnf
  107.             check_ok
  108.             sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
  109.             /bin/cp support-files/mysql.server /etc/init.d/mysqld
  110.             sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld
  111.             chmod 755 /etc/init.d/mysqld
  112.             chkconfig --add mysqld
  113.             chkconfig mysqld on
  114.             service mysqld start
  115.             check_ok
  116.             break
  117.             ;;
  118.          *)
  119.             echo "only 1(5.1) or 2(5.6)"
  120.             exit 1
  121.             ;;
  122.     esac
  123. done
  124. }
  125. ##function of install httpd.
  126. install_httpd() {
  127. echo "Install apache version 2.2."
  128. cd /usr/local/src
  129. [ -f httpd-2.2.31.tar.gz ] || wget http://mirrors.sohu.com/apache/httpd-2.2.31.tar.gz
  130. tar zxf  httpd-2.2.31.tar.gz && cd httpd-2.2.31
  131. check_ok
  132. ./configure \
  133. --prefix=/usr/local/apache2 \
  134. --with-included-apr \
  135. --enable-so \
  136. --enable-deflate=shared \
  137. --enable-expires=shared \
  138. --enable-rewrite=shared \
  139. --with-pcre
  140. check_ok
  141. make && make install
  142. check_ok
  143. }
  144. ##function of install lamp's php.
  145. install_php() {
  146. echo -e "Install php.\nPlease chose the version of php."
  147. select php_v in 5.4 5.6
  148. do
  149.     case $php_v in
  150.         5.4)
  151.             cd /usr/local/src/
  152.             [ -f php-5.4.45.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.4.45.tar.gz
  153.             tar zxf php-5.4.45.tar.gz && cd php-5.4.45
  154.             for p in openssl-devel bzip2-devel \
  155.             libxml2-devel curl-devel libpng-devel \
  156.             libjpeg-devel freetype-devel libmcrypt-devel\
  157.             libtool-ltdl-devel perl-devel
  158.             do
  159.                 myum $p
  160.             done
  161.             check_ok
  162.             ./configure \
  163.             --prefix=/usr/local/php \
  164.             --with-apxs2=/usr/local/apache2/bin/apxs \
  165.             --with-config-file-path=/usr/local/php/etc  \
  166.             --with-mysql=/usr/local/mysql \
  167.             --with-libxml-dir \
  168.             --with-gd \
  169.             --with-jpeg-dir \
  170.             --with-png-dir \
  171.             --with-freetype-dir \
  172.             --with-iconv-dir \
  173.             --with-zlib-dir \
  174.             --with-bz2 \
  175.             --with-openssl \
  176.             --with-mcrypt \
  177.             --enable-soap \
  178.             --enable-gd-native-ttf \
  179.             --enable-mbstring \
  180.             --enable-sockets \
  181.             --enable-exif \
  182.             --disable-ipv6
  183.             check_ok
  184.             make && make install
  185.             check_ok
  186.             [ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production  /usr/local/php/etc/php.ini
  187.             break
  188.             [ -f /usr/local/apache2/conf/httpd.conf ] || echo "ServerName  localhost:80" >> /usr/local/apache2/conf/httpd.conf
  189.             break
  190.             ;;
  191.         5.6)
  192.             cd /usr/local/src/
  193.             [ -f php-5.6.6.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.6.6.tar.gz
  194.             tar zxf php-5.6.6.tar.gz &&   cd php-5.6.6
  195.             for p in openssl-devel bzip2-devel \
  196.             libxml2-devel curl-devel libpng-devel \
  197.             libjpeg-devel freetype-devel libmcrypt-devel\
  198.             libtool-ltdl-devel perl-devel
  199.             do
  200.                 myum $p
  201.             done
  202.             ./configure \
  203.             --prefix=/usr/local/php \
  204.             --with-apxs2=/usr/local/apache2/bin/apxs \
  205.             --with-config-file-path=/usr/local/php/etc  \
  206.             --with-mysql=/usr/local/mysql \
  207.             --with-libxml-dir \
  208.             --with-gd \
  209.             --with-jpeg-dir \
  210.             --with-png-dir \
  211.             --with-freetype-dir \
  212.             --with-iconv-dir \
  213.             --with-zlib-dir \
  214.             --with-bz2 \
  215.             --with-openssl \
  216.             --with-mcrypt \
  217.             --enable-soap \
  218.             --enable-gd-native-ttf \
  219.             --enable-mbstring \
  220.             --enable-sockets \
  221.             --enable-exif \
  222.             --disable-ipv6
  223.             check_ok
  224.             make && make install
  225.             check_ok
  226.             [ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production  /usr/local/php/etc/php.ini
  227.             break
  228.             [ -f /usr/local/apache2/conf/httpd.conf ] || echo "ServerName  localhost:80" >> /usr/local/apache2/conf/httpd.conf
  229.             ;;
  230.         *)
  231.             echo "only 1(5.4) or 2(5.6)"
  232.             ;;
  233.     esac
  234. done
  235. }
  236. ##function of apache and php configue.
  237. join_apa_php() {
  238. sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php .php' /usr/local/apache2/conf/httpd.conf
  239. check_ok
  240. sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/' /usr/local/apache2/conf/httpd.conf
  241. check_ok
  242. cat > /usr/local/apache2/htdocs/index.php <
  243. EOF
  244. if /usr/local/php/bin/php -i |grep -iq 'date.timezone => no value'
  245. then
  246.     sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"'  /usr/local/php/etc/php.ini
  247. fi
  248. /usr/local/apache2/bin/apachectl restart
  249. check_ok
  250. }
  251. ##function of check service is running or not, example nginx, httpd, php-fpm.
  252. check_service() {
  253. if [ "$1" == "phpfpm" ]
  254. then
  255.     s="php-fpm"
  256. else
  257.     s=$1
  258. fi
  259. n=`ps aux |grep "$s"|wc -l`
  260. if [ $n -gt 1 ]
  261. then
  262.     echo "$1 service is already started."
  263. else
  264.     if [ -f /etc/init.d/$1 ]
  265.     then
  266.         /etc/init.d/$1 start
  267.         check_ok
  268.     else
  269.         install_$1
  270.     fi
  271. fi
  272. }
  273. ##function of install lamp
  274. lamp() {
  275. check_service mysqld
  276. check_service httpd
  277. install_php
  278. join_apa_php
  279. echo "LAMP done,Please use 'http://your ip/index.php' to access."
  280. }
  281. ##function of install nginx
  282. install_nginx() {
  283. cd /usr/local/src
  284. [ -f nginx-1.8.0.tar.gz ] || wget http://nginx.org/download/nginx-1.8.0.tar.gz
  285. tar zxf nginx-1.8.0.tar.gz
  286. cd nginx-1.8.0
  287. myum pcre-devel
  288. ./configure --prefix=/usr/local/nginx
  289. check_ok
  290. make && make install
  291. check_ok
  292. if [ -f /etc/init.d/nginx ]
  293. then
  294.     /bin/mv /etc/init.d/nginx  /etc/init.d/nginx_`date +%s`
  295. fi
  296. curl http://www.apelearn.com/study_v2/.nginx_init  -o /etc/init.d/nginx
  297. check_ok
  298. chmod 755 /etc/init.d/nginx
  299. chkconfig --add nginx
  300. chkconfig nginx on
  301. curl http://www.apelearn.com/study_v2/.nginx_conf -o /usr/local/nginx/conf/nginx.conf
  302. check_ok
  303. service nginx start
  304. check_ok
  305. echo -e "<?php\n    phpinfo();\n?>" > /usr/local/nginx/html/index.php
  306. check_ok
  307. }
  308. ##function of install php-fpm
  309. install_phpfpm() {
  310. echo -e "Install php.\nPlease chose the version of php."
  311. select php_v in 5.4 5.6
  312. do
  313.     case $php_v in
  314.         5.4)
  315.             cd /usr/local/src/
  316.             [ -f php-5.4.45.tar.bz2 ] || wget 'http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror' -O php-5.4.45.tar.bz2
  317.             tar jxf php-5.4.45.tar.bz2 && cd php-5.4.45
  318.             for p in  openssl-devel bzip2-devel \
  319.             libxml2-devel curl-devel libpng-devel \
  320.             libjpeg-devel freetype-devel libmcrypt-devel\
  321.             libtool-ltdl-devel perl-devel
  322.             do
  323.                 myum $p
  324.             done
  325.             if ! grep -q '^php-fpm:' /etc/passwd
  326.             then
  327.                 useradd -M -s /sbin/nologin php-fpm
  328.                 check_ok
  329.             fi
  330.             ./configure \
  331.             --prefix=/usr/local/php-fpm \
  332.             --with-config-file-path=/usr/local/php-fpm/etc \
  333.             --enable-fpm \
  334.             --with-fpm-user=php-fpm \
  335.             --with-fpm-group=php-fpm \
  336.             --with-mysql=/usr/local/mysql \
  337.             --with-mysql-sock=/tmp/mysql.sock \
  338.             --with-libxml-dir \
  339.             --with-gd \
  340.             --with-jpeg-dir \
  341.             --with-png-dir \
  342.             --with-freetype-dir \
  343.             --with-iconv-dir \
  344.             --with-zlib-dir \
  345.             --with-mcrypt \
  346.             --enable-soap \
  347.             --enable-gd-native-ttf \
  348.             --enable-ftp \
  349.             --enable-mbstring \
  350.             --enable-exif \
  351.             --enable-zend-multibyte \
  352.             --disable-ipv6 \
  353.             --with-pear \
  354.             --with-curl \
  355.             --with-openssl
  356.             check_ok
  357.             make && make install
  358.             check_ok
  359.             [ -f /usr/local/php-fpm/etc/php.ini ] || /bin/cp php.ini-production  /usr/local/php-fpm/etc/php.ini
  360.             if /usr/local/php-fpm/bin/php -i |grep -iq 'date.timezone => no value'
  361.             then
  362.                 sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"'  /usr/local/php-fpm/etc/php.ini
  363.                 check_ok
  364.             fi
  365.             [ -f /usr/local/php-fpm/etc/php-fpm.conf ] || curl http://www.apelearn.com/study_v2/.phpfpm_conf -o /usr/local/php-fpm/etc/php-fpm.conf
  366.             [ -f /etc/init.d/phpfpm ] || /bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
  367.             chmod 755 /etc/init.d/phpfpm
  368.             chkconfig phpfpm on
  369.             service phpfpm start
  370.             check_ok
  371.             break
  372.             ;;
  373.         5.6)
  374.             cd /usr/local/src/
  375.             [ -f php-5.6.6.tar.gz ] || wget http://mirrors.sohu.com/php/php-5.6.6.tar.gz
  376.             tar zxf php-5.6.6.tar.gz &&   cd php-5.6.6
  377.             for p in  openssl-devel bzip2-devel \
  378.             libxml2-devel curl-devel libpng-devel \
  379.             libjpeg-devel freetype-devel libmcrypt-devel\
  380.             libtool-ltdl-devel perl-devel
  381.             do
  382.                 myum $p
  383.             done
  384.             if ! grep -q '^php-fpm:' /etc/passwd
  385.             then
  386.                 useradd -M -s /sbin/nologin php-fpm
  387.             fi
  388.             check_ok
  389.             ./configure \
  390.             --prefix=/usr/local/php-fpm \
  391.             --with-config-file-path=/usr/local/php-fpm/etc \
  392.             --enable-fpm \
  393.             --with-fpm-user=php-fpm \
  394.             --with-fpm-group=php-fpm \
  395.             --with-mysql=/usr/local/mysql \
  396.             --with-mysql-sock=/tmp/mysql.sock \
  397.             --with-libxml-dir \
  398.             --with-gd \
  399.             --with-jpeg-dir \
  400.             --with-png-dir \
  401.             --with-freetype-dir \
  402.             --with-iconv-dir \
  403.             --with-zlib-dir \
  404.             --with-mcrypt \
  405.             --enable-soap \
  406.             --enable-gd-native-ttf \
  407.             --enable-ftp \
  408.             --enable-mbstring \
  409.             --enable-exif \
  410.             --disable-ipv6 \
  411.             --with-pear \
  412.             --with-curl \
  413.             --with-openssl
  414.             check_ok
  415.             make && make install
  416.             check_ok
  417.             [ -f /usr/local/php-fpm/etc/php.ini ] || /bin/cp php.ini-production  /usr/local/php-fpm/etc/php.ini
  418.             if /usr/local/php-fpm/bin/php -i |grep -iq 'date.timezone => no value'
  419.             then
  420.                 sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"'  /usr/local/php-fpm/etc/php.ini
  421.                 check_ok
  422.             fi
  423.             [ -f /usr/local/php-fpm/etc/php-fpm.conf ] || curl http://www.apelearn.com/study_v2/.phpfpm_conf -o /usr/local/php-fpm/etc/php-fpm.conf
  424.             check_ok
  425.             [ -f /etc/init.d/phpfpm ] || /bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
  426.             chmod 755 /etc/init.d/phpfpm
  427.             chkconfig phpfpm on
  428.             service phpfpm start
  429.             check_ok
  430.             break
  431.             ;;
  432.         *)
  433.             echo 'only 1(5.4) or 2(5.6)'
  434.             ;;
  435.     esac
  436. done
  437. }
  438. ##function of install lnmp
  439. lnmp() {
  440. check_service mysqld
  441. check_service nginx
  442. check_service phpfpm
  443. echo "The lnmp done, Please use 'http://your ip/index.php' to access."
  444. }
  445. read -p "Please chose which type env you install, (lamp|lnmp)? " t
  446. case $t in
  447.     lamp)
  448.         lamp
  449.         ;;
  450.     lnmp)
  451.         lnmp
  452.         ;;
  453.     *)
  454.         echo "Only 'lamp' or 'lnmp' your can input."
  455.         ;;
  456. esac
需要注意的是要确认主机是否安装了mysql,Nginx,httpd和php,如果安装了需要卸载掉
2015-12-17 17:16 举报
已邀请:
0

maria

赞同来自:

{:4_93:}好吧,你赢了!你很牛逼!
0

369666951

赞同来自:

  1. nice


0

自己定义

赞同来自:

请教一个问题:vi编辑器,有什么简便的方法把这么长的代码,一下全部选中,复制粘贴?
0

杭州小白

赞同来自:

自己定义 发表于 2015-12-17 17:47
请教一个问题:vi编辑器,有什么简便的方法把这么长的代码,一下全部选中,复制粘贴?

额 我是cat出来然后复制粘贴的
0

杭州小白

赞同来自:

maria 发表于 2015-12-17 17:28
好吧,你赢了!你很牛逼!

牛逼的是铭哥 几个小时就完成这么个脚本,而我只是改改几个参数链接{:4_99:}
0

杭州小白

赞同来自:

需要注意的是要确认主机是否安装了mysql,Nginx,httpd和php,如果安装了需要卸载掉
0

阿铭 管理员

赞同来自:

我突然想到,我们应该搞到github去,这样大家都可以维护。
0

杭州小白

赞同来自:

阿铭 发表于 2015-12-17 21:27
我突然想到,我们应该搞到github去,这样大家都可以维护。

那个我还不知道怎么用,本来想改下链接的{:4_107:}
0

阿铭 管理员

赞同来自:

0

jinm

赞同来自:

本帖最后由 jinm 于 2015-12-19 18:59 编辑

经测试,成功安装。你们好强!
0

默写

赞同来自:

感谢分享~~

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: