In this article, We will learn how to host a Node.js or similar apps like Next.js or React! In this article, we’ll explore how to deploy a Node.js application on a Linux (Ubuntu) server using PM2 and Nginx. We will be using Ubuntu, other Linux platforms’ commands are pretty much the same, just change the format.
Step 1: Setting Up Your Ubuntu Server
Before starting, ensure your Ubuntu server is updated and has Node.js installed. If Node.js is not installed, use the following commands:
sudo apt update
sudo apt install nodejs
sudo apt install npm
Verify the installation:
node -v
npm -v
Step 2: Install PM2 for Process Management
PM2 is a process manager for Node.js applications, providing features like restarting your app if it crashes and keeping logs. To install PM2 globally, run:
sudo npm install pm2@latest -g
After installation, you can start your Node.js application with PM2:
pm2 start app.js
Replace app.js
with your application’s entry file. PM2 will now manage your Node.js app and keep it running in the background.
To save the process list and have it start on boot, use:
pm2 save
pm2 startup systemd
This ensures that PM2 starts your application automatically after a server reboot.
Step 3: Install and Configure Nginx
For webserver we will use Nginx, You can also use Apache if you have it installed.
sudo systemctl status nginx
sudo systemctl status apache2
If nothing returns then you will need to install Nginx. Check the installation process of nginx.
Step 4: Configure Nginx as a Reverse Proxy
To allow Nginx to forward HTTP requests to your Node.js application, you need to configure it as a reverse proxy. First, create a Nginx configuration file:
sudo nano /etc/nginx/sites-available/serverguy.dev
Replace the contents of this file with the following configuration, adjusting the server_name
to match your domain name:
server {
listen 80;
server_name serverguy.dev;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
This tells Nginx to forward traffic on port 80 to port 3000, where your Node.js application is running.
After editing the file, test the Nginx configuration:
sudo nginx -t
If there are no errors, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 5: Firewall Configuration (Optional)
If you have a firewall enabled, like in AWS EC2 edit the security group to ensure that traffic can pass through on ports 80 and 443 for HTTP and HTTPS. If you’re using UFW then you can configure this by running:
sudo ufw allow 'Nginx Full'
Step 6: Test Your Application
Your Node.js application should now be live, and accessible via the domain you gave in your Nginx configuration. Open a browser and navigate to http://your_domain.com
to verify that the app is running properly.
Step 7: Adding SSL
Now that our website is live we need to enable SSL for our website. See how to enable SSL with Nginx in Ubuntu with Certbot.
Conclusion
Now that we have installed pm2 and settled up a Node.js website you should have basic knowledge about how to run your node.js site with pm2. Soon we will post how to host multiple sites with pm2. If you’ve any issue regarding the process comment, ServerGuy is committed to helping developers and new devops achieve reliable deployments with modern tools.