# 🚀 دليل النشر على Ubuntu Server

## المتطلبات
- Ubuntu 22.04 LTS أو 24.04 LTS
- RAM: 1 GB كحد أدنى (2 GB موصى به)
- Storage: 10 GB كحد أدنى
- صلاحية root أو sudo

---

## الطريقة 1: نشر تلقائي كامل (موصى به)

### الخطوة 1 — رفع الكود على السيرفر
```bash
# من جهازك المحلي:
scp -r crypto-mining-system/ root@YOUR_SERVER_IP:/tmp/
```

أو باستخدام Git:
```bash
# على السيرفر:
git clone https://your-repo-url.git /tmp/crypto-mining-system
```

### الخطوة 2 — تشغيل المثبِّت
```bash
ssh root@YOUR_SERVER_IP
cd /tmp/crypto-mining-system
chmod +x deploy/scripts/*.sh
sudo bash deploy/scripts/install.sh
```

> **مع دومين محدد:**
> ```bash
> DOMAIN=your-domain.com sudo bash deploy/scripts/install.sh
> ```

### الخطوة 3 — إضافة مفتاح OpenRouter
```bash
sudo nano /opt/crypto-mining/backend/.env
# عدّل: OPENROUTER_API_KEY=sk-or-...
sudo systemctl restart cryptomining-api
```

### الخطوة 4 — تعبئة قاعدة البيانات
```bash
curl -X POST http://localhost:8000/seed/miners/sync
```

### الخطوة 5 (اختياري) — تفعيل SSL
```bash
# تأكد أن الدومين يشير إلى IP السيرفر أولاً
sudo bash deploy/scripts/setup_ssl.sh your-domain.com admin@your-domain.com
```

### الخطوة 6 (اختياري) — تأمين السيرفر
```bash
sudo bash deploy/scripts/server_hardening.sh
```

---

## الطريقة 2: نشر يدوي خطوة بخطوة

```bash
# 1. تحديث النظام
sudo apt-get update && sudo apt-get upgrade -y

# 2. تثبيت Python 3.11
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt-get install -y python3.11 python3.11-venv python3.11-dev

# 3. تثبيت Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs

# 4. تثبيت MySQL
sudo apt-get install -y mysql-server nginx

# 5. إنشاء قاعدة البيانات
sudo mysql < /tmp/crypto-mining-system/database/schema.sql

# 6. إعداد Backend
cd /opt/crypto-mining/backend
python3.11 -m venv venv
venv/bin/pip install -r requirements.txt
cp .env.example .env && nano .env

# 7. بناء Frontend
cd /opt/crypto-mining/frontend
npm install && npm run build

# 8. تفعيل الخدمات
sudo cp /tmp/crypto-mining-system/deploy/systemd/*.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now cryptomining-api

# 9. Nginx
sudo cp /tmp/crypto-mining-system/deploy/nginx/cryptomining.conf \
    /etc/nginx/sites-available/cryptomining
sudo ln -sf /etc/nginx/sites-available/cryptomining /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
```

---

## إدارة النظام

```bash
# حالة الخدمات
sudo bash deploy/scripts/manage.sh status

# إعادة التشغيل
sudo bash deploy/scripts/manage.sh restart

# مشاهدة الـ Logs مباشرة
sudo bash deploy/scripts/manage.sh logs

# تعبئة قاعدة البيانات
sudo bash deploy/scripts/manage.sh seed

# نسخ احتياطي يدوي
sudo bash deploy/scripts/manage.sh backup

# تحديث النظام بعد تعديل الكود
sudo bash deploy/scripts/deploy.sh
```

---

## هيكل الملفات على السيرفر

```
/opt/crypto-mining/
├── backend/
│   ├── .env              ← مفاتيح API (محمي chmod 640)
│   ├── venv/             ← Python virtual environment
│   └── logs/             ← ملفات السجل
├── frontend/
│   └── dist/             ← React build (يخدمه Nginx)
└── database/
    └── schema.sql

/etc/systemd/system/
├── cryptomining-api.service
├── cryptomining-backup.service
└── cryptomining-backup.timer

/etc/nginx/sites-available/
└── cryptomining

/opt/backups/cryptomining/
└── db_YYYYMMDD_HHMMSS.sql.gz

/root/cryptomining_credentials.txt  ← كلمات المرور (مشفر 600)
```

---

## أوامر مفيدة

```bash
# مشاهدة Logs
journalctl -u cryptomining-api -f
journalctl -u cryptomining-api --since "1 hour ago"

# فحص الصحة
curl http://localhost:8000/health | python3 -m json.tool

# Swagger API
http://YOUR_SERVER_IP/api/docs

# إحصائيات الأجهزة في قاعدة البيانات
sudo bash deploy/scripts/manage.sh miners-count

# تعديل الإعدادات وإعادة التشغيل
sudo bash deploy/scripts/manage.sh update-env

# فحص Fail2ban
sudo fail2ban-client status sshd

# تجديد SSL يدوياً
sudo certbot renew
```

---

## Ports المستخدمة

| Port | الاستخدام | مفتوح للعموم؟ |
|------|----------|--------------|
| 22   | SSH | نعم (مقيّد بـ fail2ban) |
| 80   | HTTP (Nginx) | نعم |
| 443  | HTTPS (Nginx) | نعم (بعد SSL) |
| 8000 | FastAPI | لا (localhost فقط) |
| 3306 | MySQL | لا (localhost فقط) |

---

## استكشاف الأخطاء

### Backend لا يشتغل
```bash
journalctl -u cryptomining-api -n 50 --no-pager
systemctl status cryptomining-api
```

### خطأ في قاعدة البيانات
```bash
sudo mysql -u cryptomining -p crypto_mining
SHOW TABLES;
SELECT COUNT(*) FROM miners;
```

### Nginx لا يستجيب
```bash
sudo nginx -t
sudo journalctl -u nginx -n 20
```

### منفذ 8000 لا يرد
```bash
curl http://localhost:8000/health
ss -tlnp | grep 8000
```
