Linux配置Nginx,MySql,php-fpm开机启动的方法

一. Nginx 开机启动

NGINX SHELL脚本   放到/etc/init.d/下取名nginx

下面代码里根据你原始安装路径去更改

nginx="/usr/localinx/sbininx" 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[plain] view plain copy
 
  1. #!/bin/sh   
  2. #   
  3. # nginx - this script starts and stops the nginx daemon   
  4. #   
  5. # chkconfig: - 85 15   
  6. # description: Nginx is an HTTP(S) server, HTTP(S) reverse   
  7. # proxy and IMAP/POP3 proxy server   
  8. # processname: nginx   
  9. # chkconfig: 2345 90 91<span style="white-space:pre">   </span>  
  10. # description: nginx web server  
  11. # processname: nginx  
  12. # config: /opt/nginx/conf/nginx.conf  
  13. # pidfile: /opt/nginx/nginx.pid  
  14.   
  15.   
  16. # Source function library.  
  17. . /etc/init.d/functions  
  18.   
  19.   
  20. # Source networking configuration.  
  21. . /etc/sysconfig/network  
  22.   
  23.   
  24.   
  25.   
  26. if [ -f /etc/sysconfig/nginx ];then  
  27. . /etc/sysconfig/nginx  
  28. fi  
  29.   
  30.   
  31. # Check that networking is up.   
  32. [ "$NETWORKING" = "no" ] && exit 0  
  33.   
  34.   
  35. nginx="/usr/local/nginx/sbin/nginx"   
  36. prog=$(basename $nginx)  
  37.   
  38.   
  39. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"  
  40.   
  41.   
  42. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  43.   
  44.   
  45. lockfile=/var/lock/subsys/nginx  
  46.   
  47.   
  48. start() {   
  49. [ -x $nginx ] || exit 5   
  50. [ -f $NGINX_CONF_FILE ] || exit 6   
  51. echo -n $"Starting $prog: "   
  52. daemon $nginx #-c $NGINX_CONF_FILE   
  53. retval=$?   
  54. echo   
  55. [ $retval -eq 0 ] && touch $lockfile   
  56. return $retval   
  57. }  
  58.   
  59.   
  60. stop() {   
  61. echo -n $"Stopping $prog: "   
  62. killproc $prog -QUIT   
  63. retval=$?   
  64. echo   
  65. [ $retval -eq 0 ] && rm -f $lockfile   
  66. return $retval   
  67. killall -9 nginx   
  68. }  
  69.   
  70.   
  71. restart() {   
  72. configtest || return $?   
  73. stop   
  74. sleep 1   
  75. start   
  76. }  
  77.   
  78.   
  79. reload() {   
  80. configtest || return $?   
  81. echo -n $"Reloading $prog: "   
  82. killproc $nginx -HUP   
  83. RETVAL=$?   
  84. echo   
  85. }  
  86.   
  87.   
  88. force_reload() {   
  89. restart   
  90. }  
  91.   
  92.   
  93. configtest() {   
  94. $nginx -t #-c $NGINX_CONF_FILE   
  95. }  
  96.   
  97.   
  98. rh_status() {   
  99. status $prog   
  100. }  
  101.   
  102.   
  103. rh_status_q() {   
  104. rh_status >/dev/null 2>&1   
  105. }  
  106.   
  107.   
  108. case "$1" in   
  109. start)   
  110. <span style="white-space:pre">  </span>rh_status_q && exit 0   
  111. <span style="white-space:pre">  </span>$1   
  112. <span style="white-space:pre">  </span>;;   
  113. stop)   
  114. rh_status_q || exit 0   
  115. <span style="white-space:pre">  </span>$1   
  116. <span style="white-space:pre">  </span>;;   
  117. restart)   
  118. <span style="white-space:pre">  </span>$1   
  119. <span style="white-space:pre">  </span>;;   
  120. test)   
  121. <span style="white-space:pre">  </span>configtest   
  122. <span style="white-space:pre">  </span>;;   
  123. reload)   
  124. <span style="white-space:pre">  </span>rh_status_q || exit 7   
  125. <span style="white-space:pre">  </span>$1   
  126. <span style="white-space:pre">  </span>;;   
  127. force-reload)   
  128. <span style="white-space:pre">  </span>force_reload   
  129. <span style="white-space:pre">  </span>;;   
  130. status)   
  131. <span style="white-space:pre">  </span>rh_status   
  132. <span style="white-space:pre">  </span>;;   
  133. condrestart|try-restart)   
  134. <span style="white-space:pre">  </span>rh_status_q || exit 0   
  135. <span style="white-space:pre">  </span>;;   
  136. *)   
  137. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|test}"   
  138. exit 2   
  139. esac  


