使用apt安装nodejs,

sudo apt install nodejs

安装npm

apt install npm


源码安装

1.下载Nodejs源码

http://nodejs.cn/download/

wget -c 资源地址

-c表示当网络中断恢复后断点续传

下载后解压,进入解压目录

2.生成Makefile

编译需要依赖python环境、c、c++

apt install gcc

apt install python

./configure --prefix=/usr/local/nodejs/

运行python脚本

3.make -j 4 && sudo make install

安装make工具

apt install make
apt install make-guile

-j 4 表示用4个线程去编译

4.配置环境变量

vi ~/.bashrc

ubuntu安装nodejs教程

修改完后 source ~/.bashrc 使配置生效

env | grep PATH

env显示环境变量

启动node

node

查看nodejs版本

node --version

最简单的http服务

1.使用require引入http模块

2.创建http服务

3.侦听端口

server.js

'use strict'

var http = require('http');

var app = http.createServer(function(req,res){
    res.writeHead(200,{'Content-Type':'text/plain'});
    res.end('HelloWorld\n');
}).listen(8080,'0.0.0.0');

启动Nodejs服务

netstat -ntpl 查询所有tcp端口

node app.js

浏览器访问ip:8080 显示HElloWorld

启动程序的三种方式

node app.js

nohub node app.js

forever start app.js

使用forever启动

1.安装forever

yum -y install forever -g

-g 表示在任何目录下使用forever,如果不加,只能在当前目录使用

如果执行失败

使用 npm config set strict-ssl false

forever start server.js 启动服务

forever stop server.js 关闭服务

ssh root@IP 使用ssh连接

标签: 安装, http, ubuntu, apt, install, js, nodejs, forever

相关文章推荐

添加新评论,含*的栏目为必填