Deploying Applications on VPS: A Complete Guide
Moving from local development to production requires understanding server management. This guide covers deploying on VPS using cPanel, Webuzo panels, and manual configuration.
Understanding Your Hosting Options
cPanel
- User-friendly control panel
- Automatic SSL certificates (AutoSSL)
- Built-in FTP, email, and database management
- Suitable for beginners to intermediate users
Webuzo
- Lightweight alternative to cPanel
- Lower resource consumption
- Easier server setup
- Better for budget-conscious teams
Manual VPS
- Full control and customization
- Higher performance potential
- Steeper learning curve
- Requires Linux knowledge
Setting Up a VPS
Initial Server Configuration
# Update system
sudo apt update && sudo apt upgrade -y
# Create deploy user
sudo useradd -m -s /bin/bash deploy
sudo usermod -aG sudo deploy
# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Install Nginx
sudo apt install -y nginxInstalling cPanel
# SSH into your server
ssh root@your_server_ip
# Download and install cPanel
cd /home
wget http://httpupdate.cpanel.net/latest
sh latestDeploying Node.js Applications
Using PM2 for Process Management
# Install PM2 globally
npm install -g pm2
# Start your application
pm2 start server.js
# Configure startup script
pm2 startup
pm2 saveNginx Configuration
# /etc/nginx/sites-available/myapp
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000;
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 HTTPS with Let's Encrypt
# Install Certbot
sudo apt install -y certbot python3-certbot-nginx
# Get SSL certificate
sudo certbot certonly --nginx -d example.com
# Auto-renewal
sudo systemctl enable certbot.timerDatabase Deployment
MySQL Setup
# Install MySQL
sudo apt install -y mysql-server
# Secure installation
sudo mysql_secure_installation
# Create database
mysql -u root -p << EOF
CREATE DATABASE myapp;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON myapp.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
EOFRunning Migrations with Prisma
# Run migrations on production
npx prisma migrate deploy
# Generate Prisma client
npx prisma generateMonitoring and Maintenance
Health Checks
# Monitor PM2 processes
pm2 monit
# View logs
pm2 logs myappBackup Strategy
#!/bin/bash
# backup.sh - Daily backup script
BACKUP_DIR="/home/backups"
DB_NAME="myapp"
DATE=$(date +%Y%m%d_%H%M%S)
# Backup database
mysqldump -u root -p$DB_PASSWORD $DB_NAME > "$BACKUP_DIR/db_$DATE.sql"
# Backup application files
tar -czf "$BACKUP_DIR/app_$DATE.tar.gz" /var/www/myapp
# Keep only last 7 days
find $BACKUP_DIR -type f -mtime +7 -deleteUsing Cron for Automated Tasks
# Add to crontab -e
0 2 * * * /home/deploy/backup.sh # Daily backup at 2 AM
0 */6 * * * curl https://example.com/health-check # Health check every 6 hoursSecurity Best Practices
-
Firewall Configuration
sudo ufw allow 22/tcp sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw enable -
SSH Key Authentication
ssh-keygen -t rsa -b 4096 # Disable password authentication in sshd_config -
Environment Variables
- Never commit
.envfiles - Use secrets management tools
- Rotate keys regularly
- Never commit
Performance Optimization
| Setting | Value |
|---|---|
| Worker Processes | CPU cores |
| Worker Connections | 1024+ |
| Gzip Compression | on |
| Browser Cache | 30d |
Troubleshooting Common Issues
Application won't start
pm2 logs myapp # Check logs
pm2 delete myapp && pm2 start ecosystem.config.js # RestartHigh memory usage
pm2 kill # Stop all processes
pm2 start ecosystem.config.js --max-memory-restart 1GSSL certificate issues
certbot renew --dry-run # Test renewal
certbot renew # Force renewalConclusion
Deploying on VPS requires attention to detail, but the flexibility and control are worth it. Whether using cPanel for simplicity or manual configuration for control, proper setup ensures reliable, secure applications in production.