# Deploying Rethinksoft to AWS Ubuntu Server

## Prerequisites
- AWS EC2 instance running Ubuntu 20.04 or later
- SSH access to your server
- Domain name (optional but recommended)
- Basic knowledge of terminal/command line

## Step-by-Step Deployment Guide

### 1. Connect to Your AWS EC2 Instance

```bash
ssh -i your-key.pem ubuntu@your-ec2-public-ip
```

### 2. Update System Packages

```bash
sudo apt update
sudo apt upgrade -y
```

### 3. Install Node.js and npm

```bash
# Install Node.js (v18 or later recommended)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

# Verify installation
node --version
npm --version
```

### 4. Install Git (if you're cloning from GitHub)

```bash
sudo apt install -y git
```

### 5. Clone Your Repository or Upload Code

**Option A: Clone from GitHub**
```bash
cd /home/ubuntu
git clone https://github.com/your-username/rethinksoft-ai.git
cd rethinksoft-ai/ai\ website
```

**Option B: Upload files via SFTP/SCP**
```bash
# From your local machine
scp -i your-key.pem -r ./ai\ website ubuntu@your-ec2-public-ip:/home/ubuntu/
```

### 6. Install Project Dependencies

```bash
cd /home/ubuntu/ai\ website
npm install
```

### 7. Build the Next.js Application

```bash
npm run build
```

### 8. Install PM2 (Process Manager)

```bash
sudo npm install -g pm2
```

### 9. Start Your Application with PM2

```bash
pm2 start npm --name "rethinksoft" -- start

# Save PM2 configuration
pm2 save

# Create startup script
pm2 startup
# Follow the output to run the additional command
```

### 10. Install and Configure Nginx

```bash
sudo apt install -y nginx

# Start Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
```

### 11. Configure Nginx as Reverse Proxy

```bash
# Create Nginx configuration
sudo nano /etc/nginx/sites-available/rethinksoft
```

**Paste the following configuration:**
```nginx
server {
    listen 80;
    server_name your-domain.com www.your-domain.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;
        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;
    }

    # Gzip compression
    gzip on;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml+rss;
}
```

**Enable the site:**
```bash
sudo ln -s /etc/nginx/sites-available/rethinksoft /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

# Test Nginx configuration
sudo nginx -t

# Restart Nginx
sudo systemctl restart nginx
```

### 12. Set Up SSL Certificate (Let's Encrypt)

```bash
sudo apt install -y certbot python3-certbot-nginx

# Generate SSL certificate
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
```

### 13. Update Your Nginx Configuration for HTTPS

Certbot should automatically update your config, but you can verify:

```bash
sudo nano /etc/nginx/sites-available/rethinksoft
```

Ensure it includes:
```nginx
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
```

### 14. Set Up Automatic SSL Renewal

```bash
sudo certbot renew --dry-run
```

### 15. Configure AWS Security Groups

In AWS Console:
1. Go to EC2 → Security Groups
2. Edit inbound rules:
   - HTTP (80) from 0.0.0.0/0
   - HTTPS (443) from 0.0.0.0/0
   - SSH (22) from your IP only

### 16. Verify Your Deployment

```bash
# Check PM2 status
pm2 status

# Check Nginx status
sudo systemctl status nginx

# View application logs
pm2 logs rethinksoft
```

Visit your domain in a browser to verify it's working!

---

## Useful Commands

### Check if app is running on port 3000
```bash
curl http://localhost:3000
```

### View PM2 logs
```bash
pm2 logs rethinksoft
```

### Restart application
```bash
pm2 restart rethinksoft
```

### Stop application
```bash
pm2 stop rethinksoft
```

### Start application
```bash
pm2 start rethinksoft
```

### Reload Nginx
```bash
sudo systemctl reload nginx
```

### Check Nginx status
```bash
sudo systemctl status nginx
```

---

## Troubleshooting

### Port 3000 already in use
```bash
# Find process using port 3000
lsof -i :3000

# Kill the process
kill -9 <PID>
```

### Permission denied when accessing files
```bash
sudo chown -R ubuntu:ubuntu /home/ubuntu/ai\ website
```

### PM2 not starting on reboot
```bash
pm2 startup
# Follow the instructions in the output
pm2 save
```

### Nginx returning 502 Bad Gateway
- Check if PM2 is running: `pm2 status`
- Check PM2 logs: `pm2 logs rethinksoft`
- Restart PM2: `pm2 restart rethinksoft`

---

## Environment Variables

If you need environment variables (for API keys, etc.):

```bash
nano /home/ubuntu/ai\ website/.env.local
```

Add your variables:
```
NEXT_PUBLIC_API_URL=your-api-url
ANOTHER_VAR=value
```

Then restart the app:
```bash
pm2 restart rethinksoft
```

---

## Performance Optimization

### Enable Gzip Compression (already in config above)

### Increase Node.js Memory
```bash
pm2 start npm --name "rethinksoft" -- start --max-old-space-size=2048
```

### Monitor Resources
```bash
pm2 monit
```

---

## Regular Maintenance

### Check for updates
```bash
cd /home/ubuntu/ai\ website
git pull origin main
npm install
npm run build
pm2 restart rethinksoft
```

### Monitor logs
```bash
pm2 logs rethinksoft --lines 100
```

### Check server health
```bash
sudo systemctl status nginx
pm2 status
free -h  # Check memory
df -h   # Check disk space
```

---

## Summary

Your Rethinksoft website is now running on AWS Ubuntu with:
✅ Node.js runtime
✅ PM2 process management
✅ Nginx reverse proxy
✅ SSL/HTTPS (Let's Encrypt)
✅ Auto-start on server reboot
✅ Automatic SSL renewal

Your site is live and production-ready! 🚀
