1. 程式人生 > >Linux配置Nginx,MySql,php-fpm開機啟動的方法

Linux配置Nginx,MySql,php-fpm開機啟動的方法

sleep 管理 support dev view linux 使用 pre work

一. 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. # Source function library.
  15. . /etc/init.d/functions
  16. # Source networking configuration.
  17. . /etc/sysconfig/network
  18. if [ -f /etc/sysconfig/nginx ];then
  19. . /etc/sysconfig/nginx
  20. fi
  21. # Check that networking is up.
  22. [ "$NETWORKING" = "no" ] && exit 0
  23. nginx="/usr/local/nginx/sbin/nginx"
  24. prog=$(basename $nginx)
  25. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  26. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  27. lockfile=/var/lock/subsys/nginx
  28. start() {
  29. [ -x $nginx ] || exit 5
  30. [ -f $NGINX_CONF_FILE ] || exit 6
  31. echo -n $"Starting $prog: "
  32. daemon $nginx #-c $NGINX_CONF_FILE
  33. retval=$?
  34. echo
  35. [ $retval -eq 0 ] && touch $lockfile
  36. return $retval
  37. }
  38. stop() {
  39. echo -n $"Stopping $prog: "
  40. killproc $prog -QUIT
  41. retval=$?
  42. echo
  43. [ $retval -eq 0 ] && rm -f $lockfile
  44. return $retval
  45. killall -9 nginx
  46. }
  47. restart() {
  48. configtest || return $?
  49. stop
  50. sleep 1
  51. start
  52. }
  53. reload() {
  54. configtest || return $?
  55. echo -n $"Reloading $prog: "
  56. killproc $nginx -HUP
  57. RETVAL=$?
  58. echo
  59. }
  60. force_reload() {
  61. restart
  62. }
  63. configtest() {
  64. $nginx -t #-c $NGINX_CONF_FILE
  65. }
  66. rh_status() {
  67. status $prog
  68. }
  69. rh_status_q() {
  70. rh_status >/dev/null 2>&1
  71. }
  72. case "$1" in
  73. start)
  74. <span style="white-space:pre"> </span>rh_status_q && exit 0
  75. <span style="white-space:pre"> </span>$1
  76. <span style="white-space:pre"> </span>;;
  77. stop)
  78. rh_status_q || exit 0
  79. <span style="white-space:pre"> </span>$1
  80. <span style="white-space:pre"> </span>;;
  81. restart)
  82. <span style="white-space:pre"> </span>$1
  83. <span style="white-space:pre"> </span>;;
  84. test)
  85. <span style="white-space:pre"> </span>configtest
  86. <span style="white-space:pre"> </span>;;
  87. reload)
  88. <span style="white-space:pre"> </span>rh_status_q || exit 7
  89. <span style="white-space:pre"> </span>$1
  90. <span style="white-space:pre"> </span>;;
  91. force-reload)
  92. <span style="white-space:pre"> </span>force_reload
  93. <span style="white-space:pre"> </span>;;
  94. status)
  95. <span style="white-space:pre"> </span>rh_status
  96. <span style="white-space:pre"> </span>;;
  97. condrestart|try-restart)
  98. <span style="white-space:pre"> </span>rh_status_q || exit 0
  99. <span style="white-space:pre"> </span>;;
  100. *)
  101. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|test}"
  102. exit 2
  103. 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. php_command=/usr/local/php/sbin/php-fom
  10. php_config=/usr/local/php/etc/php-fpm.conf
  11. php_pid=/usr/local/php/var/run/php-fpm.pid
  12. RETVAL=0
  13. prog="php-fpm"
  14. #start function
  15. php_fpm_start() {
  16. /usr/local/php/sbin/php-fpm
  17. }
  18. start(){
  19. if [ -e $php_pid ]
  20. then
  21. echo "php-fpm already start..."
  22. exit 1
  23. fi
  24. php_fpm_start
  25. }
  26. stop(){
  27. if [ -e $php_pid ]
  28. then
  29. parent_pid=`cat $php_pid`
  30. all_pid=`ps -ef | grep php-fpm | awk ‘{if(‘$parent_pid‘ == $3){print $2}}‘`
  31. for pid in $all_pid
  32. do
  33. kill $pid
  34. done
  35. kill $parent_pid
  36. fi
  37. exit 1
  38. }
  39. restart(){
  40. stop
  41. start
  42. }
  43. # See how we were called.
  44. case "$1" in
  45. start)
  46. start
  47. ;;
  48. stop)
  49. stop
  50. ;;
  51. restart)
  52. stop
  53. start
  54. ;;
  55. status)
  56. status $prog
  57. RETVAL=$?
  58. ;;
  59. *)
  60. echo $"Usage: $prog {start|stop|restart|status}"
  61. exit 1
  62. esac
  63. 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

Linux配置Nginx,MySql,php-fpm開機啟動的方法