1. 程式人生 > >LNMP架構六(Nginx安裝與部署)

LNMP架構六(Nginx安裝與部署)

六、Nginx的安裝與部署

1、安裝工具包 wget、vim和gcc

yum install -y wget  
yum install -y vim-enhanced  
yum install -y make cmake gcc gcc-c++  

2、下載nginx安裝包

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

3、安裝依賴包

yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

4、解壓nginx-1.6.2.tar.gz

tar -zxvf nginx-1.6.2.tar.gz

5、編譯安裝

./configure --prefix=/usr/local/nginx

make && make install

6、啟動Nginx

/usr/local/nginx/sbin/nginx

 ps -ef  |grep nginx

如果要關閉nginx,我們可以使用如下命令:

# /usr/local/nginx/sbin/nginx -s stop

如果想要重新熱啟動nginx

,則使用如下命令:

 # /usr/local/nginx/sbin/nginx -s reload

---------------------------------------------------------------------------------------------------------

新增Nginx 到 service 啟動

cd /etc/init.d/

vi  nginx

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: 2345 30 30
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

#nginx程式路徑
nginxd=/usr/local/nginx/sbin/nginx

#nginx配置檔案路徑
nginx_config=/usr/local/nginx/conf/nginx.conf

#nginx pid檔案的路徑,可以在nginx的配置檔案中找到
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL


 
chmod 755 /etc/init.d/nginx 
chkconfig --add nginx 

nginx啟動、停止、無間斷服務重啟 
  # service nginx start 
  # service nginx stop 
   # service nginx reload

---------------------------------------------------------------------------------------------------------