手机版
你好,游客 登录 注册
背景:
阅读新闻

Nginx 高级扩展实例

[日期:2012-08-09] 来源:51cto  作者:peaceweb [字体:大 中 小]

一、首先安装Nginx:

  1. # yum -y install pcre-devel  
  2.  
  3. # groupadd -r nginx  
  4. # useradd -r -g nginx -s /sbin/nologin -M nginx  
  5.  
  6. # tar xf nginx-1.2.3.tar.gz   
  7. # cd nginx-1.2.3  
  8. # ./configure \  
  9.   --prefix=/usr \  
  10.   --sbin-path=/usr/sbin/nginx \  
  11.   --conf-path=/etc/nginx/nginx.conf \  
  12.   --error-log-path=/var/log/nginx/error.log \  
  13.   --http-log-path=/var/log/nginx/access.log \  
  14.   --pid-path=/var/run/nginx/nginx.pid  \  
  15.   --lock-path=/var/lock/nginx.lock \  
  16.   --user=nginx \  
  17.   --group=nginx \  
  18.   --with-http_ssl_module \  
  19.   --with-http_flv_module \  
  20.   --with-http_stub_status_module \  
  21.   --with-http_gzip_static_module \  
  22.   --http-client-body-temp-path=/var/tmp/nginx/client/ \  
  23.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \  
  24.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \  
  25.   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \  
  26.   --http-scgi-temp-path=/var/tmp/nginx/scgi \  
  27.   --with-pcre  
  28.  
  29. # make && make install 

新建文件/etc/rc.d/init.d/nginx,内容如下:

  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. # config:      /etc/nginx/nginx.conf  
  10. # config:      /etc/sysconfig/nginx  
  11. # pidfile:     /var/run/nginx.pid  
  12.    
  13. # Source function library.  
  14. . /etc/rc.d/init.d/functions  
  15.    
  16. # Source networking configuration.  
  17. . /etc/sysconfig/network  
  18.    
  19. # Check that networking is up.  
  20. [ "$NETWORKING" = "no" ] && exit 0  
  21.    
  22. nginx="/usr/sbin/nginx" 
  23. prog=$(basename $nginx)  
  24.    
  25. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  26.    
  27. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  28.    
  29. lockfile=/var/lock/subsys/nginx  
  30.    
  31. make_dirs() {  
  32.    # make required directories  
  33.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`  
  34.    options=`$nginx -V 2>&1 | grep 'configure arguments:'`  
  35.    for opt in $options; do  
  36.        if [ `echo $opt | grep '.*-temp-path'` ]; then  
  37.            value=`echo $opt | cut -d "=" -f 2`  
  38.            if [ ! -d "$value" ]; then  
  39.                # echo "creating" $value  
  40.                mkdir -p $value && chown -R $user $value  
  41.            fi  
  42.        fi  
  43.    done  
  44. }  
  45.    
  46. start() {  
  47.     [ -x $nginx ] || exit 5  
  48.     [ -f $NGINX_CONF_FILE ] || exit 6  
  49.     make_dirs  
  50.     echo -n $"Starting $prog: "  
  51.     daemon $nginx -c $NGINX_CONF_FILE  
  52.     retval=$?  
  53.     echo  
  54.     [ $retval -eq 0 ] && touch $lockfile  
  55.     return $retval  
  56. }  
  57.    
  58. stop() {  
  59.     echo -n $"Stopping $prog: "  
  60.     killproc $prog -QUIT  
  61.     retval=$?  
  62.     echo  
  63.     [ $retval -eq 0 ] && rm -f $lockfile  
  64.     return $retval  
  65. }  
  66.    
  67. restart() {  
  68.     configtest || return $?  
  69.     stop  
  70.     sleep 1  
  71.     start  
  72. }  
  73.    
  74. reload() {  
  75.     configtest || return $?  
  76.     echo -n $"Reloading $prog: "  
  77.     killproc $nginx -HUP  
  78.     RETVAL=$?  
  79.     echo  
  80. }  
  81.    
  82. force_reload() {  
  83.     restart  
  84. }  
  85.    
  86. configtest() {  
  87.   $nginx -t -c $NGINX_CONF_FILE  
  88. }  
  89.    
  90. rh_status() {  
  91.     status $prog  
  92. }  
  93.    
  94. rh_status_q() {  
  95.     rh_status >/dev/null 2>&1  
  96. }  
  97.    
  98. case "$1" in  
  99.     start)  
  100.         rh_status_q && exit 0  
  101.         $1  
  102.         ;;  
  103.     stop)  
  104.         rh_status_q || exit 0  
  105.         $1  
  106.         ;;  
  107.     restart|configtest)  
  108.         $1  
  109.         ;;  
  110.     reload)  
  111.         rh_status_q || exit 7  
  112.         $1  
  113.         ;;  
  114.     force-reload)  
  115.         force_reload  
  116.         ;;  
  117.     status)  
  118.         rh_status  
  119.         ;;  
  120.     condrestart|try-restart)  
  121.         rh_status_q || exit 0  
  122.             ;;  
  123.     *)  
  124.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"  
  125.         exit 2  
  126. esac 

而后为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/nginx

添加至服务管理列表,并让其开机自动启动:
# chkconfig --add nginx
# chkconfig nginx on

而后就可以启动服务并测试了:
# service nginx start

linux
相关资讯       Nginx  Nginx实例 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款