For getting started with node+koa2+mysql, you can view this article https://blog.csdn.net/qq_41231694/article/details/123788781
1. Install nodejs on centos
1. Download the installation file
1. Download address: http://nodejs.cn/download/Check
the number of your server and download the corresponding installation file
getconf LONG_BIT
2. Select a suitable version to download
3. Installation
1. Upload the installation package to the specified location (I am used to putting it in: /usr/local/ directory), and decompress it
tar -xvf node-v16.14.2-linux-x64.tar.xz
2. Rename the folder (this step can be omitted. After renaming, the names of the following steps should be unified with those here)
mv node-v16.14.2-linux-x64 nodejs
3. Become global by establishing a soft link (set global variables)
ln -s /usr/local/nodejs/bin/npm /usr/local/bin/
ln -s /usr/local/nodejs/bin/node /usr/local/bin/
4. Check whether the installation is successful, command: node-v
node -v
v10.6.0
2. Migrate the local nodejs project to Centos
1. Move the locally developed nodejs to Centos.
Copy the nodejs project myKoa2
(except node_module, node_module is too large) to centos. I put it here /usr/my-nodejs-project/
.
2. Install dependencies and start
Switch to the project directory:
cd /usr/my-nodejs-project/myKoa2
Installation dependencies:
npm install
start up:
npm run start
Access:
access via server ip + configured port
3. Use pm2 to manage nodejs applications
Although the second step starts at the end, closing the service window after starting will directly exit the nodejs service, and the process will be killed.
At this time, use pm2, which is a node.js process manager. Because of the single-process feature of nodejs, it is very important to save the process from dying and automatically reload. Currently, it only supports the liunx platform
1. Install pm2
npm install pm2 -g
2. Configure pm2 soft connection (global variable)
npm install -g
All files will be placed in the pre-installed nodejs/bin/, so configure the soft link as follows.
ln -s /usr/local/nodejs/bin/pm2 /usr/local/bin/
3. Use pm2 to start the nodejs application, each application takes an application name, do not use the system to assign the application name
After reopening a window after nodejs npm start
, the following operations must be performed when nodejs is started.
Enter the bin directory of the nodejs project
cd /usr/my-nodejs-project/myKoa2/bin
start process
pm2 start www --name="myKoa2"
4. View all processes of pm2
pm2 list
5. Close window verification
Close all windows for verification to see if you can still access the nodejs project.