centos7环境下nodejs环境的部署过程

1.yum安装nodejs环境
curl -sL https://rpm.nodesource.com/setup_12.x | bash -

yum install -y nodejs

2.安装pm2插件
npm install pm2 -g

3.发布脚本
$ cat /usr/local/worksh/jenkins_rsync/hw_yt_rsync_www.chinasoft.com.templates.sh
#!/bin/bash

##############公司jenkins拉取git代码同步到 publish 上,然后触发 publish 上的该脚本同步到web服务器####################

# 非apache用户运行脚本,则退出
if [ `whoami` != "apache" ];then
echo " only apache user can run me"
exit 1
fi

## 定义排除的目录,线上一般排除logs文件夹
passwd="/data/www/.rsync/rsyncd.pass.www.chinasoft.com"

exclude_list="--exclude=logs/ --exclude=.git --exclude=.gitignore --exclude=.gitattributes --exclude=*.log --exclude=.svn --exclude=.env --exclude=node_modules"

# 发布单台机器 (vg-chinasoft-web02)
aws_ip_list_web01="1.1.1.2"

# 发布单台机器 (vg-chinasoft-web03)
#aws_ip_list_web02="1.1.1.3"


function func_rsync_status()
{
        if [[ $? == 0 || $? == 23 ]];then
                rsync_edit=1
        else
                rsync_edit=0
                echo "`date` 同步到目标失败! " | tee ${pubMsgFile}
                exit 1
        fi
}

# 代码发布目录,一般为/data/www/vhosts/${project_dir}
project_dir="www.chinasoft.com/httpdocs/templates"

# 判断代码发布目录变量是否为空
if [ ! $project_dir ]; then
   echo "$project_dir IS NULL"
   exit 1
fi


# 同步aws第1台web机器
function rsync_aws_web1()
{

for ip in ${aws_ip_list_web01}
do
        echo "#################### Run rsync ${ip}  start ################################"
        rsync -zavP $exclude_list --password-file=$passwd /data/www/vhosts/${project_dir}/ apache@${ip}::apache/data/www/vhosts/${project_dir}/
        func_rsync_status
    ssh -p 5202 apache@"$ip" "/bin/bash /usr/local/worksh/node_chinasoft_temp_manager.sh restart"
        echo "################### Run rsync ${ip} end #######################"
done
}


# 同步aws的第2台web机器
function rsync_aws_web2()
{

for ip in ${aws_ip_list_web02}
do
        echo "#################### Run rsync ${ip}  start ################################"
        rsync -zavP $exclude_list --password-file=$passwd /data/www/vhosts/${project_dir}/ apache@${ip}::apache/data/www/vhosts/${project_dir}/
        func_rsync_status
    ssh -p 5202 apache@"$ip" "/bin/bash /usr/local/worksh/node_chinasoft_temp_manager.sh restart"
        echo "################### Run rsync ${ip} end #######################"
done
}


# 同步所有机房
function rsync_aws_all()
{
        rsync_aws_web1
}


#####################  MAIN  ###############################
usage () {
        echo ""
        echo "  Please Input server infomation!"
        echo ""
        echo "  USAGE: `basename $0` [all|yt-account_web01|yt-account_web02]"
        echo ""
}
        
if [ $# != 1 ]
then
        usage >&2
        exit 1
fi
OPT=$1
case $OPT in
all)
        echo "start rsync ${project_dir}  servers"
        rsync_aws_all
        echo "rsync ${project_dir} to Done"
        ;;

yt-account_web01)
        echo "start rsync ${project_dir}  to ${aws_ip_list_web01} servers"
        rsync_aws_web1
        ;;

*)
        echo "Usage:`basename $0` [all|yt-account_web01|yt-account_web02]"
        ;;
esac

# 进程管理脚本
# $ cat /usr/local/worksh/node_chinasoft_temp_manager.sh
#!/bin/bash
#
source /etc/profile

# 根据参数,执行进程的启动 停止 重启等

# 非apache用户运行脚本,则退出
if [ `whoami` != "apache" ];then
echo " only apache can run me"
exit 1
fi

export NODE_ENV=production


node_process=‘chinasoft_templates‘

##############node_chinasoft_templastes###############
#1.启动 node_chinasoft_templastes
start_node_chinasoft_templastes() {
    #pid=`ps -ef |grep $node_process |grep -v grep |awk ‘{print $2}‘`
    pid=`pm2 status|grep chinasoft_templates|grep enabled|wc -l`
    if [[ ${pid} -lt 1 ]];then
        echo "starting node_chinasoft_templastes process is $node_process;pid is $pid "
        if [ $? -ne 0 ]; then
            echo
            exit 1
        fi
        cd /data/www/vhosts/www.chinasoft.com/httpdocs/templates && npm install request && npm install && npm install --dependencies
        cd /data/www/vhosts/www.chinasoft.com/httpdocs/templates && pm2 start npm --watch --name chinasoft_templates -- run start
        if [ $? == ‘0‘ ];then
            echo "start node_chinasoft_templastes $node_process ok"
        else
            echo "start node_chinasoft_templastes $node_process failed"
        fi
    else
        echo "node_chinasoft_templastes $node_process is still running!"
        exit
    fi
}

