Deploying Node.js applications on Linux

Deploying Node.js applications on Linux

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:

  1. Download the corresponding installation package and upload it to the server using the ftp tool;

  2. Unzip the installation package, tar -xvf node-v14.9.0-linux-x64.tar.xz;

  3. 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
    
  4. test

    node -v

3. Add daemon process

  1. Upload the written Node application to the server;

  2. 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:

    20200902145916.png

  3. Copy the configuration file to Systemd,sudo cp node-server.service /etc/systemd/system

  4. 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
    

4. Reference link

  1. Systemd startup for Node applications
  2. Systemd Getting Started Tutorial: Commands
  3. Systemd Introductory Tutorial: Practical Chapter
  4. How to start a Linux daemon
  5. Install NodeJS on CentOS7

Guess you like

Origin blog.csdn.net/wml00000/article/details/108363525