Under CentOS7.x, installation node + pm2 + mongoDB + nginx deployment vue project

This article made reference to qiu_freedom blog, major changes in the existing version of the update and build your own reference blog problems encountered (resolved) conferred on the description.

First, buying servers and remote connections

1. Purchase and domain name server

You can choose Tencent or other manufacturer's server, the server will get ip address after purchase, to buy the domain name, the domain name is bound to the ip address.

2. Download Xshell and Xftp

Xshell is used to remotely connect to the server, Xftp is used to remotely upload files (your project file), Download Address: URL directly , after fill in your name and email address, download address will be sent directly to your e-mail, personal use free.

Second, the installation node

1. You need a computer point of view of node version

Direct URL
Here Insert Picture Description
recommended to choose LTS with a stable version of Erbium.

2. Download the remote node

I chose Node.js version 12.16.0 of
2.1, first switch to the / usr / local / src using wget downloads

cd /usr/local/src
wget https://nodejs.org/dist/v12.16.0/node-v12.16.0-linux-x64.tar.xz

2.2, to extract the root directory
Here Insert Picture Description
noted that the display is the command line - is under the root directory
input command extracting

tar xvf node-v12.16.0-linux-x64.tar.xz

3. Create a soft link, set global environment variables so that node and npm command can be used globally

ln -s /usr/local/src/node-v12.16.0-linux-x64/bin/node /usr/local/bin/node
ln -s /usr/local/src/node-v6.11.1-linux-x64/bin/npm /usr/local/bin/npm

Detection node by node -v command is successfully installed

Third, the installation mongoDB

1. First log in using your current computer setup instructions mongodb official website

Link: URL directly
because I was CentOS server system, so I chose RedHat
1.1, copy the code circled
Here Insert Picture Description
1.2, create a file, pay attention to the corresponding version number

vi /etc/yum.repos.d/mongodb-org-4.2.repo

1.3, copy and paste the contents, press ESC,: wq to save and exit the editor

1.4, install MongoDB

sudo yum install -y mongodb-org

1.5、(可选)取消自动升级
新版本出来yum会自动升级,添加以下代码到/etc/yum.repos.d/mongodb-org-4.2.repo文件中

exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

1.6、(说明)下面是mongdb默认数据库文件以及日志的指令位置
/var/lib/mongo (the data directory)
/var/log/mongodb (the log directory)
开启mongodb
sudo service mongod start
关闭MongoDB
sudo service mongod stop
重启MongoDB.
sudo service mongod restart

四、配置mongoDB(这里请严格按照步骤)

1.开启mongoDB服务
sudo service mongod start
2.开启数据库
mongo

成功的话,如图:

Here Insert Picture Description

3.设置权限和创建权限用户

3.1、创建并切换到admin数据库

use admin

进入admin数据库,有switched to db admin的提示

3.2、 建立一个超级用户amdin,(role属性指分配的权限,db指数据库。连在一起就是在admin数据库里拥有的权限)

db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});

前面创建的是管理数据库用户的账号,对于具体的数据库,我们需要对应的用户,所以:

3.3、 创建ta数据库并切换到ta数据库

use ta

进入ta数据库有switched to db ta的提示,如图:
Here Insert Picture Description
3.4、 在ta的数据库下创建用户,分配读写的权限,db哪里一定要填你数据库的名!!

db.createUser({user:'test1',pwd:'test1',roles:[{role:'readWrite',db:'node-vue-moba-wdm'}]})

例如我的项目数据库是node-vue-moba-wdm,如图:
Here Insert Picture Description
3.5、 一定要完成上面的步骤后,再开启数据库权限
编辑文件:

vi /etc/mongod.conf

添加如下:(注意缩进,security没有缩进,authorization缩进2个字符)

security:
  authorization: enabled

3.6、 保存并重新启动。(必须)

systemctl restart mongod.service

3.7、 测试数据库
3.7.1、切换到指定数据库

use admin

3.7.2、查看所有数据库

show dbs

Here Insert Picture Description
3.7.3、(可选)你可能会发现你看不到node-vue-moba-wdm的数据库,因为你这个数据库没有一条数据,所以没显示
3.7.4 、(可选)往ta数据库插入一条数据,再运行show dbs就可以看到了

db.ta.insert({"name":"测试"})

五、安装nginx

1.下载nginx
yum install nginx
2.开启nginx服务
systemctl start nginx
3.关闭防火墙

查看防火墙状态 firewall-cmd --state

停止firewall systemctl stop firewalld.service

禁止firewall开机启动 systemctl disable firewalld.service

六、安装和配置pm2

1.全局安装pm2
npm install -g pm2
2.从安装的提示中可以看到pm2位置

Here Insert Picture Description

3.使用以下命令将pm2放入系统路径下就可以了(这样就可以在任何目录使用pm2命令),注意两个路径之间的空格
ln -s /usr/local/src/node-v12.16.0-linux-x64/bin/pm2 /usr/local/bin/

七、部署自己的代码

1.前端vue项目打包
npm run build
2.把打包好的dist文件放入你的后端项目根文件中,结构如图:

Here Insert Picture Description

3.然后在index.js文件加入如下代码:
const path = require('path');
app.use(express.static(path.join(__dirname, 'dist')));
(可选)4.删除node_modules文件
5.通过Xftp上传到远程服务器的/home文件中
6.在你项目文件下安装依赖包
npm i
7.通过pm2开启项目
pm2 start /home/'你的项目名'

Successful operation of the map
Here Insert Picture Description
(optional) attached pm2 some of the commonly used commands:
Start project: pm2 start 'your file path'
view all items: pm2 list
to restart the project: pm2 restart 'project id'
Stop the project: pm2 stop "project id "
deleted items: pm2 delete" item id "

Eight, configure nginx

At this step, the configuration is done, you will be able to visit your site at another computer, because I did not get the domain name (for the record, too much trouble), so I wrote nginx simplest configuration, the access through the public network ip, if you Xiangnong more advanced operations (multi-page Han), you can see other articles
1. modify profile

vi /etc/nginx/nginx.conf

2. Add the following code http {}, the best manual input

server {
     listen 80;
     server_name ‘你的公网ip或者你的域名’;
     location / {
          root /home/‘你的项目名’/dist;
          proxy_pass http://127.0.0.1:3000;
       }
     }

3. Save the restart nginx (must)

systemctl restart nginx.service

Nine, completed

Here Insert Picture Description

At last

This is the case of my success follow the steps, or if it can optimize better place can offer thoughts in the comments area, thank you

Published 34 original articles · won praise 13 · views 4866

Guess you like

Origin blog.csdn.net/qq_40544291/article/details/104310108