Run the following command to update your package repositories:
sudo apt update
Install Node.js using the following command:
sudo apt-get install -y nodejs
Install Nginx to act as a reverse proxy for your Node.js application:
sudo apt-get install -y nginx
Install PM2 globally to manage your Node.js application:
sudo npm i -g pm2
Navigate to the Nginx sites-available directory and open the default configuration file for editing:
cd /etc/nginx/sites-available
sudo nano default
Inside the Nginx configuration file, add the following block to configure the reverse proxy for your API:
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
After making changes to the Nginx configuration, restart Nginx to apply the changes:
sudo systemctl restart nginx
Navigate to your project directory and start your Node.js application using PM2. Replace server.js
with the actual filename of your Node.js application:
cd /path/to/your/app
pm2 start server.js --name=server
If you need to restart your Node.js application managed by PM2, you can use the following command:
pm2 restart server
These steps should help you set up a Node.js backend API with Nginx and PM2 on your Debian-based Linux system. Make sure to customize the paths and filenames according to your specific project.