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
Run Type Checking
Verify there are no type errors:
Build the Application
Create an optimized production 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
Test Production Build
Preview the production build locally: 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
Create Production Database
On your production MySQL server: CREATE DATABASE IF NOT EXISTS ontology_db;
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;
Import Schema
Import the database schema: mysql -h your-db-host -u odontology_user -p ontology_db < database.sql
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
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)
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
Clone and Build
git clone < your-rep o >
cd OdontologyApp2
npm install
npm run build
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
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
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:
Install Vercel Adapter
npm install -D @sveltejs/adapter-vercel
Update svelte.config.js: import adapter from '@sveltejs/adapter-vercel' ;
export default {
kit: {
adapter: adapter ()
}
} ;
Deploy
npm install -g vercel
vercel
Or connect your GitHub repository in the Vercel dashboard.
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
Install Netlify Adapter
npm install -D @sveltejs/adapter-netlify
Update svelte.config.js: import adapter from '@sveltejs/adapter-netlify' ;
export default {
kit: {
adapter: adapter ()
}
} ;
Deploy
npm install -g netlify-cli
netlify deploy --prod
Set Environment Variables
Configure in Netlify dashboard under Site settings > Environment variables.
Option 4: Docker
For containerized deployment:
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" ]
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 :
Post-Deployment Tasks
Verify Application
Access the application URL
Test login functionality
Verify database connectivity
Check all main features
Monitor Logs
Check application logs for errors: # PM2
pm2 logs odontology-app
# Docker
docker-compose logs -f app
Setup Monitoring
Consider implementing:
Error tracking (Sentry, Rollbar)
Performance monitoring (New Relic, DataDog)
Uptime monitoring (UptimeRobot, Pingdom)
Log aggregation (ELK Stack, Papertrail)
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:
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:
Test migrations in staging environment first
Backup production database before applying
Apply migrations during low-traffic periods
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
Database connection errors
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
Rollback Procedure
If issues occur after deployment:
Checkout Previous Version
git checkout < previous-commit-has h >
npm install
npm run build
Restore Database (if needed)
mysql -u root -p ontology_db < backup_file.sql
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: