Nginx 安装启动

  选择Nginx的理由

  高并发连接:

  官方测试Nginx能够支撑5万并发连接,在实际生产环境中可支撑2--4万并发连接数。这得益于Nginx使用了最新的epool(Linux2.6内核)和kqueue(freebsd)网络I/O模型,而Apache使用的则是传统的select模型

   内存消耗少:

   开启10个Nginx进程消耗150MB内存(15MB X 10=150MB)

2.CentOS安装Nginx服务器

  推荐直接从源码编译安装

  安装GCC编译器及相关工具:

  GCC全称为GNU Compiler Collection,是GNU社区推出的功能强大、性能优越的用于编程开发的自由编译器,是GNU的代表作品之一,目前可以编译的语言包括:C、C++、Objective-C、Fortran、Java等。必须确保您的操作系统安装有GCC编译器,另外,还必须安装Autoconf和Automake工具,它们用于自动创建功能完善的Makefile。可以使用yum命令安装GCC编译器及相关工具:

  yum -y install gcc gcc-c++ autoconf automake

  模块依赖性:Nginx的一些模块需要其他第三方库的支持,例如gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl库等。

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


  Nginx的安装:

  tar -zvxf nginx-0.x.xx.tar.gz

  cd nginx-0.x.xx

  ./configure

  make

  sudo make install

  按照以上命令,Nginx将被默认安装到/usr/local/nginx目录下。

  可以通过./configure --help命令查看Nginx可选择的编译选项

  --prefix=<path>      Nginx安装路径。如果没有指定,默认为/usr/local/nginx

  --sbin-path=<path>   Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为/usr/local/nginx/sbin/nginx

  --conf-path=<path>   在没有给定-c选项下默认的nginx.cong的路径。如果没有指定,默认为/usr/local/nginx/cong/nginx.conf

  --pid-path=<path>    在nginx.conf中没有指定pid指令的情况下,默认的Nginx.pid的路径。如果没有指定,默认为/usr/local/nginx/logs/nginx.pid

  --lock-path=<path>   nginx.lock文件的路径

  --error-log-path=<path>  在nginx.conf中没有指定error_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为/usr/local/nginx/logs/access.log

  --user=<user>   在nginx.conf中没有指定user指令的情况下,默认的Nginx使用的用户。如果没有指定,默认为nobody

  --group=<group>  在nginx.conf中没有指定user指令的情况下,默认的Nginx使用的组。如果没有指定,默认为nobody

  --builddir=DIR   指定编译的目录

  --with-rtsig_module  启用rtsig模块

  --with-select_module(--without-select_module)  允许或不允许开启SELECT模式

  ........


