# Serverga chiqarish (deploy)

cPanel (shared hosting) uchun yozilgan. VPS'da ham xuddi shu tartib — farqi `docs/SERVER.md` da (Redis, Supervisor).

Repo: `https://github.com/bobobekturdiyev/esxalta-web` · loyiha papkasi serverda: `~/esxalta.uz`

---

## 0. Serverda nima bo'lishi kerak

| Kerak | Qayerda tekshiriladi |
|---|---|
| **PHP 8.4+** (tavsiya — 8.5) | cPanel → *Select PHP Version* / *MultiPHP Manager* |
| PHP kengaytmalari: `pdo_mysql`, `mbstring`, `openssl`, `curl`, `xml`, `ctype`, `fileinfo`, `tokenizer`, `bcmath`, `intl` | *Select PHP Version → Extensions* |
| **Composer** | `composer -V` (cPanel'da odatda `/opt/cpanel/composer/bin/composer`) |
| **Node.js 22+** va `npm` — frontend serverda quriladi | `node -v` · cPanel → *Setup Node.js App* yoki `nvm` |
| **MySQL 8 / MariaDB 10.6+** | cPanel → *MySQL Databases* |
| **SSH** (Terminal) | cPanel → *Terminal* yoki `ssh user@server` |
| **SSL (https)** — Telegram webhook va Payme uchun shart | cPanel → *SSL/TLS Status* → AutoSSL |

> PHP yo'li boshqacha bo'lsa (masalan 8.4): `PHP=/opt/cpanel/ea-php84/root/usr/bin/php ./deploy.sh`.
> Terminalda `php -v` boshqa versiyani ko'rsatsa — buyruqlarda doim to'liq yo'lni ishlating.

---

## 1. Birinchi o'rnatish

### 1.1. Domen

cPanel → *Domains* → `esxalta.uz` → **Document Root**: `esxalta.uz/public`
(ya'ni `/home/USER/esxalta.uz/public`). Butun loyiha emas, faqat `public/` ochiq bo'lishi shart.

Document root'ni o'zgartirib bo'lmasa (asosiy domen `public_html` bo'lsa):

```bash
rm -rf ~/public_html && ln -s ~/esxalta.uz/public ~/public_html
```

### 1.2. Baza

cPanel → *MySQL Databases*:
1. Baza yarating: `USER_esxalta`
2. Foydalanuvchi yarating: `USER_esxalta`, kuchli parol
3. Foydalanuvchini bazaga qo'shing — **ALL PRIVILEGES**

### 1.3. Kodni olish

Repo **private** bo'lsa — serverga deploy kalit qo'shing (bir marta):

```bash
ssh-keygen -t ed25519 -C "esxalta-server" -f ~/.ssh/esxalta -N ""
cat ~/.ssh/esxalta.pub
```

Chiqqan qatorni GitHub → repo → *Settings → Deploy keys → Add deploy key* ga qo'shing (faqat o'qish). So'ng:

```bash
cat >> ~/.ssh/config <<'EOF'
Host github-esxalta
    HostName github.com
    User git
    IdentityFile ~/.ssh/esxalta
EOF
git clone github-esxalta:bobobekturdiyev/esxalta-web.git ~/esxalta.uz
```

Repo **public** bo'lsa — oddiy:

```bash
git clone https://github.com/bobobekturdiyev/esxalta-web.git ~/esxalta.uz
```

### 1.4. `.env`

```bash
cd ~/esxalta.uz
cp .env.example .env
nano .env
```

To'ldiriladigan qiymatlar:

```dotenv
APP_ENV=production
APP_DEBUG=false
APP_URL=https://esxalta.uz

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=USER_esxalta
DB_USERNAME=USER_esxalta
DB_PASSWORD=...

SESSION_SECURE_COOKIE=true
LOG_STACK=daily
LOG_LEVEL=warning

# Pochta: cPanel → Email Accounts → noreply@esxalta.uz yarating → "Connect Devices" dagi SMTP ma'lumotlari
MAIL_MAILER=smtp
MAIL_SCHEME=smtps
MAIL_HOST=mail.esxalta.uz
MAIL_PORT=465
MAIL_USERNAME=noreply@esxalta.uz
MAIL_PASSWORD=...
MAIL_FROM_ADDRESS=noreply@esxalta.uz
MAIL_FROM_NAME=Esxalta

# Navbat: Supervisor yo'q — cron o'zi bo'shatadi
QUEUE_VIA_CRON=true

# Telegram: @BotFather → /newbot
TELEGRAM_BOT_TOKEN=123456:ABC...
TELEGRAM_BOT_USERNAME=esxalta_bot

# Payme: business.paycom.uz → kassa
PAYME_MERCHANT_ID=...
PAYME_TEST_KEY=...
PAYME_KEY=...
PAYME_TEST_MODE=true          # avval test, keyin false

# Oferta (bo'sh bo'lsa config/legal.php dagi qiymatlar)
LEGAL_SELLER_EMAIL=support@esxalta.uz
```

