Deploying Applications on VPS: A Complete Guide

October 25, 2024 (1y ago)

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

Webuzo

Manual VPS

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 nginx

Installing cPanel

# SSH into your server
ssh root@your_server_ip
 
# Download and install cPanel
cd /home
wget http://httpupdate.cpanel.net/latest
sh latest

Deploying 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 save

Nginx 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.timer

Database 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;
EOF

Running Migrations with Prisma

# Run migrations on production
npx prisma migrate deploy
 
# Generate Prisma client
npx prisma generate

Monitoring and Maintenance

Health Checks

# Monitor PM2 processes
pm2 monit
 
# View logs
pm2 logs myapp

Backup 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 -delete

Using 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 hours

Security Best Practices

  1. Firewall Configuration

    sudo ufw allow 22/tcp
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    sudo ufw enable
  2. SSH Key Authentication

    ssh-keygen -t rsa -b 4096
    # Disable password authentication in sshd_config
  3. Environment Variables

    • Never commit .env files
    • Use secrets management tools
    • Rotate keys regularly

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  # Restart

High memory usage

pm2 kill  # Stop all processes
pm2 start ecosystem.config.js --max-memory-restart 1G

SSL certificate issues

certbot renew --dry-run  # Test renewal
certbot renew  # Force renewal

Conclusion

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.