安装Nginx

By | 2013年2月21日

yum安装nginx

Centos默认的yum源里没有nginx,需要手动添加源,有两种方法:

使用nginx提供的一个源设置安装包

nginx下载页面:http://nginx.org/en/download.html

nginx提供4个版本的系统的源:

我选择Centos6,文件名为:nginx-release-centos-6-0.el6.ngx.noarch.rpm。

在服务器上安装:

rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

之后便可以使用yum快速安装了:

yum install nginx

手动添加nginx源

cd /etc/yum.repos.d

vim nginx.repo

#内容为:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

baseurl中centos和6可以更改为自己的版本,比如redhat 5版本的链接为:

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

详情可以自己看nginx的下载页面,包括其他系统的源安装方法。

相关路径

nginx可执行文件:/usr/sbin/nginx

nginx配置目录:/etc/nginx

nginx log目录:/var/log/nginx

编译安装nginx

我的nginx编译安装代码:

yum install perl-ExtUtils-Embed.x86_64 pcre-devel.x86_64
#根据自己的需求,选择参数,可用的参数可以使用./configure --help命令查看
./configure --prefix=/opt/nginx
                 --with-http_gzip_static_module 
                 --with-http_perl_module
                 --with-pcre 
                 --with- rtsig_module 
                 --with-select_module 
                 --with-poll_module 
                 --with-file-aio

make
make install

为编译安装的nginx配置init脚本:

vim /etc/init.d/nginx
#内容取自yum安装的nginx后,生成的文件,改下里面的路径即可使用了

#!/bin/sh
#
# nginx        Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /opt/nginx/logs/nginx.pid
# description: nginx is a 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 [ -f /etc/sysconfig/nginx ]; then
    . /etc/sysconfig/nginx
fi

prog=nginx
nginx=${NGINX-/opt/nginx/sbin/nginx}
conffile=${CONFFILE-/opt/nginx/conf/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/opt/nginx/logs/nginx.pid}
SLEEPMSEC=100000
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 6
    echo -n $"Staring new master $prog: "
    killproc -p ${pidfile} ${prog} -USR2
    RETVAL=$?
    echo
    /bin/usleep $SLEEPMSEC
    if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
        echo -n $"Graceful shutdown of old $prog: "
        killproc -p ${oldbinpidfile} ${prog} -QUIT
        RETVAL=$?
        echo 
    else
        echo $"Upgrade failed!"
        return 1
    fi
}

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)
        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

使用chkconfig安装为安装启动项

chkconfig --add nginx
chkconfig nginx on

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!