编译安装Nginx:

1.安装支持软件:

[root@server ~]#yum –y install pcre-devel zlib-devel

2.创建运行用户、组:

Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限,增加灵活性、降低安全风险

[root@server ~]#useradd –M –s /sbin/nologin nginx

3.编译安装nginx

[root@server src]#tar zxf nginx-1.0.8.tar.gz

[root@server ~]#cd nginx-1.0.8

[root@server nginx-1.0.8]#./configure --prefix=/usr/local/nginx --user=nginx  ---group=nginx --with-http_stub_status_module

注:--with-http_stub_status_module:启用http_stub_status_module模块以支持状态统计

[root@server nginx-1.0.8]#make && make install

[root@server nginx-1.0.8]#ln –s /usr/local/nginx/sbin/nginx /usr/local/sbin/


Nginx的运行控制:

[root@server ~]#nginx –t              //检查配置文件

1.启动Nginx

[root@server ~]#nginx

2.停止Nginx

[root@server ~]#killall –s QUIT nginx

“-c  配置文件路径选项来指定路径。

需要注意的是:若服务器中已安装有httpd等其他WEB服务软件,应事先停止服务。

通过web页面访问http://server,页面默认显示”welcome to nginx!”表示编译安装成功。

注意:要在防火墙上允许80端口的通信。

3.为了使Nginx服务的启动、停止、重载等操作更加方便,可以编写Nginx服务脚本,并使用chkconfigservice工具来进行管理,也更加符合linux系统的管理习惯。

[root@server ~]#vim /etc/init.d/nginx

#!/bin/bash

# chkconfig: 2345 99 20

# description: Nginx Service Control Script

PROG="/usr/local/nginx/sbin/nginx"

PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in

 start)

  $PROG -t &> /dev/null

  if [ $? -eq 0 ] ; then

   $PROG

   echo "Nginx service start success."

  else

   $PROG -t

  fi

   ;;

 stop)

   kill -s QUIT $(cat $PIDF)

   echo "Nginx service stop success."

   ;;

 restart)

   $0 stop

   $0 start

   ;;

 reload)

  $PROG -t &> /dev/null

  if [ $? -eq 0 ] ; then

   kill -s HUP $(cat $PIDF)

   echo "reload Nginx config success."

  else

   $PROG -t

  fi

   ;;

 *)

  echo "Usage: $0 {start|stop|restart|reload}"

  exit 1

esac

注:通过killkillall命令发送HUP信号表示重载配置, 用新的配置开始新的工作进程
关闭旧的工作进程。QUIT信号表示退出进程,KILL信号表示杀死进程。通过”-s”选项指定信号种类。

添加为系统服务:

[root@server ~]#chmod +x /etc/init.d/nginx

[root@server ~]#chkconfig –add nginx