Nginx 添加到 service 启动 (完整篇),


添加用户和组

  • groupadd www  
  • useradd -g www -M www 
  •  

1.安装nginx所需的pcre库

  1. tar zxvf pcre-7.8.tar.gz  
  2. cd pcre-7.8/  
  3. ./configure  
  4. make && make install  
  5. cd ../ 

2、安装Nginx


  1. tar zxvf nginx-1.0.4.tar.gz  
  2. cd nginx-1.0.4/  
  3. ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  
  4. make && make install  

  1. cd ../ 

 [root@localhost /]# vi /etc/init.d/nginx       将下面的代码复制进去保存!


  1. #!/bin/bash  
  2. # nginx Startup script for the Nginx HTTP Server  
  3. #  
  4. # chkconfig: - 85 15  
  5. # description: Nginx is a high-performance web and proxy server.  
  6. # It has a lot of features, but it's not for everyone.  
  7. # processname: nginx  
  8. # pidfile: /var/run/nginx.pid  
  9. # config: /usr/local/nginx/conf/nginx.conf 
  10. ########### need to modify  the path ###################
  11. nginxd=/usr/local/nginx/sbin/nginx  
  12. nginx_config=/usr/local/nginx/conf/nginx.conf  
  13. nginx_pid=/usr/local/nginx/nginx.pid  
  14. # # ################################

  15. RETVAL=0  
  16. prog="nginx" 
  17.  
  18. # Source function library.  
  19. . /etc/rc.d/init.d/functions  
  20.  
  21. # Source networking configuration.  
  22. . /etc/sysconfig/network  
  23.  
  24. Check that networking is up.  
  25. [ ${NETWORKING} = "no" ] && exit 0  
  26.  
  27. [ -x $nginxd ] || exit 0  
  28.  
  29.  
  30. # Start nginx daemons functions.  
  31. start() {  
  32.  
  33. if [ -e $nginx_pid ];then 
  34.    echo "nginx already running...." 
  35.    exit 1  
  36. fi  
  37.  
  38.    echo -n $"Starting $prog: " 
  39.    daemon $nginxd -c ${nginx_config}  
  40.    RETVAL=$?  
  41.    echo  
  42.    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx  
  43.    return $RETVAL  
  44.  
  45. }  
  46.  
  47.  
  48. # Stop nginx daemons functions.  
  49. stop() {  
  50.         echo -n $"Stopping $prog: " 
  51.         killproc $nginxd  
  52.         RETVAL=$?  
  53.         echo  
  54.         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid  
  55. }  
  56.  
  57.  
  58. # reload nginx service functions.  
  59. reload() {  
  60.  
  61.     echo -n $"Reloading $prog: " 
  62.  $nginxd -s reload  
  63.     #if your nginx version is below 0.8, please use this command: "kill -HUP `cat ${nginx_pid}`" 
  64.     RETVAL=$?  
  65.     echo  
  66.  
  67. }  
  68.  
  69. # See how we were called.  
  70. case "$1" in 
  71. start)  
  72.         start  
  73.         ;;  
  74.  
  75. stop)  
  76.         stop  
  77.         ;;  
  78.  
  79. reload)  
  80.         reload  
  81.         ;;  
  82.  
  83. restart)  
  84.         stop  
  85.         start  
  86.         ;;  
  87.  
  88. status)  
  89.         status $prog  
  90.         RETVAL=$?  
  91.         ;;  
  92. *)  
  93.         echo $"Usage: $prog {start|stop|restart|reload|status|help}" 
  94.         exit 1  
  95. esac  
  96.  
  97. exit $RETVAL 

 [root@localhost /]# cd /etc/rc.d/init.d

[root@localhost init.d]#  chmod +x nginx

 [root@localhost init.d]# /sbin/chkconfig --level 345 nginx on

任何位置都能运行   service nginx start           可选  start | stop | restart | reload | status |  help


相关内容

    暂无相关文章