Getting nodejs right from the start saves hours of debugging later. In this comprehensive guide, we'll cover everything from initial setup to production-ready configuration, including production and optimization considerations.
Prerequisites
- Access to system monitoring tools (htop, iostat)
- Current baseline performance metrics for comparison
- A registered domain name (for public-facing services)
- Basic familiarity with the Linux command line
Baseline Measurement
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.
# Kernel tuning: /etc/sysctl.d/99-performance.conf
cat << 'EOF' | sudo tee /etc/sysctl.d/99-performance.conf
# Network performance
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
# Memory management
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
# File descriptors
fs.file-max = 2097152
fs.nr_open = 2097152
EOF
sudo sysctl --system
This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.
Performance Considerations
The production component plays a crucial role in the overall architecture. Understanding how it interacts with nodejs will help you make better configuration decisions.
Kernel and OS Tuning
When scaling this setup, consider vertical scaling (adding more RAM/CPU) first, as it's simpler to implement. Horizontal scaling adds complexity but may be necessary for high-traffic applications.
# Benchmark before and after optimization
# CPU benchmark
sysbench cpu --cpu-max-prime=20000 run
# Memory benchmark
sysbench memory --memory-block-size=1M --memory-total-size=10G run
# Disk I/O benchmark
sysbench fileio --file-total-size=4G --file-test-mode=rndrw prepare
sysbench fileio --file-total-size=4G --file-test-mode=rndrw run
sysbench fileio --file-total-size=4G cleanup
This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.
- Maintain runbooks for common operations
- Document all configuration changes
- Set up monitoring before going to production
- Test disaster recovery procedures regularly
Application-Level Optimization
For production deployments, consider implementing high availability by running multiple instances behind a load balancer. This approach provides both redundancy and improved performance under heavy load.
# Kernel tuning: /etc/sysctl.d/99-performance.conf
cat << 'EOF' | sudo tee /etc/sysctl.d/99-performance.conf
# Network performance
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
# Memory management
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
# File descriptors
fs.file-max = 2097152
fs.nr_open = 2097152
EOF
sudo sysctl --system
Note that file paths may vary depending on your Linux distribution. The examples here are for Debian/Ubuntu; adjust paths accordingly for RHEL/CentOS-based systems.
- Start with the minimum required resources
- Use connection pooling for database connections
- Profile before optimizing - measure first
- Implement caching at every appropriate layer
- Scale vertically before scaling horizontally
Caching Strategy
If you encounter issues during setup, check the system logs first. Most problems can be diagnosed by examining the output of journalctl or the application-specific log files in /var/log/.
# Benchmark before and after optimization
# CPU benchmark
sysbench cpu --cpu-max-prime=20000 run
# Memory benchmark
sysbench memory --memory-block-size=1M --memory-total-size=10G run
# Disk I/O benchmark
sysbench fileio --file-total-size=4G --file-test-mode=rndrw prepare
sysbench fileio --file-total-size=4G --file-test-mode=rndrw run
sysbench fileio --file-total-size=4G cleanup
Each line in the configuration serves a specific purpose. The comments explain the reasoning behind each setting, making it easier to customize for your specific use case.
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.