Ali cloud server + ubantu + nodejs basic server configuration process

All steps in the MAC computer environment

First, configure the environment

1, is connected to a remote server

1, buy Ali cloud ECS server, I use the ubantu 14.0.4 (64-bit), the time of purchase password recorded, if not set can then modify the console in ECS
2, Ali browser into the cloud platform -> ECS console -> instance list
information 3, view instance: record your own IP (male) 47.xxx.xxx.xxx
4, click management, you can modify what the instance name, the default is a string of poor recognition of garbled you can take into their own mind.
5, open a terminal in the local: Enter $ ssh [email protected] ECS server public IP remote access, enter your password for the remote server ubantu

The following information may be prompted to choose yes and then enter the remote server password password. See Welcome to Ubuntu. . . Proved successful connection

wjw$ ssh [email protected]
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.
[email protected]'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

2, to install the server upgrade kit

1, with a $ sudo apt-get update command

# sudo apt-get update

。。。
Reading package lists... Done

2, the installation work of the necessary packets

$ sudo apt-get install git vim openssl build-essential libssh-dev wget curl

Middle will remind you of how much disk space to install the package takes up, like a select input yes

3, installation nvm (node ​​version manager), to help us manage node version

On github search NVM
https://github.com/nvm-sh/nvm
find the installation command, the second step we have installed curl, like the installation commands directly in accordance with page
curl -o- https: //raw.githubusercontent. com / nvm-sh / nvm / v0.34.0 / install.sh | bash

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

After installing the server needs to reconnect it, make our nvm command to take effect.
Check with the version of the case nodeJS # nvm ls command

$ [email protected] 

...

root@Jarvis:~# nvm ls
            N/A
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)

3, installation 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:~# 

4, create a service node

1, a copy of the document nodeJS official website of sample code, modify the port number 3010
2. Create server.js file in the root directory of the server's
$ vim server.js
paste sample code:

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}/`);
});

3, run save
$ server.js the Node
// firewall configuration ubantu
$ sudo vi /etc/iptables.up.rules
input in vim

-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 
~                

Since any iptables not been previously configured server, open /etc/iptables.up.rules file is empty, so, you can execute the following command at the command line
to disable 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

Then enable

# 本地进程 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端口21、20
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#删除FTP端口21、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

In iptables.up.rules file, you can add the rules you need in the top row of the 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

Then save
: wq

Reload iptables
$ sudo iptables-Restore </etc/iptables.up.rules

Turn off unnecessary services

sudo ufw stop
sudo service nginx stop

Open server.js run the service using node command

root@Jarvis:~# node server.js 
Server running at http://127.0.0.1:3010/

Restart a remote service on another terminal in
then enter the command line:
curl http://127.0.0.1:3010 Discovery Service started successfully

root@Jarvis:~# curl http://127.0.0.1:3010
Hello, World!
root@Jarvis:~# 

// node designated as the default version and use version
$ nvm use v10.16.0 && nvm alias default v10.16.0

Guess you like

Origin www.cnblogs.com/wjw-blog/p/10963327.html