Teach you how to use NGINX and PM2 to deploy Node.js applications on VPS servers
Today I will introduce to you how to use NGINX and PM2 to deploy on VPS servers Node.js application. Node.js is a very popular back-end development framework, while NGINX is a high-performance reverse proxy server and PM2 is a powerful process manager. By using these three tools together, we can achieve efficient and stable server deployment.
sudo apt-get update sudo apt-get install nodejs sudo apt-get install npm
sudo npm install -g pm2
app.js
inside it. Write your application code in app.js
. For example, here is a simple Express application code: var express = require('express'); var app = express(); app.get('/', function(req, res) { res.send('Hello World!'); }); app.listen(3000, function() { console.log('App listening on port 3000!'); });
pm2 start app.js
Now your application has been launched through PM2 and will automatically restart on failure.
/etc/nginx/sites-enabled/default
, and configure it as follows: server { listen 80; server_name your-domain.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:3000; } }
In this configuration, change your Replace -domain.com
with your domain name. Then, replace 127.0.0.1:3000
with the address and port your application is running on.
sudo service nginx reload
NGINX will now send incoming requests to our Node.js application through the reverse proxy.
Congratulations! You have successfully deployed a Node.js application on a VPS server using NGINX and PM2. Now, you can access your domain name and should be able to see what your application is running.
The above is a brief guide to using NGINX and PM2 to deploy Node.js applications on VPS servers. I hope it will be helpful to you.
The above is the detailed content of Teach you how to use NGINX and PM2 to deploy Node.js applications on VPS servers. For more information, please follow other related articles on the PHP Chinese website!