更改脚本权限  chmod 775 /etc/init.d/nginx

二. MySQL开机启动
将mysql安装目录下 support-files目录下的mysql.server文件拷贝到/etc/init.d/目录下并改名为mysqld,并更改权限

chmod 775 /etc/init.d/mysqld

三. PHP开机启动

  PHP-FPM SHELL脚本  放到/etc/init.d/下 取名php-fpm,

  1. php_command=/usr/local/php/sbin/php-fom  
  2. php_config=/usr/local/php/etc/php-fpm.conf  

根据你的安装路径去改

[plain] view plain copy
 
  1. #!/bin/bash  
  2. # php-fpm startup script for the php-fpm   
  3. # php-fpm version:5.5.0-alpha6  
  4. # chkconfig: - 85 15  
  5. # description: php-fpm is very good  
  6. # processname: php-fpm  
  7. # pidfile: /var/run/php-fpm.pid  
  8. # config: /usr/local/php/etc/php-fpm.conf  
  9.     
  10. php_command=/usr/local/php/sbin/php-fom  
  11. php_config=/usr/local/php/etc/php-fpm.conf  
  12. php_pid=/usr/local/php/var/run/php-fpm.pid  
  13. RETVAL=0  
  14. prog="php-fpm"  
  15.     
  16. #start function  
  17. php_fpm_start() {  
  18.     /usr/local/php/sbin/php-fpm  
  19. }  
  20.     
  21. start(){  
  22.     if [ -e $php_pid  ]  
  23.     then  
  24.     echo "php-fpm already start..."  
  25.     exit 1  
  26.     fi  
  27.     php_fpm_start  
  28. }  
  29.     
  30. stop(){  
  31.     if [ -e $php_pid ]  
  32.     then  
  33.     parent_pid=`cat $php_pid`  
  34.     all_pid=`ps -ef | grep php-fpm | awk '{if('$parent_pid' == $3){print $2}}'`  
  35.     for pid in $all_pid  
  36.     do  
  37.             kill $pid  
  38.         done  
  39.         kill $parent_pid  
  40.     fi  
  41.     exit 1  
  42. }  
  43.     
  44. restart(){  
  45.     stop  
  46.     start  
  47. }  
  48.     
  49. # See how we were called.  
  50. case "$1" in  
  51. start)  
  52.         start  
  53.         ;;  
  54. stop)  
  55.         stop  
  56.         ;;  
  57. restart)  
  58.         stop  
  59.         start  
  60.         ;;  
  61. status)  
  62.         status $prog  
  63.         RETVAL=$?  
  64.         ;;  
  65. *)  
  66.         echo $"Usage: $prog {start|stop|restart|status}"  
  67.         exit 1  
  68. esac  
  69. exit $RETVAL  

使用chkconfig进行管理

[plain] view plain copy
 
  1. chkconfig --add /etc/init.d/nginx  
  2. chkconfig --add /etc/init.d/mysqld  
  3. chkconfig --add /etc/init.d/php -fpm  

设置终端模式开机启动:

[plain] view plain copy
 
    1. chkconfig php-fpm on  
    2. chkconfig nginx on  
    3. chkconfig mysqld on  
原文地址:https://www.cnblogs.com/da-guang/p/6936942.html