现在的位置: 首页 > Linux > Web技术 > 正文
nginx安装
2014年01月07日 Web技术 ⁄ 共 4828字 nginx安装已关闭评论 ⁄ 被围观 10,074 views+

1、安装原版Nginx

1.1 使用yum安装:

编辑仓库:

vi  /etc/yum.repos.d/nginx.repo

如果是CentOS,则内容如下:

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

如果是RHEL,在内容如下:

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/

gpgcheck=0

enabled=1

1.2 编译安装:

下载地址: http://nginx.org/download/nginx-1.4.4.tar.gz

1.2.1 安装准备:

解决依赖:

编译安装nginx需要事先需要安装开发包组"Development Tools"和 "Development Libraries"。同时,还需要专门安装pcre-devel包:

[root@web2 ~]# yum -y install pcre-devel

准备账户:

首先添加用户nginx,实现以之运行nginx服务进程:

[root@web2 ~]# groupadd -r nginx

[root@web2 ~]# useradd -r -g nginx nginx

1.2.2 使用默认编译参数编译安装:

[root@web2 nginx-1.4.4]# ./configure

[root@web2 nginx-1.4.4]# make

[root@web2 nginx-1.4.4]# make install

1.2.3 高级编译参数:

[root@web2 nginx-1.4.4]#  ./configure \

  --prefix=/usr/local/nginx \    ##安装路径

  --sbin-path=/usr/local/nginx/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/nginx.pid  \  ##pid路径

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

  --user=nginx \   ##运行账户

  --group=nginx \  ##运行组

  --with-http_ssl_module \  ##启用ssl模块

  --with-http_stub_status_module \   ##启用服务器状态页模块

  --with-http_gzip_static_module \  ##启用gzip压缩模块

  --http-client-body-temp-path=/var/tmp/nginx/client/ \   ##设置客户端请求主体的临时文件路径

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

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

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

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

  --without-http_upstream_ip_hash_module \  ##负载均衡中启用基于IP哈希的调度模块,不做负载均衡无需启用

  --without-http_upstream_least_conn_module \  ##负载均衡中启用最少连接调度模块,不做负载均衡无需启用

  --without-http_upstream_keepalive_module \  ##负载均衡启用keepalive调度模块,不做负载均衡无需启用

  --with-pcre  ##使用 PCRE 库

注意:最新版1.5.7的nginx还加入了对后端服务器的健康检查和基于cookie的调度。

[root@web2 nginx-1.4.4]# make

[root@web2 nginx-1.4.4]# make install

1.3 安装后的配置

新建服务脚本文件/etc/rc.d/init.d/nginx,内容如下:

#!/bin/sh

#

# 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

# config:      /etc/sysconfig/nginx

# pidfile:     /var/run/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/subsys/nginx

make_dirs() {

   # make required directories

   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

   options=`$nginx -V 2>&1 | grep 'configure arguments:'`

   for opt in $options; do

       if [ `echo $opt | grep '.*-temp-path'` ]; then

           value=`echo $opt | cut -d "=" -f 2`

           if [ ! -d "$value" ]; then

               # echo "creating" $value

               mkdir -p $value && chown -R $user $value

           fi

       fi

   done

}

start() {

    [ -x $nginx ] || exit 5

    [ -f $NGINX_CONF_FILE ] || exit 6

    make_dirs

    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

而后为此脚本赋予执行权限:

# chmod +x /etc/rc.d/init.d/nginx

添加至服务管理列表,并让其开机自动启动:

# chkconfig --add nginx

# chkconfig nginx on

而后就可以启动服务并测试了:

# service nginx start

2 Tengine编译安装

大部分的选项跟Nginx是兼容的。下面列出的都是Tengine特有的选项。如果你想查看Tengine支持的所有选项,你可以运行'./configure --help'命令来获取帮助。

--dso-path

设置DSO模块的安装路径。

--dso-tool-path

设置dso_tool脚本本身的安装路径。

--without-dso

关闭动态加载模块的功能。

--with-jemalloc

让Tengine链接jemalloc库,运行时用jemalloc来分配和释放内存。

--with-jemalloc=path

设置jemalloc库的源代码路径,Tengine可以静态编译和链接该库。

--with-http_upstream_check_module=shared

启用负载均衡健康检查模块。

--with-http_upstream_session_sticky_module=shared

启用基于cookie的会话保持方式调度模块。

--with-http_upstream_consistent_hash_module=shared

启用一致性哈希调度方式模块。

make的目标选项

大部分的目标选项跟Nginx是兼容的。下面列出的是Tengine特有的选项。

make test

运行Tengine的测试用例。你首先需要安装perl来运行这个指令。

make dso_install

将动态模块的so文件拷贝到目标目录。这个目录可以通过'--dso-path'设置。默认是在Tengine安装目录下面的modules目录。

抱歉!评论已关闭.

×