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
-
forever: https://github.com/foreverjs/forever
-
使用forever管理nodejs应用教程: http://www.jb51.net/article/50576.htm
-
help
usage: forever [action] [options] SCRIPT [script-options]
Monitors the script specified in the current process or as a daemon
actions:
start Start SCRIPT as a daemon
stop Stop the daemon SCRIPT by Id|Uid|Pid|Index|Script
stopall Stop all running forever scripts
restart Restart the daemon SCRIPT
restartall Restart all running forever scripts
list List all running forever scripts
config Lists all forever user configuration
set <key> <val> Sets the specified forever config <key>
clear <key> Clears the specified forever config <key>
logs Lists log files for all forever processes
logs <script|index> Tails the logs for <script|index>
columns add <col> Adds the specified column to the output in `forever list`
columns rm <col> Removed the specified column from the output in `forever list`
columns set <cols> Set all columns for the output in `forever list`
columns reset Resets all columns to defaults for the output in `forever list`
cleanlogs [CAREFUL] Deletes all historical forever log files
options:
-m MAX Only run the specified script MAX times
-l LOGFILE Logs the forever output to LOGFILE
-o OUTFILE Logs stdout from child script to OUTFILE
-e ERRFILE Logs stderr from child script to ERRFILE
-p PATH Base path for all forever related files (pid files, etc.)
-c COMMAND COMMAND to execute (defaults to node)
-a, --append Append logs
-f, --fifo Stream logs to stdout
-n, --number Number of log lines to print
--pidFile The pid file
--uid Process uid, useful as a namespace for processes (must wrap in a string)
e.g. forever start --uid "production" app.js
forever stop production
--sourceDir The source directory for which SCRIPT is relative to
--workingDir The working directory in which SCRIPT will execute
--minUptime Minimum uptime (millis) for a script to not be considered "spinning"
--spinSleepTime Time to wait (millis) between launches of a spinning script.
--colors --no-colors will disable output coloring
--plain alias of --no-colors
-d, --debug Forces forever to log debug output
-v, --verbose Turns on the verbose messages from Forever
-s, --silent Run the child script silencing stdout and stderr
-w, --watch Watch for file changes
--watchDirectory Top-level directory to watch from
--watchIgnore To ignore pattern when watch is enabled (multiple option is allowed)
--killSignal Support exit signal customization (default is SIGKILL)
used for restarting script gracefully e.g. --killSignal=SIGTERM
-h, --help You're staring at it
[Long Running Process]
The forever process will continue to run outputting log messages to the console.
ex. forever -o out.log -e err.log my-script.js
[Daemon]
The forever process will run as a daemon which will make the target process start
in the background. This is extremely useful for remote starting simple node.js scripts
without using nohup. It is recommended to run start with -o -l, & -e.
ex. forever start -l forever.log -o out.log -e err.log my-daemon.js
forever stop my-daemon.js
启动脚本范例
-
通过forever或脚本来管理站点服务的启动和停止,并设置服务自启动;
-
日志统一输出至/var/log/,设置日志回滚;
-
避免重复启动。
-
以下待完成:
使用方法
-
将公共启动脚本放到/usr/local/bin目录中
cp nodejs_startup.sh /usr/local/bin/
chmod 755 /usr/local/bin/nodejs_startup.sh
-
编辑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
项目构建
-
grunt: 基于任务的命令行构建工具。 类似Java业界的Ant工具一样。
-
Bower: 管理bootstrap、JQuery、CSS等前端框架
国内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