[关闭]
@Great-Chinese 2017-03-18T11:23:02.000000Z 字数 19055 阅读 917

Shell编程实战

Shell编程实战


1.1 监控思路、架构介绍

程序架构:

                    (主目录 mon)
   ____________________|_______________________________
  |          |           |             |               |
  bin        conf        shares        mail           log
  |          |           |             |               |
[main.sh][mon.conf][load.sh 502.sh][mail.php mail.sh][mon.log err.log]

bin下是主程序
conf下是配置文件
shares下是各个监控脚本
mail下是邮件引擎
log下是日志

  1. cd /usr/local/sbin/ # 进入sbin目录下
  2. mkdir mon # 创建mon目录
  3. cd mon/ # 进入mon目录下
  4. mkdir bin conf mail shares log # 创建这5个目录
  5. cd bin/ # 进入bin目录下
  6. http://ask.apelearn.com/question/8106 # 告警系统,文档参考地址

A:示例脚本 main.sh

  1. # 编辑主程序
  2. vim main.sh # 增加内容如下
  3. #!/bin/bash
  4. #Written by Melody.
  5. # 是否发送邮件的开关
  6. export send=1
  7. # 过滤ip地址
  8. export addr=`/sbin/ifconfig |grep -A1 'eth0' |grep addr: |awk '{print $2}'|awk -F: '{print $2}'`
  9. dir=`pwd`
  10. # 只需要最后一级目录名
  11. last_dir=`echo $dir|awk -F'/' '{print $NF}'`
  12. # 下面的判断目的是,保证执行脚本的时候,我们在bin目录里,不然监控脚本、邮件和日志很有可能找不到
  13. if [ $last_dir == "bin" ]; then
  14. conf_file="../conf/mon.conf"
  15. else
  16. echo "you shoud cd bin dir"
  17. exit
  18. fi
  19. exec 1>>../log/mon.log 2>>../log/err.log
  20. echo "`date +"%F %T"` load average"
  21. /bin/bash ../shares/load.sh
  22. #先检查配置文件中是否需要监控502
  23. if grep -q 'to_mon_502=1' $conf_file; then
  24. export log=`grep 'logfile=' $conf_file |awk -F '=' '{print $2}' |sed 's/ //g'`
  25. /bin/bash ../shares/502.sh
  26. fi

B:配置文件脚本 mon.conf

  1. # 编辑配置文件
  2. vim ../conf/mon.conf # 增加内容如下
  3. ## to config the options if to monitor
  4. ## cdb 主要定义mysql的服务器地址、端口以及user、password
  5. to_mon_cdb=0 ##0 or 1, default 0,0 not monitor, 1 monitor
  6. cdb_ip=10.20.3.13
  7. cdb_port=3315
  8. cdb_user=username
  9. cdb_pass=passwd
  10. ## httpd 如果是1则监控,为0不监控
  11. to_mon_httpd=0
  12. httpd_url=http://192.168.1.111:45/
  13. ## php 如果是1则监控,为0不监控
  14. to_mon_php_socket=0
  15. ## http_code_502 需要定义访问日志的路径
  16. to_mon_502=1
  17. logfile=/usr/local/nginx/logs/error.log
  18. ## request_count 定义日志路径以及域名
  19. to_mon_request_count=0
  20. req_log=/usr/local/nginx/logs/access.log
  21. domainname=www.discuz.net

1.3 监控子脚本load.sh/502.sh讲解

  1. # 编辑子脚本load.sh
  2. vim ../shares/load.sh # 增加内容如下
  3. #! /bin/bash
  4. ##Writen by aming##
  5. load=`uptime |awk -F 'average:' '{print $2}'|cut -d',' -f1|sed 's/ //g' |cut -d. -f1`
  6. if [ $load -gt 20 ] &&; [ $send -eq "1" ]
  7. then
  8. echo "$addr `date +%T` load is $load" >../log/load.tmp
  9. /bin/bash ../mail/mail.sh $addr\_load $load ../log/load.tmp
  10. fi
  11. echo "`date +%T` load is $load"
  1. # 编辑502.sh
  2. vim ../shares/502.sh # 增加内容如下
  3. #! /bin/bash
  4. d=`date -d "-1 min" +%H:%M`
  5. c_502=`grep :$d: $log |grep ' 502 '|wc -l`
  6. if [ $c_502 -gt 10 ] && [ $send == 1 ]; then
  7. echo "$addr $d 502 count is $c_502">../log/502.tmp
  8. /bin/bash ../mail/mail.sh $addr\_502 $c_502 ../log/502.tmp
  9. fi
  10. echo "`date +%T` 502 $c_502"

1.4 邮件脚本讲解

