Databáze řešení

Node.js Server Print

  • 0

How to Install a Node.js Server on Ubuntu 24.04 LTS

This guide explains how to manually install a simple Node.js web server on an Ubuntu 24.04 LTS server. These are the same steps that our automated Ansible playbook performs behind the scenes.

Estimated Time: 10-15 minutes

Requirements:

  • Ubuntu 24.04 LTS VPS
  • A user account with sudo privileges
  • Internet connection

Step 1 - Update the Package List

Update your server's package list to ensure you install the latest software.

sudo apt update

Step 2 - Install Required Packages

Install the packages needed to add the official Node.js repository.

sudo apt install -y curl ca-certificates gnupg

Step 3 - Create the APT Keyrings Directory

Create the directory that stores repository signing keys.

sudo mkdir -p /etc/apt/keyrings

Step 4 - Download the NodeSource GPG Key

Download the official NodeSource repository signing key.

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key -o /tmp/nodesource.gpg

Step 5 - Convert the Key to a Keyring

Convert the downloaded key into a format Ubuntu uses.

sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg /tmp/nodesource.gpg

Step 6 - Add the NodeSource Repository

Create a new repository file.

echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Step 7 - Update the Package List Again

Refresh the package list so Ubuntu knows about the new repository.

sudo apt update

Step 8 - Install Node.js

Install the latest Node.js package.

sudo apt install -y nodejs

Step 9 - Verify the Installation

Check the installed Node.js version.

node --version

Example output:

v20.20.2

Check the installed npm version.

npm --version

Step 10 - Create the Application Directory

Create a folder for your Node.js application.

sudo mkdir -p /opt/nodejs-app

Step 11 - Create package.json

Create the application's package file.

sudo nano /opt/nodejs-app/package.json

Paste the following:

{
  "name": "nodejs-app",
  "version": "1.0.0",
  "main": "app.js"
}

Save the file.


Step 12 - Create the Application

Create the application source file.

sudo nano /opt/nodejs-app/app.js

Paste the following:

const http = require('http');

const server = http.createServer((req, res) => {

    res.writeHead(200, {
        'Content-Type': 'text/html'
    });

    res.end(`

Hello from Node.js!

This application was deployed manually.


Server: Ubuntu

Runtime: Node.js






`);
});

server.listen(3000, '0.0.0.0', () => {
    console.log('Server running on port 3000');
});

Save the file.


Step 13 - Create the Systemd Service

Create a service so the application starts automatically.

sudo nano /etc/systemd/system/nodejs-app.service

Paste the following:

[Unit]
Description=Node.js Application
After=network.target

[Service]
User=www-data
WorkingDirectory=/opt/nodejs-app
ExecStart=/usr/bin/node /opt/nodejs-app/app.js
Restart=always

[Install]
WantedBy=multi-user.target

Save the file.


Step 14 - Reload Systemd

sudo systemctl daemon-reload

Step 15 - Enable the Service

sudo systemctl enable nodejs-app

Step 16 - Start the Service

sudo systemctl start nodejs-app

Step 17 - Verify the Service

sudo systemctl status nodejs-app

You should see:

Active: active (running)

Step 18 - Allow Port 3000 Through the Firewall

If UFW is enabled, allow traffic to the application.

sudo ufw allow 3000/tcp
sudo ufw reload

Step 19 - Test Locally

Verify the application is running.

curl http://127.0.0.1:3000

You should receive the HTML page.


Step 20 - Open the Website

Open your web browser and visit:

http://YOUR_SERVER_IP:3000

Replace YOUR_SERVER_IP with your VPS IP address.


Congratulations!

You have successfully installed a Node.js web server on Ubuntu 24.04 LTS. Your application is now running as a system service and will automatically start whenever the server reboots.


Byla tato odpověď nápomocná?
Back