Home>Article>Web Front-end> Detailed explanation of how to deploy Nodejs projects in Linux servers
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.
UseGitBash
orXshell
orCMD
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 theYum
source installation method:
yum update -y
// 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
Installation under the servergit
; the traditional way of developing source code upload server can be usedscp
orftp
method, but here theGit
flow is used to manage, and thegit
operation is performed on the server side;
rpm -qa|grep git 或者 git --version 或者 yum info git##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
yum install git
Enter y and press Enter
## Then use
rpm -qa|grep gitorgit version
to check whether Git has been installed.
andnpm
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
nodejs.org/dist/v6.9.5…Create a soft link, and you can directly use thetar xvf node-v6.9.5-linux-x64.tar.xz
node
andnpmcommands
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
,
npmVersion
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 /
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
directory and check the latest versionyum 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/profileList all versions of Node.js
nvm list-remoteInstall multiple Node.js versions
nvm install v14.0.0 -g nvm install v14.10.0 -gRun
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
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
npm i pm2 -g
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 appWe can also build ain 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
Configure system yum source
[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验证
MongoDB
Configuration address:保存退出: :wq 退出不保存 :wq!
sudo yum install -y mongodb-org
rpm -qa |grep mongodb rpm -ql mongodb-org-server
运行结果分别如下图:
// 开启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 # 删除数据文件
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.
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
// 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" ] }
vi /etc/mongod.conf security: authorization: "enabled" # disable or enabled
sudo service mongod restart
> use admin switched to db admin > db.auth("root", "123456") 1 // 授权成功 // 其他常用命令 db.updateUser(user, writeConcern) # 更新用户 db.dropUser('test') # 删除用户
// 终端连接 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:端口/数据库名称?配置项
1.0 管理员身份打开 cmd ;
2.0 进入本地 MongoDB 安装目录 cd C:\Program Files\MongoDB\Server\4.4
3.0 执行 net start mongodb
1、端口被占用了:检查端口占用命令. 根据报错提示, 我这里检查的是4000
端口:
lsof -i:4000
2、杀掉当前进程
kill 2805
在执行
npm run dev
可以了!!! 查看服务器个程序占用内存大小
du -h --max-depth=1
自己编写服务运行脚本,直接在服务器上运行,下面是运行指令,脚本内容这里就不展示了,可以自己补充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!