A:mail.php

  1. # 编辑mail.php
  2. vim ../mail/mail.php # 增加内容如下
  3. <?php
  4. class Smtp
  5. {
  6. /* Public Variables */
  7. var $smtp_port;
  8. var $time_out;
  9. var $host_name;
  10. var $log_file;
  11. var $relay_host;
  12. var $debug;
  13. var $auth;
  14. var $user;
  15. var $pass;
  16. /* Private Variables */
  17. var $sock;
  18. /* Constractor */
  19. function Smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
  20. {
  21. $this->debug = FALSE;
  22. $this->smtp_port = $smtp_port;
  23. $this->relay_host = $relay_host;
  24. $this->time_out = 30; //is used in fsockopen()
  25. #
  26. $this->auth = $auth;//auth
  27. $this->user = $user;
  28. $this->pass = $pass;
  29. #
  30. $this->host_name = "localhost"; //is used in HELO command
  31. $this->log_file = "";
  32. $this->sock = FALSE;
  33. }
  34. /* Main Function */
  35. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  36. {
  37. $mail_from = $this->get_address($this->strip_comment($from));
  38. $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
  39. $header = "MIME-Version:1.0\r\n";
  40. if($mailtype=="HTML"){
  41. $header .= "Content-Type:text/html\r\n";
  42. }
  43. $header .= "To: ".$to."\r\n";
  44. if ($cc != "") {
  45. $header .= "Cc: ".$cc."\r\n";
  46. }
  47. $header .= "From: $from<".$from.">\r\n";
  48. $header .= "Subject: ".$subject."\r\n";
  49. $header .= $additional_headers;
  50. $header .= "Date: ".date("r")."\r\n";
  51. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
  52. list($msec, $sec) = explode(" ", microtime());
  53. $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
  54. $TO = explode(",", $this->strip_comment($to));
  55. if ($cc != "") {
  56. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  57. }
  58. if ($bcc != "") {
  59. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  60. }
  61. $sent = TRUE;
  62. foreach ($TO as $rcpt_to) {
  63. $rcpt_to = $this->get_address($rcpt_to);
  64. if (!$this->smtp_sockopen($rcpt_to)) {
  65. $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
  66. $sent = FALSE;
  67. continue;
  68. }
  69. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  70. $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
  71. } else {
  72. $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
  73. $sent = FALSE;
  74. }
  75. fclose($this->sock);
  76. $this->log_write("Disconnected from remote host\n");
  77. }
  78. return $sent;
  79. }
  80. /* Private Functions */
  81. function smtp_send($helo, $from, $to, $header, $body = "")
  82. {
  83. if (!$this->smtp_putcmd("HELO", $helo)) {
  84. return $this->smtp_error("sending HELO command");
  85. }
  86. #auth
  87. if($this->auth){
  88. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  89. return $this->smtp_error("sending HELO command");
  90. }
  91. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  92. return $this->smtp_error("sending HELO command");
  93. }
  94. }
  95. #
  96. if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
  97. return $this->smtp_error("sending MAIL FROM command");
  98. }
  99. if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
  100. return $this->smtp_error("sending RCPT TO command");
  101. }
  102. if (!$this->smtp_putcmd("DATA")) {
  103. return $this->smtp_error("sending DATA command");
  104. }
  105. if (!$this->smtp_message($header, $body)) {
  106. return $this->smtp_error("sending message");
  107. }
  108. if (!$this->smtp_eom()) {
  109. return $this->smtp_error("sending . [EOM]");
  110. }
  111. if (!$this->smtp_putcmd("QUIT")) {
  112. return $this->smtp_error("sending QUIT command");
  113. }
  114. return TRUE;
  115. }
  116. function smtp_sockopen($address)
  117. {
  118. if ($this->relay_host == "") {
  119. return $this->smtp_sockopen_mx($address);
  120. } else {
  121. return $this->smtp_sockopen_relay();
  122. }
  123. }
  124. function smtp_sockopen_relay()
  125. {
  126. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
  127. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  128. if (!($this->sock && $this->smtp_ok())) {
  129. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
  130. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  131. return FALSE;
  132. }
  133. $this->log_write("Connected to relay host ".$this->relay_host."\n");
  134. return TRUE;
  135. }
  136. function smtp_sockopen_mx($address)
  137. {
  138. $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
  139. if (!@getmxrr($domain, $MXHOSTS)) {
  140. $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
  141. return FALSE;
  142. }
  143. foreach ($MXHOSTS as $host) {
  144. $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
  145. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  146. if (!($this->sock && $this->smtp_ok())) {
  147. $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
  148. $this->log_write("Error: ".$errstr." (".$errno.")\n");
  149. continue;
  150. }
  151. $this->log_write("Connected to mx host ".$host."\n");
  152. return TRUE;
  153. }
  154. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
  155. return FALSE;
  156. }
  157. function smtp_message($header, $body)
  158. {
  159. fputs($this->sock, $header."\r\n".$body);
  160. $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
  161. return TRUE;
  162. }
  163. function smtp_eom()
  164. {
  165. fputs($this->sock, "\r\n.\r\n");
  166. $this->smtp_debug(". [EOM]\n");
  167. return $this->smtp_ok();
  168. }
  169. function smtp_ok()
  170. {
  171. $response = str_replace("\r\n", "", fgets($this->sock, 512));
  172. $this->smtp_debug($response."\n");
  173. if (!ereg("^[23]", $response)) {
  174. fputs($this->sock, "QUIT\r\n");
  175. fgets($this->sock, 512);
  176. $this->log_write("Error: Remote host returned \"".$response."\"\n");
  177. return FALSE;
  178. }
  179. return TRUE;
  180. }
  181. function smtp_putcmd($cmd, $arg = "")
  182. {
  183. if ($arg != "") {
  184. if($cmd=="") $cmd = $arg;
  185. else $cmd = $cmd." ".$arg;
  186. }
  187. fputs($this->sock, $cmd."\r\n");
  188. $this->smtp_debug("> ".$cmd."\n");
  189. return $this->smtp_ok();
  190. }
  191. function smtp_error($string)
  192. {
  193. $this->log_write("Error: Error occurred while ".$string.".\n");
  194. return FALSE;
  195. }
  196. function log_write($message)
  197. {
  198. $this->smtp_debug($message);
  199. if ($this->log_file == "") {
  200. return TRUE;
  201. }
  202. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  203. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  204. $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
  205. return FALSE;;
  206. }
  207. flock($fp, LOCK_EX);
  208. fputs($fp, $message);
  209. fclose($fp);
  210. return TRUE;
  211. }
  212. function strip_comment($address)
  213. {
  214. $comment = "\([^()]*\)";
  215. while (ereg($comment, $address)) {
  216. $address = ereg_replace($comment, "", $address);
  217. }
  218. return $address;
  219. }
  220. function get_address($address)
  221. {
  222. $address = ereg_replace("([ \t\r\n])+", "", $address);
  223. $address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
  224. return $address;
  225. }
  226. function smtp_debug($message)
  227. {
  228. if ($this->debug) {
  229. echo $message;
  230. }
  231. }
  232. }
  233. $file = $argv[2];
  234. $smtpserver = "smtp.qq.com";//SMTP服务器
  235. $smtpserverport = "25";//SMTP服务器端口
  236. $smtpusermail = "244048927@qq.com";//SMTP服务器的用户邮箱
  237. $smtpemailto = "834865081@139.com";//发送给谁
  238. $smtpuser = "244048927";//SMTP服务器的用户帐号
  239. $smtppass = "123456";//SMTP服务器的用户密码
  240. $mailsubject = $argv[1];//邮件主题
  241. $mailbody = file_get_contents($file);//邮件内容
  242. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  243. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  244. //$smtp->debug = TRUE;//是否显示发送的调试信息
  245. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  246. ?>