Nginx的启动、停止、平滑重启:


     Nginx的启动:

     /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.cong

     参数“-c”指定了配置文件的路径,如果不加“-c”参数,Nginx会默认加载其安装目录的conf子目录中的nginx.conf文件。

     Nginx的停止:

     Nginx的停止方法有很多种,一般通过发送系统信号给Nginx主进程的方式来停止Nginx。

     我们通过ps命令来查找Nginx的主进程号:

     ps -ef | grep nginx

    


   从上图中可以看到,1个Nginx进程的备注信息为“master procss”,表示他为主进程,另外的1个进程的备注信息为“worker process”,表示他为子进程。1158位主进程号。

   如果在nginx.conf配置文件中指定了pid文件存放的路径,该文件中存放的就是Nginx当前的主进程号。如果没有指定pid文件存放路径,nginx.pid文件默认存放在Nginx安装目录的logs目录下。所以,我们可以直接通过以下命令来完成平滑重启,省下寻找Nginx主进程号的步骤:

   kill - 信号类型 '/usr/local/nginx/logs/nginx.pid'

  (1)从容停止Nginx

   kill - QUIT Nginx主进程号
   或
   kill - QUIT '/usr/local/nginx/logs/nginx.pid'

  (2)快速停止Nginx

   kill - TERM Nginx主进程号
  
   kill - TERM '/usr/local/nginx/logs/nginx.pid'
   或
   kill - INT Nginx主进程号
  
   kill - INT '/usr/local/nginx/logs/nginx.pid'

  (3)强制停止所有Nginx进程

   pkill -9 nginx

   Starting, Stopping, and Reloading Configuration

   To start nginx, run the executable file. Once nginx is started, it can be controlled by invoking the executable with the -s parameter. Use the following syntax:

   nginx -s signal
   Where signal may be one of the following:

   stop — fast shutdown
   quit — graceful shutdown
   reload — reloading the configuration file
   reopen — reopening the log files

   For example, to stop nginx processes with waiting for the worker processes to finish serving current requests, the following command can be executed:

   nginx -s quit

   This command should be executed under the same user that started nginx.
Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted. To reload configuration, execute:

    nginx -s reload

    Once the master process receives the signal to reload configuration, it checks the syntax validity of the new configuration file and tries to apply the configuration provided in it. If this is a success, the master process starts new worker processes and sends messages to old worker processes, requesting them to shut down. Otherwise, the master process rolls back the changes and continues to work with the old configuration. Old worker processes, receiving a command to shut down, stop accepting new connections and continue to service current requests until all such requests are serviced. After that, the old worker processes exit.

   A signal may also be sent to nginx processes with the help of Unix tools such as the kill utility. In this case a signal is sent directly to a process with a given process ID. The process ID of the nginx master process is written, by default, to the nginx.pid in the directory /usr/local/nginx/logs or /var/run. For example, if the master process ID is 1628, to send the QUIT signal resulting in nginx’s graceful shutdown, execute:

   kill -s QUIT 1628

   For getting the list of all running nginx processes, the ps utility may be used, for example, in the following way:

   ps -ax | grep nginx

  程序运行参数:

    Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。

    Nginx 的参数包括有如下几个:

    -c <path_to_config>:使用指定的配置文件而不是默认的/usr/local/nginx/conf/目录下的 nginx.conf 。

    -t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。

    -v:显示 nginx 版本号。

    -V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。

    例如我们要测试某个配置文件是否书写正确,我们可以使用以下命令:

    /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf


   Nginx的平滑重启:
  

   如果修改了Nginx的配置文件(nginx.conf),想重启Nginx,同样可以通过发送系统信号给Nginx主进程的方式类进行。不过,重启之前,要确认Nginx配置文件的语法是正确的,否则Nginx将不会加载新的配置文件。通过以下命令可以判断Nginx配置文件是否正确:

   /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

   如果配置文件不正确,屏幕将会提示配置文件的第几行出错

   如果配置文件正确,屏幕将提示以下两行信息:

   the configuration file /usr/local/nging/conf/nginx.conf syntax is ok

   configuration file /usr/local/nginx/conf/nginx.conf test is successful

   这时候,就可以平滑重启Nginx了:

   kill - HUP Nginx主进程号
  
   kill - HUP '/usr/local/nginx/logs/nginx.pid'

   当Nginx接收到HUP信号时,他会尝试先解析配置文件,如果成功,就应用新的配置文件。之后,Nginx运行新的工作进程并从容关闭旧的工作进程。通知工作进程关闭监听套接字,但是继续为当前连接的客户提供服务。所有客户端的服务完成后,旧的工作进程被关闭。如果新的配置文件应用失败,Nginx将继续使用旧的配置进行工作。


   Nginx的信号控制:


    TERM,INT   快速关闭

    QUTI       从容关闭

    HUP        平滑重启,重新加载配置文件

    USR1       重新打开日志文件,在切割日志时用途较大

    USR2       平滑升级可执行程序

    WINCH      从容关闭工作进程


    Nginx的平滑升级:


    当需要将正在运行中的Nginx升级、添加/删除服务器模块时,可以在不中断服务的情况下,使用新版本、重编译的Nginx可执行程序替换旧版本的可执行程序:

   (1)使用新的可执行程序替换旧的可执行程序,对于编译安装的Nginx,可以将新版本编译安装到旧版本的Nginx安装路径中。替换之前,最好备份一份旧的可执行文件。

   (2)发送以下指令

     kill -USR2 旧版本的Nginx进程号

    (3)旧版本Nginx的主进程将重命名他的.pid文件为.oldbin(例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新版本的Nginx可执行程序,依次启动新的主进程和新的工作进程。

    (4)此时,新、旧版本的Nginx实例会同时运行,共同处理输入的请求。要逐步停止旧版本的Nginx实例,你必须发送WINCH信号给旧的主进程,然后,它的工作进程就将开始从容关闭:

     kill -WINCH 旧版本的Nginx主进程号

    (5)一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求啦。


    (6)这时候,我们可以决定是使用新版本,还是恢复到旧版本:

     kill -HUP 旧的Nginx主进程号   : Nginx将在不重载配置文件的情况下启动他的工作进程

     kill -QUIT 新的主进程号       : 从容关闭其工作进程(worker process)

     kill -TERM 新的主进程号       : 强制退出

     新的主进程退出后,旧的主进程会移除.oldbin前缀,恢复为他的.pid文件,这样,一切就都恢复到升级之前了。如果尝试升级成功,而你也希望保留新的服务器时,可发送QUIT信号给旧的主进程,使其退出而只留下新的服务器运行。


      配置开机启动Nginx:

#cd /etc/rc.d/init.d
#touch nginx
#chmod +x nginx
#vi nginx

添加如下内容

#! /bin/sh
# chkconfig: - 85 15

PATH=/usr/appdata/nginx-1.8.0/sbin

DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/appdata/nginx-1.8.0/sbin/$NAME
CONFIGFILE=/usr/appdata/nginx-1.8.0/conf/$NAME.conf
PIDFILE=/usr/appdata/nginx-1.8.0/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}

do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}

do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac

exit 0

添加到开机启动服务

chkconfig --add nginx
chkconfig nginx on
chkconfig --list              #查看开机启动的服务


然后就可以使用
#service nginx start|stop|restart|reload|graceful
启动|停止|重启|重读配置|优雅关闭 啦



   

猜你喜欢

转载自maosheng.iteye.com/blog/2210471