Skip to content

Latest commit

 

History

History
375 lines (287 loc) · 7.28 KB

File metadata and controls

375 lines (287 loc) · 7.28 KB

استقرار بر روی میزبان ایرانی

Deployment on Iranian Host

Step 1: Choose Host & Domain

Hosting Providers (مزودان خدمات میزبانی)

  • Arvancloud (بهترین برای CDN): arvancloud.ir
  • Pardaz: pardaz.ir
  • Rayanserver: rayanserver.ir
  • Hostiran: hostiran.net
  • Tarafdari: tarafdari.com

Domain Registration (ثبت دامنه)

  • .ir domains: irannic.ir (ثبت دامنه‌های ایرانی)
  • International: Namecheap, GoDaddy (faster setup)
  • Recommended: Buy .ir domain for local credibility

Step 2: Prepare Application

2.1 Build Frontend

cd client
npm install
npm run build
# Creates dist/ folder with optimized static files

2.2 Create Production .env

# server/.env (Production)
PORT=3000
NODE_ENV=production

# MongoDB - Choose one:
# Option A: Local MongoDB on server
MONGO_URI=mongodb://127.0.0.1:27017/bodybuilding

# Option B: Hosted MongoDB Atlas (recommended)
MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/bodybuilding?retryWrites=true&w=majority

# Generate new random secret:
# node -e "console.log(require('crypto').randomBytes(48).toString('hex'))"
JWT_SECRET=<your-new-random-secret-here>
JWT_EXPIRES_IN=7d

# Update to your domain
CLIENT_ORIGIN=https://yourdomain.ir

Step 3: Setup on Server

3.1 SSH into Server

ssh user@your-server-ip

3.2 Install Dependencies

# Update system
sudo apt update && sudo apt upgrade -y

# Install Node.js (v18+)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

# Install MongoDB (if local)
sudo apt install -y mongodb

# Install PM2 (process manager)
sudo npm install -g pm2

# Install Nginx (reverse proxy)
sudo apt install -y nginx

3.3 Clone Repository

cd /opt
sudo git clone https://github.com/yourusername/bodybuilding.git
cd bodybuilding
sudo chown -R $USER:$USER .
npm install

3.4 Build for Production

npm run build

Step 4: Configure Nginx (Reverse Proxy)

Create /etc/nginx/sites-available/bodybuilding:

upstream backend {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.ir www.yourdomain.ir;

    # Redirect to HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name yourdomain.ir www.yourdomain.ir;

    # SSL Certificates (use Certbot with Let's Encrypt)
    ssl_certificate /etc/letsencrypt/live/yourdomain.ir/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.ir/privkey.pem;

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Gzip compression
    gzip on;
    gzip_types text/plain text/css application/json application/javascript;
    gzip_min_length 1000;

    # API requests -> Node.js server
    location /api/ {
        proxy_pass http://backend;
        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;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # Static files (client build)
    location / {
        root /opt/bodybuilding/client/dist;
        try_files $uri $uri/ /index.html;
    }

    # Cache static assets
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}

Enable site:

sudo ln -s /etc/nginx/sites-available/bodybuilding /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 5: SSL Certificate (HTTPS)

Using Let's Encrypt (Free)

sudo apt install -y certbot python3-certbot-nginx
sudo certbot certbot -d yourdomain.ir -d www.yourdomain.ir

This will auto-configure Nginx! Then:

sudo systemctl restart nginx

Step 6: Setup MongoDB

Option A: Local MongoDB

sudo systemctl start mongodb
sudo systemctl enable mongodb

Option B: MongoDB Atlas (Recommended)

  1. Go to mongodb.com/cloud/atlas
  2. Create free cluster
  3. Get connection string
  4. Use in .env as MONGO_URI

Step 7: Deploy with PM2

cd /opt/bodybuilding

# Start server with PM2
pm2 start server/server.js --name bodybuilding

# Make PM2 start on reboot
pm2 startup
pm2 save

# Monitor
pm2 monit
pm2 logs bodybuilding

Step 8: Setup Auto-Reload on New Deployment

Create deployment script deploy.sh:

#!/bin/bash
cd /opt/bodybuilding
git pull origin main
npm install
npm run build
pm2 restart bodybuilding
sudo systemctl restart nginx
echo "✅ Deployed successfully"

Make executable:

chmod +x deploy.sh

Run to deploy:

./deploy.sh

Step 9: Domain Setup

  1. Register domain on irannic.ir or international registrar

  2. Point DNS to your server IP:

    • Type: A
    • Host: @
    • Value: your-server-ip
    • TTL: 3600
  3. Add DNS records:

    A      @      your-server-ip
    A      www    your-server-ip
    
  4. Wait 24-48 hours for DNS propagation


Step 10: Environment Variables Checklist

Before going live, verify:

# SSH into server and check
ssh user@your-server-ip
cat /opt/bodybuilding/server/.env

# Should have:
✅ PORT=3000
✅ NODE_ENV=production
✅ MONGO_URI=<valid connection>
✅ JWT_SECRET=<long random string>
✅ CLIENT_ORIGIN=https://yourdomain.ir

Monitoring & Maintenance

View Logs

pm2 logs bodybuilding
sudo tail -f /var/log/nginx/access.log

Monitor Server

htop
df -h

Backup MongoDB

mongodump --db bodybuilding --out /backups/bodybuilding-$(date +%Y%m%d)

Auto-Backup (Cron Job)

sudo crontab -e
# Add: 0 2 * * * mongodump --db bodybuilding --out /backups/bodybuilding-$(date +\%Y\%m\%d)

Troubleshooting

Site not loading

# Check Nginx
sudo nginx -t
sudo systemctl restart nginx

# Check Node.js process
pm2 status
pm2 logs bodybuilding

# Check firewall
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 22/tcp

CORS errors

Update server/.env:

CLIENT_ORIGIN=https://yourdomain.ir

Then restart: pm2 restart bodybuilding

Slow performance

# Restart server
pm2 restart bodybuilding

# Check logs
pm2 logs bodybuilding --lines 100

# Check MongoDB
mongo
> db.stats()

Estimated Costs (Monthly)

  • Hosting: 50,000 - 200,000 ریال
  • Domain (.ir): 100,000 - 150,000 ریال/سال
  • MongoDB Atlas: Free (or $10+ for larger data)
  • SSL: Free (Let's Encrypt)

Final Checklist

  • Server rented and SSH access working
  • Node.js v18+ installed
  • Nginx installed and configured
  • MongoDB running (local or Atlas)
  • Domain registered and DNS pointing to server
  • SSL certificate installed
  • .env configured with production values
  • npm run build produces client/dist
  • PM2 managing Node.js process
  • yourdomain.ir loads without CORS errors

Questions? DM icwt.life@gmail.com