Ilova kaliti (bir marta, keyin hech qachon o'zgartirmang — saqlangan karta tokenlari shu bilan shifrlangan):

```bash
php artisan key:generate
```

### 1.5. Birinchi deploy

```bash
cd ~/esxalta.uz
chmod +x deploy.sh
./deploy.sh
```

Skript o'zi qiladi: `composer install` → `npm ci` → `npm run build` → `migrate` → **tariflar (seed)** → kesh →
**`telegram:setup`** (webhook, buyruqlar, bot tavsifi — 3 tilda).

Papka huquqlari (kerak bo'lsa):

```bash
chmod -R 775 storage bootstrap/cache
```

### 1.6. Admin

```bash
php artisan admin:create siz@pochta.uz --name="Ismingiz"
```

Kirish: `https://esxalta.uz/admin`. Keyingi adminlar — panelning o'zida (*Adminlar*).

### 1.7. Cron

cPanel → *Cron Jobs* → **Once Per Hour** (`0 * * * *`):

```
cd /home/USER/esxalta.uz && /opt/cpanel/ea-php85/root/usr/bin/php artisan schedule:run >> /dev/null 2>&1
```

Bitta qator yetadi — eslatmalar, navbat, tarif tekshiruvi va tozalash shundan ishlaydi (`docs/SERVER.md`).

### 1.8. Payme kassasi

[business.paycom.uz](https://business.paycom.uz) → kassa → sozlamalar:

| Maydon | Qiymat |
|---|---|
| Endpoint URL | `https://esxalta.uz/payme` |
| Hisob (account) maydoni | `order_id` |

1. `PAYME_TEST_MODE=true` bilan [test.paycom.uz](https://test.paycom.uz) sandbox'da barcha Merchant API testlaridan o'ting.
2. Payme kassani faollashtirgach: `.env` → `PAYME_TEST_MODE=false`, so'ng `php artisan config:cache`.
3. Saytda kichik tarifni o'zingiz to'lab ko'ring → *Admin → To'lovlar* da `paid` chiqishi kerak.

### 1.9. Tekshirish

`https://esxalta.uz/admin/health` — hamma qator yashil bo'lishi kerak. Birinchi soatda *Cron* sariq/qizil bo'ladi —
keyingi soat boshidan keyin yashillanadi. Darhol tekshirish uchun: `php artisan schedule:run`.

Qo'lda sinov:
1. Saytda eslatma qo'shing → Telegram'ni ulang → eslatmada **"Sinov xabarini yuborish"**.
2. Email ulang (kod kelishi kerak — kelmasa SMTP sozlamalari xato, *Admin → Loglar*).
3. Telegram'da `/start`, `/list`.

---

## 2. Keyingi yangilashlar

Kompyuterda:

```bash
git add -A && git commit -m "..." && git push
```

Serverda:

```bash
~/esxalta.uz/deploy.sh
```

Skript faqat o'zgargan qismlarni bajaradi (odatda bir necha soniya):
- `composer.lock` o'zgarsa — `composer install`;
- `package-lock.json` o'zgarsa — `npm ci`;
- frontend o'zgarsa — `npm run build` (avval `public/build-next` ga, so'ng bir zumda almashtiriladi — sayt to'xtamaydi);
- har doim — `migrate`, kesh, `queue:restart`;
- `TelegramSetup.php` o'zgarsa — `telegram:setup`.

Frontend'ni baribir qayta qurish: `./deploy.sh --full`.

Skript to'xtab, *"Serverda o'zgartirilgan fayllar bor"* desa — kimdir serverda faylni qo'lda o'zgartirgan.
Ko'rsatilgan faylni tekshiring (`git diff fayl`), keraksiz bo'lsa `git checkout -- fayl` va qayta ishga tushiring.

---

## 3. Orqaga qaytarish (rollback)

`deploy.sh` doim GitHub'dagi `main` ga tenglashadi — shuning uchun serverda `git reset` qilish yordam bermaydi
(keyingi deploy buzilgan kodni qaytaradi). Qaytarish **kompyuterda** qilinadi:

```bash
git log --oneline -5          # buzgan commit
git revert <commit>           # uni bekor qiluvchi yangi commit
git push
```

Serverda: `~/esxalta.uz/deploy.sh`.

Muammoli commit'da **migratsiya** bo'lgan bo'lsa — avval serverda uni qaytaring, keyin deploy:

```bash
php artisan migrate:rollback --step=1
```

Tuzatish vaqtida saytni yopib turish (foydalanuvchilarga "texnik ishlar" sahifasi; cron va admin `/admin` ham yopiladi):

```bash
php artisan down --retry=60     # yopish
php artisan up                  # ochish
```

> Katta o'zgarishlardan oldin bazaning zaxirasini oling: cPanel → *Backup* → *Download a MySQL Database Backup*.

---

## 4. Tez-tez uchraydigan muammolar

| Belgi | Yechim |
|---|---|
| Oq sahifa / 500 | `tail -50 storage/logs/laravel-*.log`; `APP_KEY` bormi; `chmod -R 775 storage bootstrap/cache` |
| Sayt ochiladi, lekin stil/JS yo'q | `public/build/manifest.json` yo'q → `./deploy.sh --full`; Node 22+ ekanini tekshiring |
| `npm ci` xato | `node -v` (22+ kerak). cPanel'da: *Setup Node.js App* → versiya tanlang, so'ng terminalda `source ~/nodevenv/.../activate` |
| Telegram javob bermayapti | `php artisan telegram:setup`; *Server holati → Telegram → Webhook* xatosiga qarang; `APP_URL` https bo'lishi shart |
| Email kelmayapti | *Admin → Loglar* (`laravel.log`); SMTP port 465 → `MAIL_SCHEME=smtps`, 587 → `smtp` |
| Eslatmalar kelmayapti | *Server holati → Cron*; cron qatori va PHP yo'li to'g'rimi; `php artisan reminders:dispatch --sync` |
| Payme "Ruxsat yo'q" (-32504) | `PAYME_TEST_MODE` va kalit mos emas (test rejimda `PAYME_TEST_KEY`, prod'da `PAYME_KEY`) |
| `.env` o'zgartirildi, lekin ta'sir qilmadi | `php artisan config:cache` (yoki `./deploy.sh`) |

Batafsil: cron, navbat, Redis, Supervisor — [SERVER.md](SERVER.md).
