
How To Host Multiple Node.js Applications On Ubuntu 24.04 and Newer
Welcome to the Greenhost.cloud blog! Today, we’re diving into an essential topic for developers and businesses looking to maximize their server capabilities: hosting multiple Node.js applications on Ubuntu 24.04 and newer. With the rise of microservices and scalable application architectures, knowing how to efficiently manage multiple apps on a single server is crucial. Let’s explore the best practices and tools to get you started.
Why Host Multiple Node.js Applications?
Hosting multiple Node.js applications on a single server can lead to significant cost savings, efficient resource utilization, and simplified management. Instead of deploying separate servers for each application, you can leverage containerization, process management, and reverse proxy setups to run multiple apps seamlessly.
Prerequisites
Before you begin, ensure you have the following:
- A server running Ubuntu 24.04 or newer
- Root or sudo access to the server
- Node.js and npm installed on your server
- Basic understanding of the command line and Node.js applications
Step-by-Step Guide
Step 1: Install Node.js
If you haven’t already installed Node.js, follow these steps:
sudo apt update
sudo apt install nodejs npm
You can check the installation by running:
node -v
npm -v
Step 2: Create Your Applications
For demonstration purposes, let’s create two simple Node.js applications. Navigate to the directory where you want to host your applications:
mkdir ~/my-apps
cd ~/my-apps
Now, let’s create two directories for our applications:
mkdir app1 app2
Inside each directory, initialize a simple Node.js application. For example, for app1
:
cd app1
npm init -y
Add a simple server in app1/index.js
:
const http = require('http');
const hostname = 'localhost';
const port = 3001;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello from App 1
');
});
server.listen(port, hostname, () => {
console.log(`App 1 running at http://${hostname}:${port}/`);
});
Repeat the same steps for app2
, but change the port to 3002
and adjust the response message accordingly.
Step 3: Use a Process Manager
To manage multiple Node.js applications effectively, you should use a process manager like PM2. PM2 allows you to start, stop, and monitor your applications easily.
Install PM2 globally:
sudo npm install -g pm2
Next, start your applications with PM2:
pm2 start ~/my-apps/app1/index.js --name app1
pm2 start ~/my-apps/app2/index.js --name app2
You can check the status of your applications with:
pm2 list
Step 4: Set Up a Reverse Proxy
To expose your applications to the outside world, you’ll want to set up a reverse proxy. Nginx is a popular choice for this purpose. First, install Nginx:
sudo apt install nginx
Now, create a configuration file for your applications:
sudo nano /etc/nginx/sites-available/my-apps
Add the following configuration:
server {
listen 80;
location /app1 {
proxy_pass http://localhost:3001;
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;
}
location /app2 {
proxy_pass http://localhost:3002;
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;
}
}
Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/my-apps /etc/nginx/sites-enabled/
Test your Nginx configuration:
sudo nginx -t
Finally, restart Nginx:
sudo systemctl restart nginx
Step 5: Access Your Applications
Now that everything is set up, you can access your applications through your server’s IP address or domain name:
http://your_server_ip/app1
for App 1http://your_server_ip/app2
for App 2
Conclusion
Congratulations! You’ve successfully hosted multiple Node.js applications on Ubuntu 24.04 and newer. By utilizing process management with PM2 and a reverse proxy with Nginx, you can efficiently manage and scale your applications as needed.
If you have any questions or need further assistance, feel free to reach out in the comments below. Happy coding, and stay green with Greenhost.cloud!