Deployment Guide
Docker Deployment (Recommended)
Preparation
- Install Docker and Docker Compose
- Prepare external MySQL service
- Clone project code
Deployment Steps
bash
# 1. Enter project directory
cd kevi_blog-v2
# 2. Configure environment variables
cp .env.example .env
# 3. Edit .env file to configure database connection
# Note: Use container internal address for Redis URL
REDIS_URL=redis://redis:6379
# 4. Start services
docker-compose up --build -d
# 5. View logs
docker-compose logs -f
# 6. Stop services
docker-compose downConfiguration Description
docker-compose.yml contains the following services:
- web: Nuxt application
- redis: Redis cache service
External MySQL service is required.
Data Persistence
Data volume mapping:
./uploads:/app/uploads- Upload file persistence
Traditional Deployment
1. Build Application
bash
# Install dependencies
pnpm install
# Type checking
pnpm run type-check
# Code linting
pnpm run lint
# Build production version
pnpm run build2. Start Service
bash
# Use PM2 for process management
pm2 start ecosystem.config.js
# Or use Node directly
pnpm run preview3. Nginx Reverse Proxy Configuration
nginx
server {
listen 80;
server_name your-domain.com;
# Frontend application
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;
# Timeout settings
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# API endpoints
location /api {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Static file caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
proxy_pass http://localhost:3000;
expires 30d;
add_header Cache-Control "public, immutable";
}
}4. SSL Configuration (HTTPS)
bash
# Install Certbot
sudo apt-get install certbot python3-certbot-nginx
# Obtain SSL certificate
sudo certbot --nginx -d your-domain.com
# Auto renewal
sudo certbot renew --dry-runServer Environment Preparation
Install Node.js
bash
# Install Node.js 18.x using NodeSource repository
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Verify installation
node --version
npm --versionInstall MySQL
bash
# Install MySQL 8.0
sudo apt-get update
sudo apt-get install mysql-server
# Security configuration
sudo mysql_secure_installation
# Create database
mysql -u root -psql
CREATE DATABASE nuxt_blog CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'blog_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON nuxt_blog.* TO 'blog_user'@'localhost';
FLUSH PRIVILEGES;Install Redis
bash
# Install Redis
sudo apt-get install redis-server
# Start Redis
sudo systemctl start redis
sudo systemctl enable redis
# Set password
sudo nano /etc/redis/redis.confModify configuration file:
conf
requirepass your_redis_passwordRestart Redis:
bash
sudo systemctl restart redisPM2 Configuration
Create ecosystem.config.js file:
javascript
module.exports = {
apps: [{
name: 'kevi-blog',
script: 'node',
args: '.output/server/index.mjs',
cwd: '/projects/kevi_blog-v2',
instances: 'max',
exec_mode: 'cluster',
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 3000
},
env_production: {
NODE_ENV: 'production'
}
}]
}PM2 commands:
bash
# Start application
pm2 start ecosystem.config.js
# Check status
pm2 status
# View logs
pm2 logs kevi-blog
# Restart application
pm2 restart kevi-blog
# Stop application
pm2 stop kevi-blog
# Set auto-start on boot
pm2 startup
pm2 savePerformance Optimization
1. Enable Gzip Compression
Configure in nuxt.config.ts:
typescript
export default defineNuxtConfig({
nitro: {
compressPublicAssets: true
}
})2. Configure CDN
Use CDN to accelerate static resources:
typescript
export default defineNuxtConfig({
app: {
cdnURL: 'https://cdn.yourdomain.com'
}
})3. Database Optimization
- Create indexes
- Optimize query statements
- Use Redis to cache hot data
4. Enable Caching
env
CACHE_STORAGE=redisMonitoring and Logging
Application Logs
bash
# PM2 logs
pm2 logs kevi-blog --lines 100
# Docker logs
docker-compose logs -f webSystem Monitoring
Install monitoring tools:
bash
# Install htop
sudo apt-get install htop
# Install glances
sudo pip3 install glances
# Run monitoring
htop
glancesBackup Strategy
Database Backup
bash
# Create backup script
nano backup-db.shbash
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/backups"
DB_NAME="nuxt_blog"
DB_USER="blog_user"
DB_PASS="your_password"
mkdir -p $BACKUP_DIR
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME | gzip > $BACKUP_DIR/db_$DATE.sql.gz
# Keep last 7 days of backups
find $BACKUP_DIR -name "db_*.sql.gz" -mtime +7 -deleteSet scheduled task:
bash
crontab -ecron
# Backup database daily at 2 AM
0 2 * * * /path/to/backup-db.shFile Backup
bash
# Backup upload directory
tar -czf /backups/uploads_$(date +%Y%m%d).tar.gz ./uploadsTroubleshooting
1. Application Failed to Start
bash
# Check port occupancy
netstat -tlnp | grep 3000
# Check logs
pm2 logs kevi-blog2. Database Connection Failed
- Check database service status
- Verify connection configuration
- Check firewall rules
3. File Upload Failed
- Check upload directory permissions
- Verify storage configuration
- Check disk space
4. Insufficient Memory
bash
# Check memory usage
free -h
# Increase swap space
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileSecurity Recommendations
Regular System Updates
bashsudo apt-get update && sudo apt-get upgradeConfigure Firewall
bashsudo ufw allow ssh sudo ufw allow http sudo ufw allow https sudo ufw enableChange Default Passwords
- Database password
- Redis password
- Administrator account password
Enable HTTPS
- Use Let's Encrypt free certificates
- Force HTTPS redirection
Regular Backups
- Database backups
- File backups
- Configuration backups
Monitor Logs
- Regularly review application logs
- Set up anomaly alerts
Production Environment Checklist
- [ ] Changed all default passwords
- [ ] Configured strong password policy
- [ ] Enabled HTTPS
- [ ] Configured firewall
- [ ] Set up regular backups
- [ ] Configured log monitoring
- [ ] Performance optimization completed
- [ ] Stress testing passed
- [ ] SSL certificate auto-renewal ready
- [ ] Database backup scripts ready
- [ ] Monitoring system configured
- [ ] Domain DNS resolution correct
- [ ] CDN configuration completed (optional)