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

来源:互联网 发布:php高性能框架 编辑:程序博客网 时间:2024/05/17 18:48

nginx wiki 中文站:http://wiki.nginx.org/Chs

添加用户和组

    • 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  
  5. cd ../ 

vim /etc/init.d/nginx       将下面的代码复制进去保存!

小插曲VI 小技巧

vi一般用法
一般模式              编辑模式                  指令模式
h 左               a,i,r,o,A,I,R,O             :w 保存
j 下                进入编辑模式                :w! 强制保存
k 上                dd 删除光标当前行           :q! 不保存离开
l 右                ndd 删除n行                 :wq! 保存后离开
0 移动到行首        yy 复制当前行                :e! 还原原始档
$ 移动到行尾        nyy 复制n行                  :w filename 另存为
H 屏幕最上          p,P 粘贴                     :set nu 设置行号
M 屏幕中央          u 撤消                      :set nonu 取消行号
L 屏幕最下          [Ctrl]+r 重做上一个动作       ZZ 保存离开
G 档案最后一行      [ctrl]+z 暂停退出            :set nohlsearch   永久地关闭高亮显示
/work 向下搜索                                   :sp 同时打开两个文档
?work 向上搜索                                   [Ctrl]+w 两个文档设换
gg 移动到档案第一行                              :nohlsearch    暂时关闭高亮显示

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

0 0
原创粉丝点击