SurfSmart/README.md
2025-06-19 03:34:40 +00:00

7.8 KiB

SurfSmart Deployment Guide for CentOS 9 Stream

demo

Prerequisites

  • Fresh CentOS 9 Stream installation
  • Root or sudo access
  • Domain name pointed to your server (optional, for production)

Step 1: System Update and Basic Tools

# Update system
sudo dnf update -y

# Install development tools
sudo dnf groupinstall "Development Tools" -y
sudo dnf install -y wget curl git nano vim

# Install Python 3.10 and pip
sudo dnf install -y python3.10 python3.10-devel python3.10-pip

# Create Python alias
sudo alternatives --set python3 /usr/bin/python3.10

# Install Node.js 18.x (for React frontend)
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo dnf install -y nodejs

# Install Redis (for Celery)
sudo dnf install -y redis
sudo systemctl enable redis
sudo systemctl start redis

# Install Nginx (web server)
sudo dnf install -y nginx
sudo systemctl enable nginx

Step 2: MongoDB Setup

  1. Create a free account at https://www.mongodb.com/cloud/atlas
  2. Create a cluster
  3. Get your connection string
  4. Whitelist your server's IP address

Option B: Install MongoDB Locally

# Create MongoDB repo file
sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo <<EOF
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
EOF

# Install MongoDB
sudo dnf install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod

Step 3: Create Application User and Directory

# Create app user
sudo useradd -m -s /bin/bash surfsmart

# Create app directory
sudo mkdir -p /var/www/surfsmart
sudo chown surfsmart:surfsmart /var/www/surfsmart

# Switch to app user
sudo su - surfsmart
cd /var/www/surfsmart

Step 4: Clone and Setup the Application

# Clone your repository (replace with your actual repo URL)
git clone https://github.com/yourusername/surfsmart.git .
# OR if you have the files locally, upload them to /var/www/surfsmart

# Create Python virtual environment
python3.10 -m venv backend_flask/venv
source backend_flask/venv/bin/activate

# Install Python dependencies
cd backend_flask
pip install --upgrade pip
pip install -r requirements.txt
pip install gunicorn  # Production WSGI server

# Create .env file from example
cp .env.example .env
nano .env  # Edit with your actual values

# Exit from virtual environment
deactivate
cd ..

Step 5: Frontend Build

# As surfsmart user, in /var/www/surfsmart
cd frontend_react
npm install
npm run build

# The built files will be in frontend_react/dist
cd ..

Step 6: Create Systemd Services

Exit from surfsmart user and run as root/sudo:

6.1 Gunicorn Service (Flask Backend)

sudo tee /etc/systemd/system/surfsmart-backend.service <<EOF
[Unit]
Description=SurfSmart Flask Backend
After=network.target

[Service]
User=surfsmart
Group=surfsmart
WorkingDirectory=/var/www/surfsmart/backend_flask
Environment="PATH=/var/www/surfsmart/backend_flask/venv/bin"
Environment="PYTHONPATH=/var/www/surfsmart"
ExecStart=/var/www/surfsmart/backend_flask/venv/bin/gunicorn -w 4 -b 127.0.0.1:5000 "myapp:create_app()"
Restart=always

[Install]
WantedBy=multi-user.target
EOF

6.2 Celery Worker Service

sudo tee /etc/systemd/system/surfsmart-celery.service <<EOF
[Unit]
Description=SurfSmart Celery Worker
After=network.target redis.service

[Service]
User=surfsmart
Group=surfsmart
WorkingDirectory=/var/www/surfsmart/backend_flask
Environment="PATH=/var/www/surfsmart/backend_flask/venv/bin"
Environment="PYTHONPATH=/var/www/surfsmart"
ExecStart=/var/www/surfsmart/backend_flask/venv/bin/celery -A celery_worker.celery_app:celery worker --loglevel=info
Restart=always

[Install]
WantedBy=multi-user.target
EOF

6.3 Enable and Start Services

sudo systemctl daemon-reload
sudo systemctl enable surfsmart-backend
sudo systemctl enable surfsmart-celery
sudo systemctl start surfsmart-backend
sudo systemctl start surfsmart-celery

# Check status
sudo systemctl status surfsmart-backend
sudo systemctl status surfsmart-celery

Step 7: Configure Nginx

sudo tee /etc/nginx/conf.d/surfsmart.conf <<EOF
server {
    listen 80;
    server_name your-domain.com;  # Replace with your domain or use server IP

    # Frontend (React)
    location / {
        root /var/www/surfsmart/frontend_react/dist;
        try_files \$uri \$uri/ /index.html;
    }

    # Backend API proxy
    location /api {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host \$host;
        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;
        
        # CORS headers (if needed)
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
        
        if (\$request_method = 'OPTIONS') {
            return 204;
        }
    }
}
EOF

# Test and reload Nginx
sudo nginx -t
sudo systemctl reload nginx

Step 8: Configure Firewall

# Allow HTTP and HTTPS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
# Install Certbot
sudo dnf install -y certbot python3-certbot-nginx

# Get SSL certificate (replace with your domain)
sudo certbot --nginx -d your-domain.com

# Auto-renewal
sudo systemctl enable certbot-renew.timer

Step 10: Initial Setup and Testing

# Create first user via API or MongoDB directly
# Test the application by accessing http://your-server-ip or https://your-domain.com

# Monitor logs
sudo journalctl -u surfsmart-backend -f
sudo journalctl -u surfsmart-celery -f
sudo tail -f /var/log/nginx/error.log

Security Considerations

  1. Environment Variables: Never commit .env file to git
  2. Firewall: Only open necessary ports (80, 443)
  3. MongoDB: Use strong passwords and connection encryption
  4. API Keys: Keep all API keys secure
  5. Updates: Regularly update system packages

Maintenance Commands

# Restart services
sudo systemctl restart surfsmart-backend
sudo systemctl restart surfsmart-celery
sudo systemctl restart nginx

# View logs
sudo journalctl -u surfsmart-backend -n 100
sudo journalctl -u surfsmart-celery -n 100

# Update application
cd /var/www/surfsmart
sudo -u surfsmart git pull
sudo -u surfsmart bash -c "cd backend_flask && source venv/bin/activate && pip install -r requirements.txt"
sudo -u surfsmart bash -c "cd frontend_react && npm install && npm run build"
sudo systemctl restart surfsmart-backend surfsmart-celery

Troubleshooting

  1. Backend not starting: Check logs with journalctl -u surfsmart-backend
  2. Celery issues: Ensure Redis is running: sudo systemctl status redis
  3. MongoDB connection: Verify connection string in .env file
  4. Frontend 404: Check Nginx configuration and dist folder
  5. CORS errors: Verify Nginx CORS headers configuration

Performance Optimization

  1. Gunicorn Workers: Adjust -w parameter based on CPU cores
  2. MongoDB Indexes: Create indexes for frequently queried fields
  3. Redis Memory: Configure Redis maxmemory policy
  4. Nginx Caching: Add caching headers for static assets

Backup Strategy

# Backup MongoDB (if local)
mongodump --out /backup/mongodb/$(date +%Y%m%d)

# Backup application files
tar -czf /backup/surfsmart-$(date +%Y%m%d).tar.gz /var/www/surfsmart

# Backup environment files
cp /var/www/surfsmart/backend_flask/.env /backup/surfsmart-env-$(date +%Y%m%d)