【前端】CentOS 7 系列教程之五: 安装最新版 nginx 并转发 node 服务

转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_5.html

 

进入/usr/local目录

cd /usr/local

下载最新版的ngxin压缩包

wget http://nginx.org/download/nginx-1.13.9.tar.gz

解压

tar -zxvf nginx-1.13.9.tar.gz

删除压缩包

rm -f nginx-1.13.9.tar.gz

进入nginx目录准备安装

cd nginx-1.13.9

使用默认配置

./configure

安装

make && make install

删除安装文件,节约空间

rm -rf /usr/local/nginx-1.13.9

添加环境变量

vim /etc/profile

在最后添加

export PATH=$PATH:/usr/local/nginx/sbin

使配置生效

source /etc/profile

添加nginx服务,首先新增启动文件

vim /etc/init.d/nginx

内容如下

#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server

# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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/local/nginx/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

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

    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 755 /etc/init.d/nginx
chkconfig --add /etc/init.d/nginx

运行nginx和nginx服务

nginx
service nginx start

修改nginx,用它来代理转发node

vim /usr/local/nginx/conf/nginx.conf

修改location的默认路径转发到3000端口

location / {
    proxy_pass http://127.0.0.1:3000;
}

让nginx重新加载配置

nginx -s reload

 

设置重启后自动以git用户启动test项目

首先新建启动文件

vim /etc/init.d/test_autorun

内容如下

#!/bin/sh
# chkconfig: 2345 80 90
# description: text_autorun
# 以git用户启动服务
su - git -c "pm2 start /srv/www/test/index.js -i 1 --name \"test-production\""

然后设置它为可执行文件,并设置开机自动启动

chmod 755 /etc/init.d/test_autorun
chkconfig --add /etc/init.d/test_autorun

 

好了,现在nginx已经配置完成了。

posted @ 2018-03-18 22:33  静茹♂鱼  阅读(649)  评论(0编辑  收藏  举报