Part 3 β Installing Node.js, npm, and PM2 on Oracle Cloud (OCI) Instance
π Overview
In the previous part, we successfully created and configured our Oracle Cloud Compute Instance, set up the Virtual Cloud Network (VCN), and connected to the server via SSH.
Now itβs time to turn that plain server into a Node.js-powered backend machine ready to run real-world JavaScript applications.
In this guide, weβll install Node.js, npm (Node Package Manager), and PM2, a powerful process manager that keeps your app running continuously β even if your server restarts.
Why Use Node.js and PM2 on OCI?
Oracle Cloudβs Always Free tier offers stable and scalable compute power β perfect for running Node.js apps that demand non-blocking, event-driven performance.
- Node.js β Handles backend logic, APIs, and real-time connections.
- npm β Manages dependencies and builds your application.
- PM2 β Keeps your Node.js app running forever and restarts it automatically if it crashes.
Together, they create a robust production environment on OCI.
π§ Step 1: Connect to Your Oracle Cloud Instance via SSH
If you havenβt already, open your terminal and connect using your private SSH key:
ssh -i ~/.ssh/id_rsa ubuntu@<your-public-ip>Once logged in, update your system packages:
sudo apt update && sudo apt upgrade -yThis ensures your Ubuntu environment has the latest security patches and dependencies.
π§± Step 2: Install Node.js and npm
There are multiple ways to install Node.js, but the NodeSource repository is the most reliable for production use.
πΉ Install Node.js LTS (Recommended)
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejsVerify your installation:
node -v
npm -vβ You should see versions like
v20.xfor Node and10.xor higher for npm.
βοΈ Step 3: Install Build Tools (Optional but Recommended)
If you plan to install packages that require compilation (like bcrypt, sharp, or native add-ons), run:
sudo apt install -y build-essentialThis ensures you have GCC and other compilation dependencies.
π§ Step 4: Create a Sample Node.js App
Letβs create a simple βHello Worldβ app to test everything.
mkdir ~/nodeapp
cd ~/nodeapp
nano app.jsPaste the following code:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Node.js running on Oracle Cloud!');
});
server.listen(3000, '0.0.0.0', () => {
console.log('Server is running on port 3000');
});Save and exit (CTRL + O, ENTER, CTRL + X).
Run the app:
node app.jsIf you see:
Server is running on port 3000π Congratulations! Your Node.js app is now live on OCI.
Now, visit http://<your-public-ip>:3000 in your browser β you should see:
Hello from Node.js running on Oracle Cloud!π Step 5: Install and Configure PM2
Running Node.js directly with node app.js works, but it stops if:
- You close your SSH session, or
- The server restarts.
Thatβs where PM2 comes in β it ensures your app always stays online.
πΉ Install PM2 globally
sudo npm install pm2@latest -gVerify installation:
pm2 -vπΉ Start your app using PM2
pm2 start app.js --name "nodeapp"List all running apps:
pm2 listYouβll see something like:
βββββββ¬ββββββββββββ¬βββββββββ¬βββββββ¬βββββββββ¬βββββββββββ
β id β name β mode β pid β status β uptime β
βββββββΌββββββββββββΌβββββββββΌβββββββΌβββββββββΌβββββββββββ€
β 0 β nodeapp β fork β 1324 β online β 0s β
βββββββ΄ββββββββββββ΄βββββββββ΄βββββββ΄βββββββββ΄βββββββββββNow your app will run 24/7, even after you close the terminal.
π Step 6: Enable PM2 Startup Script (Auto-Start on Boot)
You donβt want your app to disappear after a server reboot, right?
Run:
pm2 startup systemdIt will display a command like:
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u ubuntu --hp /home/ubuntuCopy and execute it.
Finally, save your PM2 process list:
pm2 saveβ Now your app will automatically restart on every boot.
Step 7: Managing Your App with PM2
Here are some essential commands to manage your Node.js apps with PM2:
| Command | Description |
|---|---|
pm2 start app.js | Start an app |
pm2 stop app.js | Stop the app |
pm2 restart app.js | Restart the app |
pm2 delete app.js | Remove the app |
pm2 logs | View live logs |
pm2 monit | Monitor performance |
These make it easy to handle multiple applications or APIs on the same OCI instance.
Step 8: Allow Node.js Port in Firewall (If Not Done)
To make your app accessible publicly, open port 3000 (or your custom port) in OCI Security List.
- Go to Networking β Virtual Cloud Networks β Security Lists
- Edit Ingress Rules
- Add:
- Source CIDR:
0.0.0.0/0 - IP Protocol:
TCP - Destination Port Range:
3000
- Source CIDR:
Now anyone can access your Node.js server via your OCI instance IP.
β‘ Step 9: Test from Browser or Curl
From your local computer:
curl http://<your-public-ip>:3000or open the same URL in a web browser β your βHello from Node.jsβ message should appear instantly.
π§© Step 10: Next Steps β Prepare for Frontend (React.js)
At this stage, youβve:
- Installed Node.js and npm
- Deployed a running backend server
- Configured PM2 for automatic management
- Opened firewall ports
In the next part (Part 4), weβll:
π¨ Build and deploy a React.js frontend, serve it with NGINX, and connect it with your backend through a reverse proxy.
π§ Pro Tips
- Use LTS versions of Node.js for stability in production.
- Regularly run
pm2 saveafter changes. - Use environment variables for production secrets.
- Keep your server updated (
sudo apt update && sudo apt upgrade -y). - You can also use .env files and
dotenvpackage for secure configuration.
β Summary
By completing this tutorial, youβve successfully:
- Installed Node.js and npm on your Oracle Cloud instance
- Created and tested a backend app
- Configured PM2 for automatic app management
- Set up security rules for external access
Your backend environment on OCI is now production-ready.
