1. Nodejs installation

1. Log in to the server

Under MAC, first open terminal, then type sudo su - press Enter to go to the root directory and then type: SSH -p port number server username @ip (for example, SSH -p 22 [email protected]) press Enter to enable you to enter yes or no to confirm the connection. Enter yes and then enter the password of the user on the server and press Enter to enter the directory of your account on the server, that is, the connection is successfulCopy the code

Update the server for the first time

yum update -y
Copy the code

2. Download NodeJS

Obtain the connection to download the operating system from the NodeJS official website

Wget HTTP: / / https://nodejs.org/dist/v8.10.0/node-v8.10.0-linux-x64.tar.xzCopy the code

The wget command is used to download the Node.js installation package. After the download is complete, you can see the nodeJS installation package in the server directory.

Decompress the installation package

The tar XVF node - v8.10.0 - Linux - x64. Tar. XzCopy the code

The decompressed files are displayed in the server directory

By default, node is installed in /root/node-v8.10.0-linux-x64/. Install node in another directory, such as /opt/node/.

Development:

Linux centos Directory file description

Linux directory Structure

Mkdir -p /opt/node/ mv /root/node-v8.10.0-linux-x64/* /opt/node/Copy the code

Delete the /root/node-v8.10.0-linux-x64/ installation package

Rm - rf/root/node - v8.10.0 - Linux - x64 /Copy the code

The installation package is a compiled file. After decompression, node and NPM are already stored in the bin folder. You do not need to compile the package again.

3. Create a soft link to make the node and NPM commands globally valid. Create soft links so that node and NPM commands can be used directly in any directory:

ln -s /opt/node/bin/node /usr/local/bin/node
ln -s /opt/node/bin/npm /usr/local/bin/npm

Copy the code

4. Install CNPM

npm install -g cnpm --registry=https://registry.npm.taobao.org 
Copy the code

Configure environment variables, global command

ln -s /opt/node/bin/cnpm /usr/local/bin/cnpm
Copy the code

Type node -v to see the version information

2. Install pM2

Pm2 is a process manager for Node applications with load balancing function. When you want to put your stand-alone code on all cpus on all servers, and keep the process alive forever,0 second reload.

We run the Node project locally, just node serve.js will run, but if shut down, the project will not run. Pm2 solves this problem by making the process permanent.

Install the pm2

npm install pm2@latest -g
Copy the code

Create a soft link to make the pm2 command globally valid

ln -s /opt/node/bin/pm2 /usr/local/bin/pm2
Copy the code

Enter pm2 -v to see the version information

3. Install mongodb

1. Download mongodb

Wget HTTP: / / https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.3.tgzCopy the code

2. Decompress the downloaded package

Tar - ZXVF mongo - Linux - x86_64-3.6.3. TGZCopy the code

3. Rename mongodb-linux-x86_64-3.6.3

Mv mongo - Linux - x86_64-3.6.3 mongodb3.6.3Copy the code

4. Create the mongodb directory in /usr/local

mkdir -p mongodb
Copy the code

5, move mongodb3.6.3 to/usr/local/mongo directory:

Mv/mongodb3.6.3 / / usr /local/mongodb
Copy the code

6, in the/usr/local/mongo/mongodb3.6.3 / directory to create a store data and log directory:

mkdir -p data/koa2-blog/db
mkdir -p data/koa2-blog/log
Copy the code

7, in the/usr/local/mongo/mongodb3.6.3 / conf directory to create a configuration file mongo. Conf.

mkdir -p conf
vim mongodb.conf
Copy the code

mongodb.conf

# set port number (default port number 27017)
port = 27018

You must specify an IP address for remote connection. 0.0.0.0 does not restrict IP access and enables the corresponding portBind_ip = 0.0.0.0Set the directory for storing data files
dbpath = /usr/local/ mongo/mongodb3.6.3 / data/db/koa2 - blog# Set the directory for storing log files and the name of the log file
logpath = /usr/local/ mongo/mongodb3.6.3 / data/koa2 blog2 /log/mongodb.log

Each database will be stored in a separate directory
directoryperdb = true

Set to run as a daemon, that is, in the background
fork = true

# log append
logappend=true

# access permission
auth=false

Copy the code

Parameter Description:

Parameter Description: -- dbPath Database path (data file) -- logPath Log file path --master specifies the master machine --slave specifies the slave machine --source Specifies the IP address of the master machine --pologSize Specifies that the log file size does not exceed 64 MB. Because resync is very costly and time consuming, it is best to avoid resync by setting an oplogSize large enough (the default oplog size is 5% of the size of the free disk). -- logAppend add, --journal --port --port --fork -- run in the background --only specifies which database to copy -- Slavedelay refers to the interval of slave replication detection --auth -- Syncdelay Time for data to be written to disks (seconds). 0 indicates that data is written directly without waiting. -- NotablesCAN Does not allow table scanning. Default 2000 -- pidFilepath Specifies the process file. If this parameter is not specified, the process file will not be generated. --bind_ip Binds the IP addressCopy the code

8. Customize services

Create the mongodb. Service file in the /lib/systemd/system/ directory with the following content

[Unit]
     Description=mongodb
     After=network.target remote-fs.target nss-lookup.target
[Service]
     Type=forking
     ExecStart=/usr/local/ mongo/mongodb3.6.3 / bin/mongod-f /usr/local/ mongo/mongodb3.6.3 / conf/mongo. Conf ExecReload = / bin /kill -s HUP $MAINPID
     ExecStop=/usr/local/ mongo/mongodb3.6.3 / bin/mongod - shutdown-f /usr/local/ mongo/mongodb3.6.3 / conf/mongo. Conf PrivateTmp =true
[Install]
    WantedBy=multi-user.target

Copy the code

9. Set permissions

chmod 754 mongodb.service 
Copy the code

10, start and close the service, set the boot

# start service
systemctl start mongodb.service   
# service shutdown
systemctl stop mongodb.service   
# Boot
systemctl enable mongodb.service 
# check status
systemctl status mongodb.service
If the mongodb. Service configuration is incorrect, reload the file after the change
systemctl daemon-reload
Copy the code

Set the mongo command to globally valid

ln -s /usr/local/ mongo/mongodb3.6.3 / bin/mongo/usr /local/bin/mongo
ln -s /usr/local/ mongo/mongodb3.6.3 / bin/mongod/usr /local/bin/mongod
Copy the code

Database connection succeeded

[root@VM_0_11_centos ~]# mongo --port=27018Mongo shell version v3.6.3 connecting to: mongo: / / 127.0.0.1:27018 / mongo server version: 3.6.3 Server has startup Warnings: 2018-03-27T13:14:24.748+0800 I STORAGE [initandListen] 2018-03-27T13:14:24.748+0800 I STORAGE [initandListen] ** WARNING: Strongly Recommended with the WiredTiger Storage Engine 2018-03-27T13:14:24.748+0800 I STORAGE [initandlisten] * * See http://dochub.mongodb.org/core/prodnotes-filesystem 2018-03-27 T13:14:27. 801 + 0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user,whichIs not recommended. 2018-03-27T13:14:27.801+0800 I CONTROL [initandListen] 2018-03-27T13:14:27.801+0800 I CONTROL [initandListen] 2018-03-27T13:14:27.801+0800 I CONTROL [initandListen] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is'always'.
2018-03-27T13:14:27.801+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-03-27T13:14:27.801+0800 I CONTROL  [initandlisten] 
2018-03-27T13:14:27.801+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-03-27T13:14:27.801+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'2018-03-27T13:14:27.801+0800 I CONTROL [initandListen] > 2018-03-27T13:14:27.801+0800 I CONTROL [initandListen] >Copy the code

12. Set the database access permission

Step 1: Enter the admin table

 use admin
Copy the code

Step 2: Create a super admin account

db.createUser({user:"admin".pwd:"wz123",roles:[{role:"userAdminAnyDatabase", db: "admin"}]})
Successfully added user: {
	"user" : "admin"."roles": [{"role" : "userAdminAnyDatabase"."db" : "admin"}}]Copy the code

Step 3: open access authentication In the/usr/local/mongo/mongodb3.6.3 / conf/mongo. Set up the conf

# access permission
auth=true
Copy the code

Restart the mongodb service and connect to the database. Run the show users command to check all users. You can see that an error occurs and you need to verify the administrator’s permission.

[root@VM_0_11_centos ~]# mongo --port=27018Mongo shell version v3.6.3 connecting to: mongo: / / 127.0.0.1:27018 / mongo server version: 3.6.3 > Use admin switched to db admin > show users 2018-03-27T14:10:38.323+0800 E QUERY [thread1] Error: not authorized on admin to executecommand{usersInfo: 1.0.$db: "admin" } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1686:1
shellHelper.show@src/mongo/shell/utils.js:799:9
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
> 
Copy the code

After the permissions are verified, you can view the user Users

> db.auth('admin'.'wz123')
1
> show users
{
	"_id" : "admin.admin"."user" : "admin"."db" : "admin"."roles": [{"role" : "userAdminAnyDatabase"."db" : "admin"}}]Copy the code

The admin user created by admin only has privileges to manage users and roles. If you try to do anything else, such as read data from foo in the test database, mongodb returns an error.

The database where you create the user (in this case, the Test database) is the user authentication database. Although the user is authenticated to this database, the user can still have roles for other databases. That is, the user authentication database does not restrict user permissions.

The permission roles are as follows:

Built-in roles Database user roles: read and readWrite. Database management roles: dbAdmin, dbOwner, and userAdmin. Cluster management roles: clusterAdmin, clusterManager, clusterMonitor, and hostManager. Backup and restoration roles: Backup and restore. All database roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase Superuser roles: Root // There are several other roles that indirectly or directly provide system superuser access (dbOwner, userAdmin, userAdminAnyDatabase). Allow users to read and write data from the specified database readWrite: allows users to read and write data from the specified database dbAdmin: allows users to perform management functions in the specified database, such as creating and deleting indexes, viewing statistics, and accessing system. Profile userAdmin: Allows users to write to the System. users collection, create, delete, and manage users in the specified database clusterAdmin: Only available in the Admin database, giving users management rights to all sharding and replication set related functions. ReadAnyDatabase: only available in the Admin database, granting the user read and write permissions on all databases readWriteAnyDatabase: Only available in the admin database, granting the user read and write permissions on all databases userAdminAnyDatabase: DbAdminAnyDatabase: This parameter is available only in the admin database and is granted to the user with the dbAdmin permission for all databases. Root: available only in the admin database. Super account, super permissionCopy the code

Step 4: Create a database and assign access permissions to the database

Set read and write permissions for database KOA2

[root@VM_0_11_centos ~]# mongo --port=27018Mongo shell version v3.6.3 connecting to: mongo: / / 127.0.0.1:27018 / mongo server version: 3.6.3 > Use admin switched to db admin > db.auth('admin'.'wz123')
1
> use koa2
switched to db koa2
> db.createUser({user:'wz'.pwd:'wz123',roles:[{role:'readWrite',db:'koa2'}]})
Successfully added user: {
	"user" : "wz"."roles": [{"role" : "readWrite"."db" : "koa2"}}] >Copy the code

Step 5

Write test data. Robo 3T connects to the database and writes a test collection to the KOA2 database

db.test.insert({title:'test',name:'the king',age:12})
Copy the code

Open RoBo 3T to connect to database, you can see that the collection has test

4. Install Nginx

yum install nginx
Copy the code

Modify the configuration in /etc/nginx. conf to delete all writes

## Working mode and connection number upper limit
events {
    The maximum number of connections for a single process
    worker_connections 1024;
}

Set up the HTTP server
http {
    include /etc/nginx/conf.d/*.conf;
}

Copy the code

Create koa2-blog-8081.conf in /etc/nginx/conf.d

server{ listen 80; Server_name 118.25.6. XXX;Configure the front-end file
    location / {
        root /home/koa2-blog/build/;
        index  index.html index.htm;
    }
    Configure the back-end interface
     location /api {
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_set_header X-Nginx-Proxy true;
           proxy_set_header Connection "";
           proxy_set_header Cookie $http_cookie; Proxy_pass http://127.0.0.1:8081; }}Copy the code

Then restart Nginx

 systemctl restart nginx.service
Copy the code

Command:

# Start nginx service
systemctl start nginx.service
# Stop nginx service
systemctl stop nginx.service
Restart the nginx service
systemctl restart nginx.service
# re-read the nginx configuration (this is most commonly used to make the modified configuration take effect without stopping the nginx service)
systemctl reload nginx.service
Copy the code

Parsing all nginx. Conf

5. Upload the project to the server

Upload the backend to the /home/koa2-blog/ directory and upload the react project to this directory

--koa2-blog
    --build
    --server
    --node_modules
    --package.json
Copy the code

Pm2 run server. Js

Common PM2 commands

pm2 start server.js
Copy the code

Open the front-end project in your browser and see that the site is accessible

Refer to the article