#2.停止 node_chinasoft_templastes
stop_node_chinasoft_templastes() {
    echo -n $"Stopping node_chinasoft_templastes $node_process: "
    #pid=`ps -ef |grep $node_process |grep -v grep |awk ‘{print $2}‘`
    #if [ ! "$pid" ];then
    pid=`pm2 status|grep chinasoft_templates|grep enabled|wc -l`
    if [[ ${pid} -lt 1 ]];then
        echo "node_chinasoft_templastes $node_process is not running"
    else
        cd /data/www/vhosts/www.chinasoft.com/httpdocs/templates && pm2 stop chinasoft_templates
        echo "stop node_chinasoft_templastes $node_process ok killed $pid"
    fi
}

#3.重启 restart_node_chinasoft_templastes
restart_node_chinasoft_templastes() {
    stop_node_chinasoft_templastes
    start_node_chinasoft_templastes
}

#4.查看 node_chinasoft_templastes 状态
status_node_chinasoft_templastes(){
    #pid=`ps -ef |grep $node_process |grep -v grep |awk ‘{print $2}‘`
    #if [ ! "$pid" ];then
    pid=`pm2 status|grep chinasoft_templates|grep enabled|wc -l`
    if [[ ${pid} -lt 1 ]];then
        echo "node_chinasoft_templastes $node_process is not running"
    else
        echo "node_chinasoft_templastes $node_process is running"
    fi
}

#####################  MAIN  ###############################
usage () {
        echo ""
        echo "  Please Input server infomation!"
        echo ""
        echo "  USAGE: `basename $0` [start|stop|restart|status]"
        echo ""
}
    

if [ $# != 1 ]
then
        usage >&2
        exit 1
fi
OPT=$1
case $OPT in
 
start)
        echo "start `basename $0`"
        start_node_chinasoft_templastes
    ;;
stop)
        stop_node_chinasoft_templastes
    ;;
restart)
        restart_node_chinasoft_templastes
    ;;
status)
        status_node_chinasoft_templastes
    ;;
*)
    echo "Usage:`basename $0`  [start|stop|restart|status]"
    exit 1
esac

# 进程状态
[apache@vg-chinasoft-web02:~/vhosts/www.chinasoft.com/httpdocs/templates]$ pm2 status
┌─────┬───────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name                  │ namespace   │ version │ mode    │ pid      │ uptime │ ?    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ chinasoft_templates    │ default     │ N/A     │ fork    │ 10655    │ 1s     │ 5    │ online    │ 0%       │ 51.0mb   │ apache   │ enabled  │
└─────┴───────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘


服务启动了,但是端口没有监听上,即如下程序没有启动

# ps -ef|grep node
apache   29956 29943  0 Oct08 ?        00:00:49 node /data/www/vhosts/www.chinasoft.com/httpdocs/templates/node_modules/.bin/nuxt start


su apache
cd /data/www/vhosts/www.chinasoft.com/httpdocs/templates && npm install request && npm install && npm install --dependencies
# 测试,出现如下界面代表ok,如果不行,就删除 /data/www/vhosts/www.chinasoft.com/httpdocs/templates/node_modules ,重新执行安装依赖的操作  cd /data/www/vhosts/www.chinasoft.com/httpdocs/templates && npm install request && npm install && npm install --dependencies

[apache@vg-chinasoft-web02:~/vhosts/www.chinasoft.com/httpdocs/templates]$ node /data/www/vhosts/www.chinasoft.com/httpdocs/templates/node_modules/.bin/nuxt start

 WARN  mode option is deprecated. You can safely remove it from nuxt.config                                                                                                                                                                        03:39:49


 WARN  No .env file found in /data/www/vhosts/www.chinasoft.com/httpdocs/templates.                                                                                                                                                     nuxt:dotenv 03:39:49


? NuxtJS collects completely anonymous data about usage.                                                                                                                                                                                           03:39:49
  This will help us improving Nuxt developer experience over the time.
  Read more on https://git.io/nuxt-telemetry

? Are you interested in participation? Yes


   ╭────────────────────────────────────────────────────╮
   │                                                    │
   │   Nuxt.js @ v2.14.5                                │
   │                                                    │
   │   ? Environment: production                        │
   │   ? Rendering:   server-side                       │
   │   ? Target:      server                            │
   │                                                    │
   │   Memory usage: 28 MB (RSS: 99.5 MB)               │
   │                                                    │
   │   Listening: http://172.0.0.3:2233/templates/   │
   │                                                    │
   ╰────────────────────────────────────────────────────╯


cd /data/www/vhosts/www.chinasoft.com/httpdocs/templates && pm2 start npm --watch --name chinasoft_templates -- run start


# 直到出现一下进程
[apache@vg-chinasoft-web02:~/vhosts/www.chinasoft.com/httpdocs/templates]$ ps -ef|grep node
apache   13255 13242 32 03:40 ?        00:00:00 node /data/www/vhosts/www.chinasoft.com/httpdocs/templates/node_modules/.bin/nuxt start

centos7环境下nodejs环境的部署过程

上一篇:Netty实现dubbo的原理


下一篇:JS 全屏和退出全屏详解及实例代码