April 28, 2025•7 min read••
Tags ▼
No tags
#tech/ubuntu/server #tech/nginx #tech/pm2
To install NVM (Node Version Manager) on your Ubuntu Server so that you can manage multiple Node.js versions, follow these steps:
First, update the package list to ensure your system is up to date:
sudo apt update
NVM requires some dependencies to work properly. Install them with this command:
sudo apt install curl
You can install NVM using a simple command that downloads the installation script and runs it. Use curl to download the script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
This will install the latest version of NVM. The script automatically adds the required settings to your .bashrc file.
After installation, you need to either close the terminal and reopen it or run the following command to load NVM into your current session:
source ~/.bashrc
To check that NVM was successfully installed, run the following command:
nvm --version
This should print the version of NVM you just installed.
With NVM installed, you can now install any version of Node.js. For example, to install Node.js 18.x:
nvm install 18
You can also install the latest LTS version of Node.js with:
nvm install --lts
To set a default version of Node.js to use, run:
nvm use 18 nvm alias default 18
Replace 18 with the version number you want to set as the default.
After installing Node.js, verify that it's working properly by checking the versions of Node.js and npm:
node --version npm --version
If you want to manage different versions of Node.js, you can list the installed versions using:
nvm ls
To switch to a different version, use:
nvm use <version>
Now you have NVM installed, and you can easily manage different Node.js versions on your Ubuntu Server. Let me know if you need help with anything else! Deploying applications on an Ubuntu server depends on the type of application you're working with. Since you mentioned you're interested in using Node.js and NVM, I'll walk you through how to deploy a basic Node.js application on your Ubuntu Server.
Since you’ve already installed NVM, you can choose which version of Node.js to use for your app. For example, to use Node.js 18, you can run:
nvm install 18 nvm use 18
If you have an existing Node.js app, upload it to your server, typically under /var/www/your-app. You can use scp, SFTP, or Git to transfer the files.
Alternatively, you can create a new app for testing. If you don’t have one, here’s a basic example:
Create a directory for your app:
mkdir /var/www/myapp cd /var/www/myapp
Initialize a new Node.js app:
npm init -y
Create an example app.js file:
nano app.js
Add this simple Express server code to app.js:
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => res.send('Hello World!')); app.listen(port, () => { console.log(`App listening at http://localhost:${port}`); });
Install Express:
npm install express
You can test your app by running it:
node app.js
Now, visit your server’s IP address on port 3000 in your browser, e.g., http://your-server-ip:3000. You should see "Hello World!" in your browser.
PM2 is a process manager for Node.js apps. It ensures that your app stays online, even after a server reboot or crash.
Install PM2 globally:
npm install -g pm2
Start your application with PM2:
pm2 start app.js
Set PM2 to start automatically on server boot:
pm2 startup
Follow the printed instructions to complete the setup.
Save the current PM2 process list:
pm2 save
Now, your Node.js app will start automatically if the server restarts.
To serve your Node.js app on port 80 (the default HTTP port), you'll need to set up Nginx as a reverse proxy. This way, users can visit your app using http://your-server-ip without specifying a port number.
Install Nginx:
sudo apt install nginx
Configure Nginx for Your App: Create a new configuration file for your app:
sudo nano /etc/nginx/sites-available/myapp
Add the following content:
server { listen 80; server_name your-domain.com; # Or use your server's IP address 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; } }
Replace your-domain.com with your domain name or server IP.
Enable the Nginx configuration:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
Test the Nginx configuration for syntax errors:
sudo nginx -t
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Now, you should be able to access your Node.js app via http://your-domain.com or http://your-server-ip without needing to specify the port.
For security, you should set up SSL on your server. You can use Let's Encrypt to get a free SSL certificate.
Install Certbot and Nginx plugin:
sudo apt install certbot python3-certbot-nginx
Obtain an SSL certificate:
sudo certbot --nginx -d your-domain.com
Follow the instructions provided by Certbot to complete the process.
Automatically renew the certificate: Certbot automatically sets up a cron job for renewal. You can verify this with:
sudo systemctl status certbot.timer
Your app should now be accessible securely via HTTPS.
You can use PM2 to monitor and manage your app. Some useful PM2 commands are:
View logs:
pm2 logs
Check running processes:
pm2 list
Restart the app:
pm2 restart app
Stop the app:
pm2 stop app
You've now deployed a Node.js app on your Ubuntu server using Nginx as a reverse proxy and PM2 as a process manager. You can also scale this setup or deploy additional apps using similar steps.
Let me know if you need further details or help with any step!
Follow on your preferred channel for new articles, notes, and experiments.