centos6.5编译安装nginx

系统64位 centos6.5 

nginx官网:http://nginx.org/

下载nginx源码包:

wget  http://nginx.org/download/nginx-1.6.2.tar.gz   稳定版

tar zxvf nginx-1.6.2.tar.gz ;解压
cd nginx-1.6.2 ;进入源码目录

配置编译参数:

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=www

编译和安装:

make && make install

添加启动脚本:vim /etc/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    if [ -z "`grep $user /etc/passwd`" ]; then
 35        useradd -M -s /bin/nologin $user
 36    fi
 37    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
 38    for opt in $options; do
 39        if [ `echo $opt | grep '.*-temp-path'` ]; then
 40            value=`echo $opt | cut -d "=" -f 2`
 41            if [ ! -d "$value" ]; then
 42                # echo "creating" $value
 43                mkdir -p $value && chown -R $user $value
 44            fi
 45        fi
 46    done
 47 }
 48  
 49 start() {
 50     [ -x $nginx ] || exit 5
 51     [ -f $NGINX_CONF_FILE ] || exit 6
 52     make_dirs
 53     echo -n $"Starting $prog: "
 54     daemon $nginx -c $NGINX_CONF_FILE
 55     retval=$?
 56     echo
 57     [ $retval -eq 0 ] && touch $lockfile
 58     return $retval
 59 }
 60  
 61 stop() {
 62     echo -n $"Stopping $prog: "
 63     killproc $prog -QUIT
 64     retval=$?
 65     echo
 66     [ $retval -eq 0 ] && rm -f $lockfile
 67     return $retval
 68 }
 69  
 70 restart() {
 71     configtest || return $?
 72     stop
 73     sleep 1
 74     start
 75 }
 76  
 77 reload() {
 78     configtest || return $?
 79     echo -n $"Reloading $prog: "
 80     killproc $nginx -HUP
 81     RETVAL=$?
 82     echo
 83 }
 84  
 85 force_reload() {
 86     restart
 87 }
 88  
 89 configtest() {
 90   $nginx -t -c $NGINX_CONF_FILE
 91 }
 92  
 93 rh_status() {
 94     status $prog
 95 }
 96  
 97 rh_status_q() {
 98     rh_status >/dev/null 2>&1
 99 }
100  
101 case "$1" in
102     start)
103         rh_status_q && exit 0
104         $1
105         ;;
106     stop)
107         rh_status_q || exit 0
108         $1
109         ;;
110     restart|configtest)
111         $1
112         ;;
113     reload)
114         rh_status_q || exit 7
115         $1
116         ;;
117     force-reload)
118         force_reload
119         ;;
120     status)
121         rh_status
122         ;;
123     condrestart|try-restart)
124         rh_status_q || exit 0
125             ;;
126     *)
127         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
128         exit 2
129 esac

:wq

chmod +x /etc/init.d/nginx ;给于可执行权行
chkconfig --add nginx

启动服务器: /usr/local/nginx/sbin/nginx
重启服务器: /usr/local/nginx/sbin/nginx -s reload

检查nginx配置文件是否正确:
/usr/nginx/sbin/nginx -t 或 nginx -t -c /usr/nginx/conf/nginx.conf

 

posted @ 2014-11-19 19:36  i文学  Views(246)  Comments(0Edit  收藏  举报