“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
Install nodejs
Start by installing nodeJS on the server and configuring the environment, which you can refer to the official documentation without going into details
Copy projects to the server
In fact, git can be directly clone on the server. Of course, the normal version is usually the code package uploaded to the server deployment, you can transfer files to the server through PCSP:
(1) Ensure that the server (Linux) is ready and interfaces are enabled. (Specific practices directly to Baidu)
(2) Package the project into a gz file (Windows can use 7-zip, first compressed into tar, and then compressed into gz)
(3) Open the PC terminal, enter the PSCP directory, and enter the command:
PSCP [path of project package] [username]@192.168.1.1:/home/[username]Copy the code
Then enter the user password of the server to send the compressed package to /home/[username] of the server.
Note: the user must have write permission for this folder, otherwise it cannot be transmitted.
(4) Decompress to the specified directory.
Deployment project
We use PM2 for project deployment, and install pM2 via terminal on the server.
npm install -g pm2
Copy the code
Then go to the project directory and deploy with the following command:
pm2 start bin/www
Copy the code
Nodejs entry js file is bin/ WWW by default, so we start this file.
Once started, the service is accessible via IP and port. If we bind a domain name to this port, we can also access the port directly through the domain name.
A firewall
If the extranet cannot access this interface, it is probably caused by the firewall. We need to disable the firewall or whitelist this port.
Here are some common instructions for firewalls:
Start: systemctl start firewalld
Disable: systemctl stop firewalld
Check the status: Systemctl status Firewalld
Disable: systemctl disable firewalld
Systemctl enable firewalld
Here are some port-related instructions:
Add: firewall-cmd –zone=public –add-port=80/ TCP –permanent
(–permanent takes effect permanently. If no parameter is set to permanent, the parameter will become invalid after restart.)
Reload: firewall-cmd –reload
See firewall-cmd –zone= public –query-port=80/ TCP
Delete: firewall-cmd –zone= public –remove-port=80/ TCP –permanent
Pm2 command
Here are some common PM2 commands:
Install: NPM install -g pm2
Pm2 start bin/ WWW –name
Pm2 start bin/ WWW -i x –name
Start the program: pm2 start < app_name | | id all >
List processes: pm2 list
Exit procedures: pm2 stop < app_name | | id all >
Restart application: pm2 restart < app_name | | id all >
Program information: pm2 describe id | all
Monitoring: PM2 Monit
Real-time centralized log processing: PM2 logs
Check the error log: pm2 logs < app_name | id >
API: PM2 Web (Port: 9615)
The log
If the startup fails or the service exits abnormally, troubleshoot the fault using logs.
Pm2 error logs are stored in the /root/.pm2/logs/ directory. The log file is named after the process.
By executing pM2 logs on the terminal, you can see the NodeJS console information in real time.
Many environmental
Json configuration can be used to launch projects so that different environments (development/production) can be launched
pm2 start xxx.json
Copy the code
You can configure configuration files for different environments under the NodeJS project, especially to use different ports and names for development and production environments
A simple example of a configuration file:
dev.json
{
"apps": [
{
"name": "custom-dev",
"script": "./bin/www",
"env": {
"PORT": "10008",
}
}
]
}
Copy the code
release.json
{
"apps": [
{
"name": "custom",
"script": "./bin/www",
"env": {
"PORT": "8000",
}
}
]
}
Copy the code
In the bin/ WWW entry file generated by nodejs by default, the port will get the configuration of the process env by default, as shown below
var port = normalizePort(process.env.PORT || '8000');
Copy the code
So by starting this way, different environments will use different ports.
debugging
Sometimes we need to do simple debugging on the server side, and while we can use PM2 logs to view real-time console information, if multiple projects are running on the server at the same time, there will be a lot of invalid information. We can start it directly using the Node command
node ./bin/www
Copy the code
However, this startup will directly use the interface we set up in the WWW, and this interface is likely to be the production environment interface, so we should avoid using this interface, add the port, as follows:
PORT=1234 node ./bin/www
Copy the code
This will start with port 1234