6.18 How to install and deploy a NodeJS project
Node.js is a powerful platform for creating fast and scalable network applications. Based on Google Chrome’s V8 engine, this technology allows using JavaScript on the server side, opening new possibilities for developers.
In today’s world, Node.js has become an integral part of the ecosystem, allowing developers to create everything from simple web servers to complex microservice architectures. Its event-driven architecture and non-blocking I/O make it particularly effective for real-time applications.
A significant advantage of Node.js is its huge developer community and rich ecosystem of npm (Node Package Manager) packages, which significantly accelerates the development process and implementation of new features.
What is Node.js and why is it needed?
Node.js is used for:
- Creating web servers and APIs.
- Developing real-time applications (chats, games).
- Creating command-line tools.
- Processing streaming data.
- Microservice architecture.
Important: before installing Node.js, make sure You have root
administrator rights on the server. It is also recommended to create a backup of important data before making significant changes to the system.
Installing Node.js
Node.js can be installed in several ways. The simplest method is through the operating system’s standard repositories. This method is suitable for quick start and testing but may not provide the latest version of Node.js. Let’s examine the installation example for Ubuntu/Debian and CentOS OS.
During installation, two main components will be installed:
- Node.js – JavaScript runtime environment.
- npm (Node Package Manager) – package manager that allows installing additional modules.
The simplest way to install is through standard repositories. Let’s examine the installation example for different OS.
Ubuntu/Debian:
Update package list
Install Node.js and npm
Check version:
CentOS/AlmaLinux/RockyLinux:
Update system:
Install epel-release:
Install Node.js and npm:
Check versions
We inform You:
when installing through apt
or yum
, You may not get the latest version of Node.js. If You need a specific or the latest version, it is recommended to use nvm.
Installation through nvm (Node Version Manager) provides a more flexible approach to managing Node.js versions. This tool is particularly useful when You need to:
- Work with different projects requiring different Node.js versions.
- Test code on different Node.js versions.
- Easily upgrade to new versions.
- Avoid permission issues when installing global packages.
The nvm installation process includes the following steps:
- Downloading the installation script.
- Configuring environment variables.
- Installing the required Node.js version.
Installation procedure through nvm (Node Version Manager).
Advantages of using nvm:
- Easy switching between Node.js versions.
- Ability to install latest versions.
- Isolated environments for different projects.
- Doesn’t require sudo for package installation.
Ubuntu/Debian:
Install nvm:
Reload shell profile:
View available versions:
Install latest Node.js version:
Install specific version:
Switch between versions:
Set default version:
CentOS/AlmaLinux/RockyLinux:
Install required dependencies:
Download and install nvm:
Add nvm to current session:
Reload profile:
Check nvm installation:
Install latest Node.js version:
Or install specific version:
Project Deployment
Reminder: when developing commercial projects, it is recommended to use LTS (Long Term Support) versions of Node.js to ensure stability and security.
Web Server Configuration (Nginx)
For Node.js application in production, it is recommended to use a web server as a reverse proxy. There are several popular options:
- Nginx – lightweight and high-performance web server.
- Apache – reliable and time-tested web server.
- Caddy – modern web server with automatic HTTPS support.
- HAProxy – specialized load balancing solution.
In our example, we’ll look at configuring Nginx, as one of the most popular solutions.
Installing Nginx:
Ubuntu/Debian:
CentOS/AlmaLinux/RockyLinux:
Proxy configuration
Example Nginx configuration:
It’s important to understand key configuration points:
- Proxying allows the Node.js application to work on an internal port while Nginx handles all external requests.
- WebSocket settings are necessary for real-time functionality.
- Proper log configuration will help in debugging problems.
- Timeouts protect the server from hung connections.
Reminder: in production environment, it is also recommended to configure:
- Response compression (gzip).
- Static file caching.
- SSL/TLS certificate.
- Request size limits.
Application Launch
After installing Node.js and configuring the web server, it’s necessary to ensure reliable launch and maintenance of Your application. For this, we’ll use PM2 – a process manager for Node.js applications.
PM2 provides the following capabilities:
- Automatic application restart after crashes.
- Resource and log monitoring.
- Load balancing.
- Environment variable management.
- Auto-start on server reboot.
Installing PM2:
Starting the application:
Setting up auto-start:
Additionally: after setting up auto-start through PM2, we recommend:
- Reboot the server and verify that the application started.
- Check process status through
pm2 list
. - View logs through
pm2 logs
.
The following are the main PM2 commands for managing a Node.js application.
Start application:
Stop:
Restart:
View logs:
Process monitoring:
Conclusion
Node.js provides powerful tools for creating modern web applications. With proper configuration and maintenance, Your application will work stably and efficiently. Regular component updates and performance monitoring will help ensure the security and reliability of Your service.
To ensure stable operation of Your application, it is recommended to:
- Regularly update Node.js and npm packages.
- Use logging to track errors.
- Configure system resource monitoring.
- Regularly create backups.