全部步骤在MAC 电脑环境下操做node
1、配置环境
一、链接到远程服务器
一、购买阿里云ECS服务器,我选用的 ubantu 14.0.4 (64位),购买的时候输入的密码记录下来,没有设置的话能够随后在ECS控制台修改 二、浏览器进入阿里云平台 -> ECS控制台 -> 实例列表 三、查看实例的信息:记录下本身的IP(公) 47.xxx.xxx.xxx 四、点击管理,能够修改一下实例名称什么的,默认是一串很差识别的乱码,能够改为本身好好记的。 五、在本地的打开终端: 输入 $ ssh root@47.xxx.xxx.xxx ECS服务器公网 IP远程链接,输入本身的远程ubantu 服务器密码nginx
可能会提示如下信息 选yes 而后再输入远程服务器密码密码便可。看到Welcome to Ubuntu。。。证实链接成功git
wjw$ ssh root@47.xxx.xxx.xxx The authenticity of host '47.xxx.xxx.xxx (47.xxx.xxx.xxx)' can't be established. ECDSA key fingerprint is SHA256:Nq44XG9TtdnZ7yNE6P0k0S2FOGmiz/sdSkmu2j7ze2k. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '47.xxx.xxx.xxx' (ECDSA) to the list of known hosts. root@47.103.101.102's password: Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-93-generic x86_64) * Documentation: https://help.ubuntu.com/ Welcome to Alibaba Cloud Elastic Compute Service ! Last login: Sun Jun 2 15:22:59 2019
二、给服务器安装升级工具包
一、用$ sudo apt-get update 命令
# sudo apt-get update 。。。 Reading package lists... Done
二、安装必备的工做包
$ sudo apt-get install git vim openssl build-essential libssh-dev wget curl
中间会提示你,安装包会占用多少磁盘空间,选择输入yes就好github
三、安装nvm (node version manager), 帮助咱们管理 node 版本
到github 上搜索nvm https://github.com/nvm-sh/nvm 找到安装命令,第二步咱们已经安装了 curl, 直接按照 网页命令安装就好 curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bashubuntu
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 13226 100 13226 0 0 7074 0 0:00:01 0:00:01 --:--:-- 7076 => Downloading nvm from git to '/root/.nvm' => Cloning into '/root/.nvm'... remote: Enumerating objects: 278, done. remote: Counting objects: 100% (278/278), done. remote: Compressing objects: 100% (249/249), done. remote: Total 278 (delta 33), reused 88 (delta 16), pack-reused 0 Receiving objects: 100% (278/278), 142.36 KiB | 0 bytes/s, done. Resolving deltas: 100% (33/33), done. Checking connectivity... done. => Compressing and cleaning up git repository => Appending nvm source string to /root/.bashrc => Appending bash_completion source string to /root/.bashrc => Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
安装好以后 须要从新连一下服务器,使咱们的nvm命令生效. 用 # nvm ls 命令查看nodeJS的版本状况vim
$ root@47.x.x.x ... root@Jarvis:~# nvm ls N/A iojs -> N/A (default) node -> stable (-> N/A) (default) unstable -> N/A (default)
三、安装nodeJS
$ nvm install node #或者 $ nvm use v12.3.1 # 也能够用 如下命令 来指定默认版本 $ nvm alias default v112.3.1 # 最后 查看版本状况 $ nvm ls root@Jarvis:~# nvm ls -> v12.3.1 default -> node (-> v12.3.1) node -> stable (-> v12.3.1) (default) stable -> 12.3 (-> v12.3.1) (default) iojs -> N/A (default) unstable -> N/A (default) lts/* -> lts/dubnium (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.16.0 (-> N/A) lts/dubnium -> v10.16.0 (-> N/A) root@Jarvis:~#
四、建立一个node服务
一、在 nodeJS 官网的文档里 拷贝示例代码,修改 端口号 为3010 二、在 服务端的根目录下建立 server.js 文件 $ vim server.js 粘贴示例代码:浏览器
const http = require('http'); const hostname = '127.0.0.1'; const port = 3010; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
三、保存运行 $ node server.js //配置ubantu 防火墙 $ sudo vi /etc/iptables.up.rules 在vim下输入bash
-A INPUT -s 127.0.0.1 -p tcp --destination-port 3010 -m state --state NEW,ESTABLISHED -j ACCEPT -A OUTPUT -s 127.0.0.1 -p tcp --soruce-port 3010 -m state --state ESTABL ISHED -j ACCEPT ~
因为服务器以前没有配置过任何 iptables ,打开的 /etc/iptables.up.rules 是空文件,因此,能够先在 命令行执行如下命令 先停用iptables服务器
$sudo iptalbes -F $sudo iptables -X $sudo iptables -Z $sudo iptables -P INPUT ACCEPT v$sudo iptables -P OUTPUT ACCEPT $sudo iptables -P FORWARD ACCEPT $sudo modprobe -r ip_tables
再启用ssh
# 本地进程 lo 的 INPUT 和 OUTPUT 连接,eth0 的 INPUT 链 $sudo iptables -A INPUT -i lo -j ACCEPT $sudo iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -jACCEPT $sudo iptables -A INPUT -i eth0 -m state --state NEW,INVALID -j LOG $sudo iptables -A OUTPUT -o lo -j ACCEPT #开放SSH端口22 iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT #开放Web端口80 iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT #开放FTP端口2一、20 iptables -A INPUT -p tcp --dport 20 -j ACCEPT iptables -A INPUT -p tcp --dport 21 -j ACCEPT #删除FTP端口2一、20 iptables -D INPUT -p tcp --dport 20 -j ACCEPT iptables -D INPUT -p tcp --dport 21 -j ACCEPT #容许loopback(否则会致使DNS没法正常关闭等问题) IPTABLES -A INPUT -i lo -p all -j ACCEPT (若是是INPUT DROP) IPTABLES -A OUTPUT -o lo -p all -j ACCEPT (若是是OUTPUT DROP) #保存iptables规则 iptables-save > /etc/iptables.up.rules $sudo modprobe ip_tables #修改 /etc/network/interfaces ,添加下面末尾2行脚本 auto eth0 iface eth0 inet dhcp pre-up iptables-restore < /etc/network/iptables.up.rules post-down iptables-save > /etc/network/iptables.up.rules
在 iptables.up.rules 文件中 ,能够在 COMMIT一行的上方添加本身须要的规则
# Generated by iptables-save v1.4.21 on Sun Jun 2 17:45:31 2019 *filter :INPUT ACCEPT [1:40] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [465:67402] -A INPUT -i lo -j ACCEPT -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -i eth0 -m state --state INVALID,NEW -j LOG -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT -A OUTPUT -o lo -j ACCEPT COMMIT # Completed on Sun Jun 2 17:45:31 2019
而后保存 :wq
从新加载 iptables $sudo iptables-restore < /etc/iptables.up.rules
关闭没必要要的服务
sudo ufw stop sudo service nginx stop
用node命令打开 server.js 运行服务
root@Jarvis:~# node server.js Server running at http://127.0.0.1:3010/
再在另外一个终端里启动一个 远程服务 而后再命令行里 输入: curl http://127.0.0.1:3010 发现服务启动成功
root@Jarvis:~# curl http://127.0.0.1:3010 Hello, World! root@Jarvis:~#
//指定 node 版本并 设为默认使用版本 $nvm use v10.16.0 && nvm alias default v10.16.0