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

yum install -y nodejs

 

不同版本的安装方法参考:https://github.com/nodesource/distributions

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

ln -s /usr/local/node-v14.0.0/bin/pm2 /usr/bin/pm2

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

 

 

使用不同版本nodejs

安装不同版本的nodejs进行编译
cd /usr/local
tar xf node-v12.19.0-linux-x64.tar.gz
mv node-v12.19.0-linux-x64 node-v12.19.0

ln -s /usr/local/node-v12.19.0/bin/node /usr/bin/node12
ln -s /usr/local/node-v12.19.0/bin/npm /usr/bin/npm12



# 修改npm 的node环境,确保npm使用指定的node12版本(如果不指定会使用系统默认版本)
修改第一行
#!/usr/bin/env node12

[root@jenkins_server:~]# more /usr/local/node-v12.19.0/bin/npm 
#!/usr/bin/env node12
;(function () { // wrapper in case we're in module_context mode
  // windows: running "npm blah" in this folder will invoke WSH, not node.
  /* global WScript */
  if (typeof WScript !== 'undefined') {
    WScript.echo(
      'npm does not work when run\n' +
        'with the Windows Scripting Host\n\n' +
        "'cd' to a different directory,\n" +
        "or type 'npm.cmd <args>',\n" +
        "or type 'node npm <args>'."
    )
    WScript.quit(1)
    return
  }




# 安装指定版本Nodejs

wget https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.gz
tar xf node-v12.16.1-linux-x64.tar.gz
ln -s /usr/local/node-v12.16.1-linux-x64/bin/npm /usr/bin/npm
ln -s /usr/local/node-v12.16.1-linux-x64/bin/node /usr/bin/node
node --version
v12.16.1
npm --version
6.13.4


npm install pm2 -g

ln -s /usr/local/node-v12.16.1-linux-x64/bin/pm2 /usr/bin/pm2

 

nodejs进程管理程序

# vim node_cn_templates_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='templates-cn-max'
node_port=2233

program_dir="/data/www/vhosts/edrawmax.cn/httpdocs/temp/"

#3.重启 restart_templates-cn-max
restart_community_cn_master() {
    cd ${program_dir} && npm i
    cd ${program_dir} && pm2 start
    cd ${program_dir} && pm2 restart templates-cn-max
    if [[ $? == '0' ]];then
        sleep 2
        pid=`pm2 status|grep templates-cn-max|grep online|wc -l`
        pro_num=`sudo /usr/bin/netstat -tnlp|grep ${node_port}|wc -l`
        if [[ $pid -gt 1 && $pro_num -gt 0 ]];then
            echo "start templates-cn-max $node_process ok"
        else
            echo "start templates-cn-max $node_process failed"
        fi
    fi
}

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

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

if [ $# != 1 ]
then
        usage >&2
        exit 1
fi
OPT=$1
case $OPT in

restart)
        restart_community_cn_master
    ;;
status)
        status_community_cn_master
    ;;
*)
    echo "Usage:`basename $0`  [restart|status]"
    exit 1
esac

 

# 开机自启动

# php 程序
su apache -l -c 'cd /data/www/vhosts/ && /bin/bash /usr/local/worksh/saler_server_manager.sh start'
# nodejs
su apache -l -c 'cd /data/www/vhosts/mm.chinasoft.cn/community/ && pm2 start'
sleep 1
/usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf -u apache

# 第一次启动程序直接使用 pm2 start 以集群模式启动程序
pm2 start
[apache]$ pm2 status

┌─────┬─────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name                    │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼─────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 5   │ templates-cn-max        │ default     │ 2.14.12 │ cluster │ 2306     │ 2m     │ 25   │ online    │ 0%       │ 62.8mb   │ apache   │ disabled │
│ 6   │ templates-cn-max        │ default     │ 2.14.12 │ cluster │ 2313     │ 2m     │ 24   │ online    │ 0%       │ 56.3mb   │ apache   │ disabled │
│ 7   │ templates-cn-max        │ default     │ 2.14.12 │ cluster │ 2329     │ 2m     │ 25   │ online    │ 0%       │ 61.7mb   │ apache   │ disabled │
└─────┴─────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

 

 添加apache用户运行 netstat 命令的权限

apache ALL=NOPASSWD: /usr/bin/netstat

# vim /etc/sudoers

 

/data/www/vhosts/chinasoft.cn/httpdocs/temp]$ more ecosystem.config.js 
module.exports = {
    apps: [
        {
            name: 'templates-cn-max',
            exec_mode: 'cluster',
            instances: '3', // Or a number of instances
            script: './node_modules/nuxt/bin/nuxt.js',
            args: 'start'
        }
    ],
    node_args: [
        "--max_old_space_size=200"
    ]
}