Introduction
Install Node.js is very easy in ubuntu server.Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.
In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 server.Node.js is a powerful and versatile runtime environment that allows developers to execute JavaScript code on the server side.
Installing Node.js on the latest version of Ubuntu is straightforward. You can use several methods, but here’s a common and reliable way using the NodeSource repository. Follow these steps,
Step 1: Update Your Package Index
First, open a terminal and update your package index to ensure you have the latest information on available packages.
sudo apt update
Step 2: Install Required Packages
You’ll need curl
to fetch the installation script. If you don’t have it installed, you can do so with below command
sudo apt install curl
Step 3: Download and Install Node.js
NodeSource maintains a repository for Node.js. You can choose the version you want to install (e.g., Node.js 16 or 18). Here’s how to install Node.js 18
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Then, install Node.js:
sudo apt install -y nodejs
Step 4: Verify the Installation
To check if Node.js and npm (Node Package Manager) are installed correctly, run
node -v npm -v
Step 5: Install Build Tools (Optional)
If you plan to compile and install native add-ons from npm, it’s a good idea to install build tools
sudo apt install -y build-essential
To host your Node.js application on Ubuntu, you’ll need to set up a few things beyond just installing Node.js. Here’s a step-by-step guide to help you get started
Step 1: Prepare Your Application
Make sure your Node.js application is ready. If you haven’t created one yet, you can create a simple app as follows
Create a project directory
mkdir myapp cd myapp
Initialize a new Node.js project
npm init -y
create a simple server in index.js
const http = require('http'); const hostname = '0.0.0.0'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Install PM2 (Process Manager)
PM2 is a popular process manager for Node.js applications that helps keep your app running. Install it globally
sudo npm install -g pm2
Step 3: Start Your Application with PM2
Run your application using PM2:
pm2 start index.js --name myapp
Step 4: Set Up PM2 to Run on Startup
To ensure PM2 starts your app on server reboot, run
pm2 startup pm2 save
Step 5: Configure a Reverse Proxy with Nginx
Using Nginx as a reverse proxy will help manage incoming traffic to your Node.js application.
Install Nginx:
sudo apt install nginx
Configure Nginx: Create a new configuration file for your application:
sudo nano /etc/nginx/sites-available/myapp
Add the following configuration, replacing your_domain_or_IP
with your domain name or server IP address:
server { listen 80; server_name your_domain_or_IP; location / { proxy_pass http://localhost:3000; # The port your app is running on 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; } }
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
Test Nginx configuration:
sudo nginx -t
Restart Nginx:
sudo systemctl restart nginx
Step 6: Open Your Firewall (If Applicable)
If you have a firewall running (like UFW), allow HTTP traffic
sudo ufw allow 'Nginx Full'
Step 7: Access Your Application
Now, you should be able to access your application by navigating to http://your_domain_or_IP
in your web browser.
Conclusion
Your Node.js application is now hosted on your Ubuntu server! If you have any further questions or need more details on specific steps, feel free to ask.
To know how to install nodejs in windows for beginner’s click here.