How to Deploy a Node.js App Using Apache2 on an Ubuntu Server

Deploying a Node.js application on an Apache2 server can be a great way to manage your web applications effectively. In this article, we’ll walk through the step-by-step process of deploying your Node.js app on an Ubuntu server using Apache2 as a reverse proxy.

Prerequisites

Before you start, ensure you have the following:

  • An Ubuntu server with Apache2 installed.
  • Node.js and npm (Node Package Manager) installed on your server.
  • Your Node.js application ready for deployment.

Install Node.js and npm

If you haven’t installed Node.js and npm yet, you can do so using the following commands:

sudo apt update
sudo apt install nodejs npm

You can verify the installation with:

node -v
npm -v




Prepare Your Node.js Application

Navigate to your Node.js application directory and install the necessary dependencies:

cd /path/to/your/app
npm install

Make sure your app is set to run on a specific port (e.g., 3000) in your code, typically by using the following snippet:

const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000

app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`)
})




Test Your Application

Run your Node.js application to ensure it’s working as expected:

node app.js

Visit http://your_server_ip:3000 in your web browser to check if your application is running correctly.

Install Apache2

If you haven’t installed Apache2, you can do it using:

sudo apt install apache2

Ensure Apache is running:

sudo systemctl start apache2
sudo systemctl enable apache2




Enable the Proxy Modules

To allow Apache to act as a reverse proxy, you need to enable the required modules:

sudo a2enmod proxy
sudo a2enmod proxy_http

After enabling the modules, restart Apache:

sudo systemctl restart apache2




Configure Apache to Proxy Requests

Create a new configuration file for your Node.js application:

sudo nano /etc/apache2/sites-available/your_app.conf

Add the following configuration, replacing your_domain_or_ip and 3000 with your app’s domain or IP and the port your app is running on:

<VirtualHost *:80>
    ServerName your_domain_or_ip
    ProxyRequests off
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>

Save and exit the editor.

Enable Your Site Configuration

Enable the new site configuration:

sudo a2ensite your_app.conf

And restart Apache to apply the changes:

sudo systemctl restart apache2




Run Your Node.js Application in the Background

To keep your application running in the background, use a process manager like pm2. First, install pm2 globally:

sudo npm install -g pm2

Then, start your application with pm2:

pm2 start app.js
pm2 startup
pm2 save

This ensures that your Node.js application restarts automatically if the server reboots.

Your Node.js application should now be accessible via Apache2 on your Ubuntu server. You can visit http://your_domain_or_ip in your web browser to see your app in action. By using Apache as a reverse proxy, you can take advantage of its features, such as SSL termination and request handling.