The Tech Pulse

The Tech Pulse

Ubuntu Server - Install NVM

#tech/ubuntu/server #tech/nginx #tech/pm2

  • Install NVM
    • [[#Step 1 Update Your Package List]]
    • [[#Step 2 Install Required Dependencies]]
    • [[#Step 3 Install NVM]]
    • [[#Step 4 Load NVM into Your Current Session]]
    • [[#Step 5 Verify NVM Installation]]
    • [[#Step 6 Install a Specific Version of Node.js]]
    • [[#Step 7 Set a Default Node.js Version]]
    • [[#Step 8 Verify Node.js and NPM Installation]]
    • [[#Step 9 List Installed Versions and Switch Between Them]]
  • [[#Deploy applications on Ubuntu Server]]
    • [[#Step 1 Install Node.js (Using NVM)]]
    • [[#Step 2 Set Up Your Node.js Application]]
    • [[#Step 3 Test the Application]]
    • [[#Step 4 Install and Set Up PM2 to Manage Your Node App]]
    • [[#Step 5 Set Up Nginx as a Reverse Proxy]]
    • [[#Step 6 Set Up SSL (Optional but Recommended)]]
    • [[#Step 7 Monitor and Manage Your Application]]

To install NVM (Node Version Manager) on your Ubuntu Server so that you can manage multiple Node.js versions, follow these steps:

Step 1: Update Your Package List

First, update the package list to ensure your system is up to date:

sudo apt update

Step 2: Install Required Dependencies

NVM requires some dependencies to work properly. Install them with this command:

sudo apt install curl

Step 3: Install NVM

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.

Step 4: Load NVM into Your Current Session

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

Step 5: Verify NVM Installation

To check that NVM was successfully installed, run the following command:

nvm --version

This should print the version of NVM you just installed.

Step 6: Install a Specific Version of Node.js

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

Step 7: Set a Default Node.js Version

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.

Step 8: Verify Node.js and NPM Installation

After installing Node.js, verify that it's working properly by checking the versions of Node.js and npm:

node --version npm --version

Step 9: List Installed Versions and Switch Between Them

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>

Deploy applications on Ubuntu Server

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.

Steps to Deploy a Node.js App on Ubuntu Server

Step 1: Install Node.js (Using NVM)

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

Step 2: Set Up Your Node.js Application

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:

  1. Create a directory for your app:

    mkdir /var/www/myapp cd /var/www/myapp
  2. Initialize a new Node.js app:

    npm init -y
  3. 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}`); });
  4. Install Express:

    npm install express

Step 3: Test the Application

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.

Step 4: Install and Set Up PM2 to Manage Your Node App

PM2 is a process manager for Node.js apps. It ensures that your app stays online, even after a server reboot or crash.

  1. Install PM2 globally:

    npm install -g pm2
  2. Start your application with PM2:

    pm2 start app.js
  3. Set PM2 to start automatically on server boot:

    pm2 startup

    Follow the printed instructions to complete the setup.

  4. Save the current PM2 process list:

    pm2 save

Now, your Node.js app will start automatically if the server restarts.

Step 5: Set Up Nginx as a Reverse Proxy

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.

  1. Install Nginx:

    sudo apt install nginx
  2. 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.

  3. Enable the Nginx configuration:

    sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
  4. Test the Nginx configuration for syntax errors:

    sudo nginx -t
  5. 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.

Step 6: Set Up SSL (Optional but Recommended)

For security, you should set up SSL on your server. You can use Let's Encrypt to get a free SSL certificate.

  1. Install Certbot and Nginx plugin:

    sudo apt install certbot python3-certbot-nginx
  2. Obtain an SSL certificate:

    sudo certbot --nginx -d your-domain.com

    Follow the instructions provided by Certbot to complete the process.

  3. 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.

Step 7: Monitor and Manage Your Application

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

Conclusion

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!