Home >Web Front-end >JS Tutorial >Detailed explanation of how to deploy Nodejs projects in Linux servers

Detailed explanation of how to deploy Nodejs projects in Linux servers

青灯夜游
青灯夜游forward
2021-05-06 10:34:573644browse

This article introduces in detail the method of deploying Nodejs projects on Linux and teaches you step by step how to deploy NodeJs projects on Linux servers. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of how to deploy Nodejs projects in Linux servers

Log in to the server

Use GitBash or Xshell or CMD Wait for the terminal to log in and connect to the server

ssh 用户名@ip地址

If you use the server for the first time, update it first. The following program basically uses the Yum source installation method:

yum update -y

Yum command

// 1 安装 
yum install package  // 安装指定的安装包package 

// 2 更新和升级 
yum update  // 全部更新 
yum update package  // 更新指定程序包package
yum check-update  // 检查可更新的程序 
yum upgrade package  // 升级指定程序包package 

// 3 查找和显示 
yum info // 列出所有可以安装或更新的包的信息
yum info package //显示安装包信息package 
yum list // 显示所有已经安装和可以安装的程序包 
yum list package  // 显示指定程序包安装情况package
yum search package // 搜索匹配特定字符的package的详细信息

// 4 删除程序 
yum remove | erase package  // 删除程序包package
yum deplist package  // 查看程序package依赖情况

// 5 清除缓存 
yum clean packages  // 清除缓存目录下的软件包 
yum clean headers // 清除缓存目录下的 headers 
yum clean oldheaders // 清除缓存目录下旧的 headers 
yum clean, yum clean all  // (= yum clean packages; yum clean oldheaders) 清除缓存目录下的软件包及旧的headers

Git installation

Installation under the servergit; the traditional way of developing source code upload server can be usedscp or ftp method, but here the Git flow is used to manage, and the git operation is performed on the server side;

2.1. Check whether git is installed

rpm -qa|grep git
或者
 git --version
或者
 yum info git

Detailed explanation of how to deploy Nodejs projects in Linux servers

Detailed explanation of how to deploy Nodejs projects in Linux servers

##If it is already installed, you need to uninstall it first. The uninstall command is as follows:

rpm -e --nodeps git or rpm -e git

Detailed explanation of how to deploy Nodejs projects in Linux servers

2.2. Install Git

yum install git

Detailed explanation of how to deploy Nodejs projects in Linux servers

Enter y and press Enter

Detailed explanation of how to deploy Nodejs projects in Linux servers## Then use

rpm -qa|grep git

or git version to check whether Git has been installed.

Detailed explanation of how to deploy Nodejs projects in Linux servers

2.3. Clone the remote project to the server

Node installation

under the server To install, choose any of the following methods to deploy the Node.js environment;

3.1. Install using binary files

The installation package used in this deployment process is compiled. binary file. After decompression,

node

and npm already exist in the bin folder, so there is no need to recompile. Complete the following operations to deploy the Node.js environment using binaries: Download the Node.js installation package

wget
nodejs.org/dist/v6.9.5…

##Unzip the file

tar xvf node-v6.9.5-linux-x64.tar.xz
Create a soft link, and you can directly use the

node

and

npm commands

ln -s /root/node-v6.9.5-linux-x64/bin/node /usr/local/bin/node
ln -s /root/node-v6.9.5-linux-x64/bin/npm /usr/local/bin/npm
in any directory to view node

,

npm Version

node -v
npm -v
At this point, the Node.js environment has been installed. The software is installed in the /root/node-v6.9.5-linux-x64/

directory by default

If you need to install the software to other directories (for example: /opt/node /

), please do the following:

mkdir -p /opt/node/
mv /root/node-v6.9.5-linux-x64/* /opt/node/
rm -f /usr/local/bin/node
rm -f /usr/local/bin/npm
ln -s /opt/node/bin/node /usr/local/bin/node
ln -s /opt/node/bin/npm /usr/local/bin/npm
3.2. Use NVM to manage multiple versions (yum installation)

NVM (Node Version Manager) is Node .js version management software allows you to easily switch between various versions of Node.js. Suitable for people who have been doing node development for a long time or who need to quickly update node versions or quickly switch node versions

Complete the following operations and use NVM to install multiple Node.js versions:

Use git to Clone the source code to the local

~/.nvm

directory and check the latest version

yum install git

git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
Activate NVM
echo ". ~/.nvm/nvm.sh" >> /etc/profile
source /etc/profile

List all versions of Node.js

nvm list-remote

Install multiple Node.js versions

nvm install v14.0.0 -g
nvm install v14.10.0 -g

Run

nvm ls

View the installed Node.js version, The version currently in use is v7.4.0. The return result is as follows

[root@iZXXXXZ .nvm]# nvm ls
      v6.9.5
->    v7.4.0
      system
stable -> 7.4 (-> v7.4.0) (default)
unstable -> 6.9 (-> v6.9.5) (default)
Run nvm use v7.4.0

Switch the Node.js version to

v7.4.0. The returned results are as follows.

[root@iZXXXXZ .nvm]# nvm use v7.4.0
Now using node v7.4.0
Nginx deployment

Installation under the server;
sudo yum install -y nginx
// 检测是否安装成功,验证 Nginx 配置是否正确;
nginx -t

启动命令 service nginx start
停止命令 service nginx stop
重启命令 service nginx restart
# config: /etc/nginx/nginx.conf 安装目录
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
日志文件在var/log/nginx

Pm2 installation

Installation within the project , cd to the project;
npm i pm2 -g

Start the project

pm2 start app // 实际执行文件在app 工程下的 index.js

pm2 Common commands:

// 安装
sudo npm i pm2 -g
 --watch:监听文件变化
 // 2表示启动多少实例
pm2 start app.js --watch i 2
 // 根据机器cpu核心数最大限度利用资源
pm2 start app -i max

 // 查看运行的进程
pm2 ls

 // 关闭
pm2 stop app
pm2 stop all

// 重启
pm2 restart app
We can also build a in the project process.yml

Configuration file

apps:
  - script : app.js
    intance : 2
    watch : true
    env : 
      NODE_ENV : production
Then execute
pm2 start process.yml

MongoDB installation

Install within the project; use Yum source installation;

Configure system yum source

vi /etc/yum.repos.d/mongodb-org-4.4.repo Add the following information:
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc


name         # 名称
baseurl      # 获得下载的路径
gpkcheck=1   # 表示对从这个源下载的rpm包进行校验;
enable=1     # 表示启用这个源。
gpgkey       # gpg验证

Latest

MongoDB

Configuration address:

docs.mongodb.com/manual/tuto…

保存退出:
:wq

退出不保存
:wq!

安装MongoDB

sudo yum install -y mongodb-org

验证

rpm -qa |grep mongodb

rpm -ql mongodb-org-server

运行结果分别如下图:

Detailed explanation of how to deploy Nodejs projects in Linux servers

启动MongoDB(常用指令)

// 开启MongoDB
sudo service mongod start  或者
systemctl start mongod.service  

sudo chkconfig mongod on  # 加入开机启动
sudo service mongod restart # 重启MongoDB

// 关闭MongoDB
sudo service mongod stop  # 关闭防火墙

// 卸载MongoDB
sudo yum erase $(rpm -qa | grep mongodb-org)    # 卸载MongoDB
sudo rm -r /var/log/mongodb  # 删除日志文件
sudo rm -r /var/lib/mongo    # 删除数据文件

实现远程连接Mongodb

修改绑定 ip 默认127.0.0.1 只允许本地连接, 所以修改为 bindIp:0.0.0.0,退出保存;.1

vi /etc/mongod.conf

// 编辑以下内容
// network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

重启MongoDB

sudo service mongod restart 
复制代码

开放对外端口,关闭防火墙

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT

远程连接

// 启动 MongoDB 服务,后面是服务器公网 ip 地址 + 默认端口 27017
mongo 10.128.218.14:27017

1.0 创建用户,设置账号,密码,权限

// admin数据库(管理使用)
> use admin
switched to db admin
> db.createUser({ user:"root", pwd:"123456", roles:["root"] })
Successfully added user: { "user" : "root", "roles" : [ "root" ] }

// 其他数据库 test (用作开发使用)
> use test
switched to db test
> db.createUser({ user:"admin", pwd:"123456", roles:["readWrite", "dbAdmin"] })
Successfully added user: { "user" : "root", "roles" : [ "root" ] }

2.0 修改 mongodb.conf 文件,启用身份验证

vi /etc/mongod.conf

security:
  authorization: "enabled"   # disable or enabled

3.0 重启 MongoDB

sudo service mongod restart

4.0 用户认证

> use admin
switched to db admin
> db.auth("root", "123456")
1 // 授权成功
 
// 其他常用命令
db.updateUser(user, writeConcern) # 更新用户
db.dropUser('test') # 删除用户

5.0 远程连接

// 终端连接
mongo 10.128.218.14:27017/database -u username -p password

// mongoose方式连接(项目内的方式)
mongoose.connect('mongodb://admin:123456@10.128.218.14:27017/test?options...', {useNewUrlParser: true});

# 用户名:密码@域名或者服务器公网IP:端口/数据库名称?配置项

Window本地启动 MongoDB:

1.0 管理员身份打开 cmd ;

2.0 进入本地 MongoDB 安装目录 cd C:\Program Files\MongoDB\Server\4.4

3.0 执行 net start mongodb

Detailed explanation of how to deploy Nodejs projects in Linux servers

其他命令

1、端口被占用了:检查端口占用命令. 根据报错提示, 我这里检查的是 4000 端口:

lsof -i:4000

Detailed explanation of how to deploy Nodejs projects in Linux servers

2、杀掉当前进程

kill 2805

在执行

npm run dev

可以了!!! 查看服务器个程序占用内存大小

du -h --max-depth=1

Detailed explanation of how to deploy Nodejs projects in Linux servers

Linux shell 脚本

自己编写服务运行脚本,直接在服务器上运行,下面是运行指令,脚本内容这里就不展示了,可以自己补充 xshell 脚本相关知识。

sh run.sh // 启动服务脚本;
sh stop.sh // 启动服务脚本;
netstat -nltp  // 当前服务器端口状态;

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of Detailed explanation of how to deploy Nodejs projects in Linux servers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete