Skip to main content

Overview

This guide covers deploying OdontologyApp to production. The application uses SvelteKit with the adapter-auto which automatically detects and deploys to various platforms.

Prerequisites

Before deploying, ensure:

Building for Production

Create Production Build

1

Run Type Checking

Verify there are no type errors:
npm run check
2

Build the Application

Create an optimized production build:
npm run build
This command:
  • Bundles and minifies all code
  • Optimizes assets
  • Generates the production output in the build/ directory
  • Uses the adapter specified in svelte.config.js
3

Test Production Build

Preview the production build locally:
npm run preview
Test all critical functionality before deploying.

Environment Configuration

Production Environment Variables

Create a .env file for production (or set environment variables on your hosting platform):
# Database Configuration
DB_HOST=your-production-db-host
DB_USER=your-production-db-user
DB_PASS=your-secure-password
DB_NAME=ontology_db

# Optional: Node Environment
NODE_ENV=production
Security Best Practices:
  • Never commit .env files to version control
  • Use strong, unique passwords for production databases
  • Restrict database access to only your application server
  • Use environment variables provided by your hosting platform
  • Enable SSL/TLS for database connections

Database Setup

Production Database Migration

1

Create Production Database

On your production MySQL server:
CREATE DATABASE IF NOT EXISTS ontology_db;
2

Create Database User

Create a dedicated user with limited privileges:
CREATE USER 'odontology_user'@'%' IDENTIFIED BY 'secure_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON ontology_db.* TO 'odontology_user'@'%';
FLUSH PRIVILEGES;
3

Import Schema

Import the database schema:
mysql -h your-db-host -u odontology_user -p ontology_db < database.sql
4

Apply Migrations

Apply any pending migrations:
mysql -h your-db-host -u odontology_user -p ontology_db < migration_phase6_permissions.sql
mysql -h your-db-host -u odontology_user -p ontology_db < sp_finance.sql
5

Create Initial Admin User

Create an admin user for initial access:
USE ontology_db;

-- Insert branch
INSERT INTO branches (name, address, status) 
VALUES ('Main Branch', 'Main Office', 'active');

-- Insert admin user (password should be hashed with bcrypt)
INSERT INTO users (name, username, email, password, role, branch_id, status)
VALUES (
  'System Admin',
  'admin',
  '[email protected]',
  '$2a$10$...', -- Use bcrypt to hash your password
  'admin',
  1,
  'active'
);
To generate a bcrypt hash for the admin password, you can use:
node -e "console.log(require('bcryptjs').hashSync('your_password', 10))"

Deployment Options

The application uses @sveltejs/adapter-auto which supports multiple platforms:

Option 1: Node.js Server (VPS/Dedicated Server)

1

Install Node.js

Ensure Node.js 18+ is installed on your server:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
2

Clone and Build

git clone <your-repo>
cd OdontologyApp2
npm install
npm run build
3

Install Process Manager

Use PM2 to keep the app running:
sudo npm install -g pm2
pm2 start build/index.js --name odontology-app
pm2 startup
pm2 save
4

Configure Nginx (Optional)

Set up Nginx as a reverse proxy:
server {
    listen 80;
    server_name yourdomain.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;
    }
}
sudo systemctl restart nginx
5

Setup SSL with Let's Encrypt

sudo apt-get install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com

Option 2: Vercel

SvelteKit works seamlessly with Vercel:
1

Install Vercel Adapter

npm install -D @sveltejs/adapter-vercel
Update svelte.config.js:
import adapter from '@sveltejs/adapter-vercel';

export default {
  kit: {
    adapter: adapter()
  }
};
2

Deploy

npm install -g vercel
vercel
Or connect your GitHub repository in the Vercel dashboard.
3

Configure Environment Variables

Add environment variables in Vercel dashboard:
  • DB_HOST
  • DB_USER
  • DB_PASS
  • DB_NAME
Ensure your database allows connections from Vercel’s IP addresses.

Option 3: Netlify

1

Install Netlify Adapter

npm install -D @sveltejs/adapter-netlify
Update svelte.config.js:
import adapter from '@sveltejs/adapter-netlify';

export default {
  kit: {
    adapter: adapter()
  }
};
2

Deploy

npm install -g netlify-cli
netlify deploy --prod
3

Set Environment Variables

Configure in Netlify dashboard under Site settings > Environment variables.

Option 4: Docker

For containerized deployment:
1

Create Dockerfile

Create a Dockerfile in the project root:
FROM node:20-alpine AS builder

WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:20-alpine

WORKDIR /app
COPY --from=builder /app/build ./build
COPY --from=builder /app/package*.json ./
RUN npm ci --production

ENV NODE_ENV=production
EXPOSE 3000

CMD ["node", "build/index.js"]
2

Create docker-compose.yml

version: '3.8'

services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DB_HOST=db
      - DB_USER=root
      - DB_PASS=password
      - DB_NAME=ontology_db
    depends_on:
      - db
  
  db:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=ontology_db
    volumes:
      - mysql_data:/var/lib/mysql
      - ./database.sql:/docker-entrypoint-initdb.d/database.sql
    ports:
      - "3306:3306"

volumes:
  mysql_data:
3

Deploy with Docker

docker-compose up -d

Post-Deployment Tasks

1

Verify Application

  • Access the application URL
  • Test login functionality
  • Verify database connectivity
  • Check all main features
2

Monitor Logs

Check application logs for errors:
# PM2
pm2 logs odontology-app

# Docker
docker-compose logs -f app
3

Setup Monitoring

Consider implementing:
  • Error tracking (Sentry, Rollbar)
  • Performance monitoring (New Relic, DataDog)
  • Uptime monitoring (UptimeRobot, Pingdom)
  • Log aggregation (ELK Stack, Papertrail)
4

Configure Backups

Setup automated database backups:
# Create backup script
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
mysqldump -h localhost -u root -p ontology_db > backup_$DATE.sql

# Schedule with cron
0 2 * * * /path/to/backup-script.sh

Security Checklist

Before going live:

Performance Optimization

Database Optimization

-- Add indexes for frequently queried columns
CREATE INDEX idx_patients_cedula ON patients(cedula);
CREATE INDEX idx_appointments_date ON appointments(appointment_date);
CREATE INDEX idx_appointments_doctor ON appointments(doctor_id);
CREATE INDEX idx_users_username ON users(username);

-- Analyze and optimize tables
ANALYZE TABLE patients, appointments, users;
OPTIMIZE TABLE patients, appointments, users;

Application Optimization

  • Enable compression in your web server (Nginx/Apache)
  • Use CDN for static assets
  • Implement caching strategies
  • Optimize database queries (use EXPLAIN)
  • Minimize bundle size
  • Use connection pooling (already configured in db.js)

Maintenance

Regular Updates

# Update dependencies
npm update

# Check for vulnerabilities
npm audit
npm audit fix

# Rebuild and redeploy
npm run build
pm2 restart odontology-app

Database Migrations

When updating the database schema:
  1. Test migrations in staging environment first
  2. Backup production database before applying
  3. Apply migrations during low-traffic periods
  4. Verify data integrity after migration

Troubleshooting

  • Check environment variables are set correctly
  • Verify database connectivity
  • Check application logs for errors
  • Ensure correct Node.js version is installed
  • Verify build directory exists and has correct permissions
  • Verify database server is running
  • Check firewall rules allow connections
  • Verify credentials in environment variables
  • Check database user has proper permissions
  • Ensure connection pool settings are appropriate
  • Check for memory leaks in application code
  • Optimize database queries
  • Adjust connection pool size in src/lib/server/db.js
  • Consider scaling horizontally with load balancer
  • Check database query performance with EXPLAIN
  • Add database indexes where needed
  • Enable query caching
  • Use CDN for static assets
  • Monitor server resources (CPU, RAM, disk I/O)

Rollback Procedure

If issues occur after deployment:
1

Stop Current Version

pm2 stop odontology-app
2

Checkout Previous Version

git checkout <previous-commit-hash>
npm install
npm run build
3

Restore Database (if needed)

mysql -u root -p ontology_db < backup_file.sql
4

Restart Application

pm2 restart odontology-app

Next Steps

Monitoring

Set up monitoring and alerting for your production environment

Scaling

Learn about horizontal and vertical scaling strategies

Backups

Implement automated backup and disaster recovery procedures

Security

Regular security audits and penetration testing

Support

For deployment issues:

Build docs developers (and LLMs) love