B:mail.sh

  1. vim ../mail/mail.sh # 增加内容如下
  2. #!/bin/bash
  3. log=$1
  4. t_s=`date +%s`
  5. #!/bin/bash
  6. log=$1
  7. t_s=`date +%s`
  8. t_s2=`date -d "2 hours ago" +%s`
  9. if [ ! -f /tmp/$log ]
  10. then
  11. echo $t_s2 > /tmp/$log
  12. fi
  13. t_s2=`tail -1 /tmp/$log|awk '{print $1}'`
  14. echo $t_s>>/tmp/$log
  15. v=$[$t_s-$t_s2]
  16. echo $v
  17. if [ $v -gt 3600 ]
  18. then
  19. /dir/to/php ../mail/mail.php "$1 $2" "$3"
  20. echo "0" > /tmp/$log.txt
  21. else
  22. if [ ! -f /tmp/$log.txt ]
  23. then
  24. echo "0" > /tmp/$log.txt
  25. fi
  26. nu=`cat /tmp/$log.txt`
  27. nu2=$[$nu+1]
  28. echo $nu2>/tmp/$log.txt
  29. if [ $nu2 -gt 10 ]
  30. then
  31. /dir/to/php ../mail/mail.php "trouble continue 10 min $1 $2 " "$3"
  32. echo "0" > /tmp/$log.txt
  33. fi
  34. fi

2.4 lanmp脚本思路

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

2.5 脚本局部解析-安装mysql

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注