Node.js Version Management with nvm is a common requirement for VPS administrators. This guide provides practical instructions that you can follow on Ubuntu 22.04/24.04 or Debian 12, though most steps apply to other distributions as well.
Prerequisites
- Git installed on the server
- A VPS running Ubuntu 22.04 or later (2GB+ RAM recommended)
- Basic familiarity with the Linux command line
- A registered domain name (for public-facing services)
- The relevant programming language runtime installed
Environment Setup
It's recommended to test this configuration in a staging environment before deploying to production. This helps identify potential compatibility issues and allows you to benchmark performance differences.
# Set up the development environment
sudo apt update
sudo apt install -y build-essential curl git
# Install the runtime
curl -fsSL https://get.nodejs.org | bash
nodejs --version
The output should show the service running without errors. If you see any warning messages, address them before proceeding to the next step.
- Monitor disk space usage and set up alerts
- Test your backup restore procedure monthly
- Review log files weekly for anomalies
- Enable automatic security updates for critical patches
- Keep your system packages updated regularly
Project Configuration
After applying these changes, monitor the server's resource usage for at least 24 hours to ensure stability. Tools like htop, iostat, and vmstat can provide real-time insights into system performance.
# Create a systemd service for the application
sudo tee /etc/systemd/system/myapp.service << 'EOF'
[Unit]
Description=My Application
After=network.target
[Service]
User=deploy
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/nodejs /opt/myapp/server.js
Restart=always
Environment=NODE_ENV=production
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
The output should show the service running without errors. If you see any warning messages, address them before proceeding to the next step.
Deployment Process
The nodejs configuration requires careful attention to resource limits and security settings. On a VPS with limited resources, it's important to tune these parameters according to your available RAM and CPU cores.
# Set up the development environment
sudo apt update
sudo apt install -y build-essential curl git
# Install the runtime
curl -fsSL https://get.nodejs.org | bash
nodejs --version
This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.
- Set up fail2ban for brute force protection
- Use SSH keys instead of password authentication
- Use strong, unique passwords for all services
Common Issues and Solutions
- Service won't start: Check the logs with
journalctl -xe -u nodejs. Common causes include port conflicts, missing configuration files, or insufficient permissions. - Slow performance: Check for disk I/O bottlenecks with
iostat -x 1and network issues withmtr. Review application logs for slow queries or requests.
Summary
You've successfully configured nodejs on your VPS. Remember to monitor performance, keep your software updated, and maintain regular backups. If you run into issues, consult the official documentation or open a support ticket for assistance.