一、编译安装

1、使用yum安装所需的包,虽然需要编译几个依赖包,pcre、zlib、openssl

yum -y groupinstall "Development Tools""Server Platform Development"

yum install pcre-devel zlib-devel openssl-devel gcc* (一般系统装好了,以下的这几个包openssl、zlib、pcre都已经安装上了,注意: pcre-devel zlib-devel openssl-devel这几个devel包要装上,不然编译的时候会报错)


2、添加用户和组

groupadd -r nginx

useradd -r -g nginx nginx

 

3、解压安装文件

tar -zxvf nginx-1.10.1.tar.gz

cd nginx-1.10.1


4、配置选项

./configure --prefix=/usr/local/nginx #nginx家目录

--sbin-path=/usr/sbin #nginx主程序路径

--conf-path=/etc/nginx/nginx.conf #nginx主配置文件路径

--error-log-path=/var/log/nginx/error.log #错误日志路径

--http-log-path=/var/log/nginx/access.log #访问日志路径

--pid-path=/var/run/nginx/nginx.pid #进程ID路径

--lock-path=/var/lock/nginx.lock #nginx锁文件路径

--user=nginx #worker进程所属的用户名称

--group=nginx #worker进程所属的组名称

--with-http_ssl_module #启用http_ssl模块

--with-http_flv_module #启用服务端伪流媒体模块

--with-http_stub_status_module #启用健康检查模块

--with-http_gzip_static_module #启用ngnix 的静态缓存模块

--http-client-body-temp-path=/var/tmp/nginx/client #启用为http连接的请求实体临时文件设置路径

--http-proxy-temp-path=/var/tmp/nginx/proxy #为http代理临时文件设置路径

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi #为http fastcgi临时文件设置路径

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #为http uwcgi临时文件设置路径

--http-scgi-temp-path=/var/tmp/nginx/scgi  #为http scgi临时文件设置路径

--with-pcre #使用pcre库文件

--with-stream #启用TCP/UDP代理模块



运行配置检查:

Configuration summary

  + using system PCRE library

  + using system OpenSSL library

  + md5: using OpenSSL library

  + sha1: using OpenSSL library

  + using system zlib library

 

  nginx path prefix: "/usr/local/nginx"

  nginx binary file: "/usr/sbin/nginx"

  nginx modules path: "/usr/local/modules"

  nginx configuration prefix: "/etc/nginx"

  nginx configuration file: "/etc/nginx/nginx.conf"

  nginx pid file: "/var/run/nginx/nginx.pid"

  nginx error log file: "/var/log/nginx/error.log"

  nginx http access log file: "/var/log/nginx/access.log"

  nginx http client request body temporary files: "/var/tmp/nginx/client/"

  nginx http proxy temporary files: "/var/tmp/nginx/proxy/"

  nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"

  nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"

  nginx http scgi temporary files: "/var/tmp/nginx/scgi

 

5、编译和安装

make && make install


6、在/var/tmp目录下建立nginx目录

mkdir -p /var/tmp/nginx


7、检查配置文件是否有语法错误

[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


8、编辑服务启动脚本

vim /etc/init.d/nginx

#! /bin/bash

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig:   - 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

#                          proxy and IMAP/POP3 proxy server

#

# processname: nginx

# config:         /etc/nginx/nginx.conf

# pidfile:       /var/run/nginx/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/nginx.lock

start() {

  [ -x $nginx ] || exit 5

  [ -f $NGINX_CONF_FILE ] || exit 6

  echo -n $"Starting $prog: "

  daemon $nginx -c $NGINX_CONF_FILE

  retval=$?

  echo

  [ $retval -eq 0 ] && touch $lockfile

  return $retval

}

stop() {

  echo -n $"Stopping $prog: "

  killproc $prog -QUIT

  retval=$?

  echo

  [ $retval -eq 0 ] && rm -f $lockfile

  return $retval

}

restart() {

  configtest || return $?

  stop

  sleep 1

  start

}

reload() {

  configtest || return $?

  echo -n $"Reloading $prog: "

  killproc $nginx -HUP

  RETVAL=$?

  echo

}

force_reload() {

  restart

}

configtest() {

  $nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

  status $prog

}

rh_status_q() {

  rh_status >/dev/null 2>&1

}

case "$1" in

  start)

    rh_status_q && exit 0

    $1

    ;;

  stop)

    rh_status_q || exit 0

    $1

    ;;

  restart|configtest)

    $1

    ;;

  reload)

    rh_status_q || exit 7

    $1

    ;;

  force-reload)

    force_reload

    ;;

  status)

    rh_status

    ;;

  condrestart|try-restart)

    rh_status_q || exit 0

      ;;

  *)

    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

    exit 2

esac


9、启动nginx

[root@localhost init.d]# service nginx start
正在启动 nginx:                                           [确定]

10、停止nginx

[root@localhost init.d]# service nginx stop
停止 nginx:                                               [确定]



11、查看进程情况

[root@localhost init.d]# ps -ef|grep nginx
root      10342      1  0 02:04 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     10344  10342  0 02:04 ?        00:00:00 nginx: worker process                   
root      10350   1613  0 02:07 pts/0    00:00:00 grep nginx

12、查看监听的端口

[root@localhost init.d]# netstat -tnlp|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      10342/nginx


13、查看测试页

wKiom1gu07XAJvUFAAC1oYZcZtU172.png-wh_50