简介

能看着这个文章的,估计大家对nginx都有所了解,网上文章太多太多,我们不在扯淡了,直接说安装,后面会有多个示例来告诉大家配置相关服务,因nginx插件多,我不能一一说明。重要的是大家有个处理问题和解决问题的思路。

安装

依赖

  • Pcre

  • Openssl-devel

 

Nginx提供rpm dep包,这两种方式比较但是下面我们说明的使用源代码编译安装的方式。

准备

添加用户

Shell>useradd–r nginx

Shell>groupadd nginx(可选执行)

验证

Shell >id  nginx

出现如下提示,标识用户创建成功

uid=500(nginx) gid=500(nginx) group)s=500(nginx)

创建文件夹

Shell>mkdir/var/cache/nginx

获取安装包,我们这里使用nginx-1.8的稳定版本

Shell># wget

Shell># wgethttps://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

安装patch程序

Shell># yum–y install patch

解压缩nginx软件包

Shell># tarxf nginx-1.8.0.tar.gz

解压缩补丁包

Shell># unzipnginx_upstream_check_module-master.zip

打补丁

Shell># mvnginx_upstream_check_module-master patch

Shell># cdnginx-1.8.0

Shell># patch-p1 < ../patch/check_1.7.5+.patch

编译软件, 编译nginx,参数如下(按照rpm包的习惯编译,编译会因需求不同而改变。下面的仅供参考)

./configure \

--prefix=/etc/nginx \

--sbin-path=/usr/sbin/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--pid-path=/var/run/nginx.pid \

--lock-path=/var/run/nginx.lock \

--http-client-body-temp-path=/var/cache/nginx/client_temp \

--http-proxy-temp-path=/var/cache/nginx/proxy_temp \

--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \

--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \

--http-scgi-temp-path=/var/cache/nginx/scgi_temp \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_realip_module \

--with-http_gunzip_module \

--with-http_gzip_static_module \

--with-http_stub_status_module \

--with-file-aio \

--with-http_spdy_module

--add-module=/root/patch/

 

 

安装

Shell >make&& make install

 

创建开机自动启动脚本

RHEL6

命令

Shell># vi /etc/init.d/nginx

内容

#!/bin/sh

#

# nginx        Startup script for nginx

#

# chkconfig: - 85 15

# processname: nginx

# config: /etc/nginx/nginx.conf

# config: /etc/sysconfig/nginx

# pidfile: /var/run/nginx.pid

# description: nginx is an HTTP and  reverse proxy server

#

### BEGIN INIT INFO

# Provides: nginx

# Required-Start: $local_fs $remote_fs  $network

# Required-Stop: $local_fs $remote_fs  $network

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: start and stop  nginx

### END INIT INFO

 

# Source function library.

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

 

if [ -L $0 ]; then

     initscript=`/bin/readlink -f $0`

else

     initscript=$0

fi

 

sysconfig=`/bin/basename $initscript`

 

if [ -f /etc/sysconfig/$sysconfig ];  then

     . /etc/sysconfig/$sysconfig

fi

 

nginx=${NGINX-/usr/sbin/nginx}

prog=`/bin/basename $nginx`

conffile=${CONFFILE-/etc/nginx/nginx.conf}

lockfile=${LOCKFILE-/var/lock/subsys/nginx}

pidfile=${PIDFILE-/var/run/nginx.pid}

SLEEPMSEC=${SLEEPMSEC-200000}

UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}

RETVAL=0

 

start() {

     echo -n $"Starting $prog: "

 

     daemon --pidfile=${pidfile} ${nginx} -c ${conffile}

     RETVAL=$?

     echo

     [ $RETVAL = 0 ] && touch ${lockfile}

     return $RETVAL

}

 

stop() {

     echo -n $"Stopping $prog: "

     killproc -p ${pidfile} ${prog}

     RETVAL=$?

     echo

     [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}

 

reload() {

     echo -n $"Reloading $prog: "

     killproc -p ${pidfile} ${prog} -HUP

     RETVAL=$?

     echo

}

 

upgrade() {

     oldbinpidfile=${pidfile}.oldbin

 

     configtest -q || return

     echo -n $"Starting new master $prog: "

     killproc -p ${pidfile} ${prog} -USR2

     echo

 

     for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do

         /bin/usleep $SLEEPMSEC

         if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then

            echo -n $"Graceful shutdown  of old $prog: "

            killproc -p ${oldbinpidfile}  ${prog} -QUIT

            RETVAL=$?

            echo

            return

         fi

     done

 

     echo $"Upgrade failed!"

     RETVAL=1

}

 

configtest() {

     if [ "$#" -ne 0 ] ;  then

         case "$1" in

            -q)

                FLAG=$1

                ;;

            *)

                ;;

         esac

         shift

     fi

     ${nginx} -t -c ${conffile} $FLAG

     RETVAL=$?

     return $RETVAL

}

 

rh_status() {

     status -p ${pidfile} ${nginx}

}

 

# See how we were called.

case "$1" in

     start)

         rh_status >/dev/null 2>&1 && exit 0

         start

         ;;

     stop)

         stop

         ;;

     status)

         rh_status

         RETVAL=$?

         ;;

     restart)

         configtest -q || exit $RETVAL

         stop

         start

         ;;

     upgrade)

         rh_status >/dev/null 2>&1 || exit 0

         upgrade

         ;;

     condrestart|try-restart)

         if rh_status >/dev/null 2>&1; then

            stop

            start

         fi

         ;;

     force-reload|reload)

         reload

         ;;

     configtest)

         configtest

         ;;

     *)

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

         RETVAL=2

esac

 

exit $RETVAL

RHEL7

命令

Shell># vi /lib/systemd/system/nginx.service

文本

[Unit]

Description=nginx - high performance  web server

Documentation=http://nginx.org/en/docs/

After=network.target remote-fs.target  nss-lookup.target

 

[Service]

Type=forking

PIDFile=/run/nginx.pid

ExecStartPre=/usr/sbin/nginx -t -c  /etc/nginx/nginx.conf

ExecStart=/usr/sbin/nginx -c  /etc/nginx/nginx.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

 

[Install]

WantedBy=multi-user.target

启动nginx

RHEL6

Shell># service nginx start

RHEL7

Shell># systemctl start  nginx.service

设置开机启动

RHEL6

Shell># chkconfig –level 3 nginx on

RHEL7

Shell># systemctl enable nginx

 

验证

Shell># netstat –antp | grep nginx

tcp        0       0 0.0.0.0:80                   0.0.0.0:*                    LISTEN      3818/nginx