nodejs安装

直接下载二进制包

V="v0.12.7" \
&& ARCH=x64 \
&& cd /usr/local/ \
&& ( [ -f "node-$V-linux-$ARCH.tar.gz" ] || wget https://nodejs.org/download/release/$V/node-$V-linux-$ARCH.tar.gz ) \
&& tar zxvf node-$V-linux-$ARCH.tar.gz \
&& ln -sfT node-$V-linux-$ARCH nodejs

cat > /etc/profile.d/nodejs.sh <<EOF
echo "\$PATH"|grep "/usr/local/nodejs/bin" > /dev/null || export PATH="/usr/local/nodejs/bin:\$PATH"
EOF

编译安装nodejs

yum -y install gcc-c++ make
# or: yum groupinstall 'Development Tools'
V="v0.12.7" \
&& cd /usr/local/src \
&& ( [ -f "node-$V.tar.gz" ] || wget https://nodejs.org/download/release/$V/node-$V.tar.gz ) \
&& rm -rf node-$V \
&& tar zxvf node-$V.tar.gz \
&& cd node-$V \
&& ./configure --prefix=/usr/local/nodejs \
&& make && make install \
&& export PATH=/usr/local/nodejs/bin:$PATH \
&& node -v && npm -v \
&& npm config set registry http://registry.cnpmjs.org \
&& npm install -g forever

cat > /etc/profile.d/nodejs.sh <<EOF
echo "\$PATH"|grep "/usr/local/nodejs/bin" > /dev/null || export PATH="/usr/local/nodejs/bin:\$PATH"
EOF

forever

启动脚本范例

使用方法

cp nodejs_startup.sh /usr/local/bin/
chmod 755 /usr/local/bin/nodejs_startup.sh
LOGDIR=/var/log/           #统一日志目录
NODEJS=/usr/local/nodejs   #nodejs路径
#NODEJS=/opt/nodejs

#启动环境参数
#export NODE_ENV=development
#export NODE_ENV=production
export NODE_ENV=testing
export brand=foton
# 将App放到/opt/目录下,如:/opt/myportal
cp -R myportal /opt/
# 创建启动脚本软链接,使用nodejs-AppName命名(其中AppName为/opt/下的目录名称),如:
cd /etc/init.d && ln -sfT /usr/local/bin/nodejs_startup.sh nodejs-myportal

# 设置开机自启动
chkconfig --add nodejs-myportal

# 服务进程管理
/etc/init.d/nodejs-myportal {start|stop|restart|list|logs|status}
cat > /etc/logrotate.d/nodejs <<EOF
/var/log/tima/nodejs_*.log {
    copytruncate
    weekly
    rotate 20
    compress
    missingok
    size 100M
}
EOF

项目构建

国内npm镜像

npm config set registry https://registry.npm.taobao.org 
npm info underscore #(如果上面配置正确这个命令会有字符串response)
npm --registry https://registry.npm.taobao.org info underscore 
registry = https://registry.npm.taobao.org