Home  >  Article  >  Web Front-end  >  How to deploy nodejs on the server and automatically start it

How to deploy nodejs on the server and automatically start it

PHPz
PHPzOriginal
2023-05-18 09:32:37948browse

In the current era of cloud computing and Web application development, automatic deployment of servers has become an inevitable problem. As an efficient and fast server-side framework, Node.js's deployment method is even more widely used. This article will introduce you how to deploy Node.js on the server and start it automatically.

1. Server environment preparation

First of all, we need a server environment with Node.js installed. Here we take the Ubuntu system server as an example. If Node.js has not been installed on your server, please install it through the following command: (This article uses Node.js version 10 as an example)

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

After the installation is complete, you can view Node.js through the following command Version:

node -v

2. Project preparation

Before deploying Node.js to the server, we need to prepare our project. First, we need to copy the project files from local to server. Here we use the scp command to copy the local project files to the server. Assume that our project file is located locally in the /home/user/project/ directory, and we want to copy it to the server's /usr/local directory. Execute the following command in the local terminal:

scp -r /home/user/project/ username@server:/usr/local/

Among them, -r means recursively copy all files, username is the user name of the server, and server is the server IP address or host name.

3. Install pm2

After the project is ready, we need to install a tool to manage the Node.js process-pm2. pm2 has functions such as automatically restarting processes, monitoring running conditions, and multi-process management. If your project has multiple processes that need to be managed, using pm2 will make the management simpler and clearer. Execute the following command on the server to install pm2:

npm install -g pm2

4. Configure pm2 to automatically start

After pm2 has been installed, we need to create an ecosystem.config.js file of pm2 on the server. (Can be created in the project root directory), the content is as follows:

module.exports = {
  apps : [{
    name        : 'app_name', //应用名称
    script      : 'app.js', //应用入口文件
    log_date_format: 'YYYY-MM-DD HH:mm:ss',
    error_file : '/var/log/app_name/err.log', //错误日志路径
    out_file   : '/var/log/app_name/out.log', //输出日志路径
    pid_file   : '/var/run/app_name.pid', //pid文件路径
    instances  : 2, //进程数
    autorestart: true, //自动重启
    watch      : true, //监听文件变化,自动重启
    max_memory_restart: '1G', //最大内存限制达到指定值后,自动重启
    env: {
      NODE_ENV: 'production' //环境变量
    },
  }],
};

Among them, the application name, application entry file and other information need to be modified according to the actual situation. Note: The error log path, output log path, and pid file path need to ensure that the relevant directories are created on the server.

In addition, we also need to create the startup script of pm2 through the following command:

sudo pm2 startup systemd

The above command will execute a script that automatically saves and guides the pm2 process, and the generated configuration file is / etc/systemd/system/pm2.service. After executing the above command, the terminal will output a piece of text. Copy this text and execute it in the terminal to complete the configuration of automatically starting pm2.

5. Start the application

After the above steps are completed, we can use the following command to start the application:

cd /usr/local/project_folder
pm2 start ecosystem.config.js

Among them, project_folder is the project folder, economy.config .js is the pm2 configuration file just created. After executing the above command, the terminal will output the application running information. You can view the application process information through the following command:

pm2 list

You can also restart or stop the application through the following command:

pm2 restart app_name
pm2 stop app_name

Finally, after all the above After the configuration is completed, the Node.js application will be automatically started after the server is restarted, and the running status of the application will be monitored through pm2.

This article introduces how to deploy Node.js on the server and realize automatic startup. By using pm2 to manage the Node.js process, the server management process can be greatly simplified and the reliability and stability of the application can be improved.

The above is the detailed content of How to deploy nodejs on the server and automatically start it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn