Mac system to deploy Node.js Ali cloud server and achieve external network access

Reprinted https://www.jianshu.com/p/b5b6d062ba29

First record about the whole process:

1, buy Ali cloud server, create an instance of the console in the ESC

2, built on the ESC server node environment

3, node.js upload code to the ESC server and access the cloud server with public IP Ali

First, the ESC server purchase

1. Log Ali cloud, the cloud server to find ESC, click to buy, learn at this stage I chose the minimum configuration, the shortest duration

2, Ali cloud console open, select the cloud server ESC- instance - Create instance

 

Which choose to assign public IP, no longer need to apply for resilient public network IP, secure set of rules that you can use the default configuration, mirroring the latest version of CentOS service selection.

The figure represents a cloud server has completed the purchase.

Second, build a node on Ali cloud server environment

Here stepped on a lot of the pit, but the result is beautiful, multi-stepped pit learn.

1, first connector Ali cloud server, the terminal can operate on a Mac, in a terminal:

$ ssh [email protected]

 

Tip "Welcome to Alibaba Cloud Elastic Compute Service!" Represents a connection to the server is successful.

2, when the server is a piece of paper, there is no node environment, you can start to build node environment

① use the source code to build node.js, you are installing the software, you need to install to compile the source code development tools:

[root@iz8vbfrblr3s915qrmkrj8z ~]# yum -y groupinstall "Development Tools"

② install node.js, this time in a root server default path, execute

[root@iz8vbfrblr3s915qrmkrj8z ~]# cd ..

Back to the upper path, and then execute ls, list the files with the files in the current folder path

 

③ into the / usr / src folder, source code storage node in this folder, use wget command to download the installation package node.js, as is the Mac environment, so download the installation package node time to pay attention to choose mac version.

 

这里选择了最新的v9.9.0版本,下载好压缩包后,开始解压文件

[root@iz8vbfrblr3s915qrmkrj8z src]# tar -zxf node-v9.9.0.tar.gz 

现在在src目录下执行ls,看当前目录下有哪些文件

 

④ 进入解压后的文件夹

 

并执行配置脚本来进行预编译处理

⑤ 编译node.js源代码

[root@iz8vbfrblr3s915qrmkrj8z node-v9.9.0]# make

这一步需要很久,大概花了40分钟的样子才编译完成

⑥ 编译完成后,需要执行make install

⑦ 此时node.js已经安装完成,nodejs默认有npm,可以使用npm安装express 和 forever,全局安装

[root@iz8vbfrblr3s915qrmkrj8z node-v9.9.0]# npm -g install express forever

⑧ 创建软链接,可以全局使用node、npm命令,否则会报“命令未找到”

sudo ln -s /usr/local/bin/node /usr/bin/node 

sudo ln -s /usr/local/lib/node /usr/lib/node 

sudo ln -s /usr/local/bin/npm /usr/bin/npm

sudo ln -s /usr/local/bin/forever /usr/bin/forever

至此,node环境已经搭建完成。

?xml version="1.0" encoding="UTF-8"?

三、上传node.js代码到阿里云服务器

进入/home文件夹,把node代码放在此路径下

 

可以使用git拉取代码,此处只做演示,未拉取代码,后面完善

创建项目服务器文件:touch example.js

编辑example.js: vim example.js

(i,进入编辑模式,编辑完成按Esc键退出编辑,并输入:wq退出vim编辑器)

const http = require('http');

const hostname = '172.26.141.193';//私网IP

const port = 80;

const server = http.createServer((req, res) => {

    res.statusCode = 200;

    res.setHeader('Content-Type', 'text/plain');

    res.end('wo zheng ni ge xiao chou zhu wa zi 666.\n');

});

server.listen(port, hostname, ()=> {

    console.log(`Server running at http://${hostname}:${port}/`);

});

执行example.js(node example.js或者forever start example.js)

 

到此已经大功告成,可以在浏览器中输入公网IP+端口号,也可以是相应的域名

 

域名:由于没备案,备案又需要购买3个月及以上ESC服务,所以暂时无法打开,后续再购买体验一把

 

Guess you like

Origin www.cnblogs.com/boonook/p/11641689.html