Deploying Node.js applications on Linux
Article directory
1. Background description
Deploy the application developed based on Node.js to the Linux system to provide external services. It is required that the application can start automatically at boot, just like some Windows services, so as to avoid the need to manually start the service through commands after each system restart.
2. Environmental preparation
Operating system: centOS 7
The process of installing Node.js is as follows:
-
Download the corresponding installation package and upload it to the server using the ftp tool;
-
Unzip the installation package,
tar -xvf node-v14.9.0-linux-x64.tar.xz
; -
Create a soft link so that it can be found
node、npm等命令
;ln -s /app/software/node-v14.9.0-linux-x64/bin/node /usr/bin/node ln -s /app/software/node-v14.9.0-linux-x64/bin/npm /usr/bin/npm ln -s /app/software/node-v14.9.0-linux-x64/bin/npx /usr/bin/npx
-
test
node -v
3. Add daemon process
-
Upload the written Node application to the server;
-
Add configuration file
node-server.service
;[Unit] Description=node server [Service] ExecStart=/usr/bin/node /app/software/Node/index.js Restart=always User=root Group=root Environment=PATH=/usr/bin:/usr/local/bin Environment=NODE_ENV=production WorkingDirectory=/app/software/Node [Install] WantedBy=multi-user.target
The following 5 changes are made according to the actual situation:
-
Copy the configuration file to Systemd,
sudo cp node-server.service /etc/systemd/system
-
start service
# 重载配置文件 $ sudo systemctl daemon-reload # 启动服务 $ sudo systemctl start node-server # 查看状态 $ sudo systemctl status node-server # 查看日志 $ sudo journalctl -u node-server # 实时输出最新日志 $ sudo journalctl --follow -u node-server