# Deploy to locker.thekrtechnology.us — Step-by-Step Guide

**Domain:** locker.thekrtechnology.us
**Setup:** React SPA at `/` and Laravel API at `/api/` — same domain
**Server root:** `public_html/locker.thekrtechnology.us`

---

## What goes where on your server

```
public_html/locker.thekrtechnology.us/
├── index.html          ← React frontend (built files)
├── assets/             ← React JS/CSS (hashed filenames)
├── favicon.svg
└── backend/            ← Laravel app (entire backend folder)
    ├── app/
    ├── bootstrap/
    ├── config/
    ├── database/
    ├── public/         ← Laravel entry point (NOT the web root)
    ├── routes/
    ├── storage/
    ├── vendor/         ← created by composer install
    └── .env            ← you create this manually
```

---

## Step 1 — Build the React frontend on your Mac

Run this in your Mac Terminal:

```bash
cd "/Users/kaziramjanali/Downloads/LOCKER managemnet System for GYM/Locker Management For GYM/frontend"
npm install
npm run build
```

This creates a `frontend/dist/` folder with `index.html` + `assets/`.

---

## Step 2 — Upload files to your server

### Option A — Upload via cPanel File Manager (easiest)
1. Zip the `backend/` folder on your Mac → `backend.zip`
2. Zip the contents of `frontend/dist/` → `frontend-dist.zip`
3. In cPanel File Manager, go to `public_html/locker.thekrtechnology.us/`
4. Upload and extract `backend.zip` → you should have `backend/` folder
5. Upload and extract `frontend-dist.zip` → you should have `index.html` and `assets/` directly in root

### Option B — Upload via SCP (faster for large files)
```bash
# From your Mac Terminal

# Upload backend folder
scp -r "backend/" YOUR_USER@YOUR_SERVER_IP:~/public_html/locker.thekrtechnology.us/

# Upload frontend built files
scp -r "frontend/dist/." YOUR_USER@YOUR_SERVER_IP:~/public_html/locker.thekrtechnology.us/
```

---

## Step 3 — SSH into your server

```bash
ssh YOUR_USER@YOUR_SERVER_IP
```

---

## Step 4 — Install PHP 8.2 and required extensions (if not installed)

```bash
# Check if PHP 8.2 is installed
php8.2 --version

# If not installed:
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update
sudo apt-get install -y php8.2 php8.2-fpm php8.2-cli php8.2-mysql \
    php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip php8.2-bcmath \
    php8.2-gd php8.2-intl php8.2-tokenizer

sudo systemctl start php8.2-fpm
sudo systemctl enable php8.2-fpm
```

---

## Step 5 — Install Composer (if not installed)

```bash
# Check if Composer is installed
composer --version

# If not installed:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
```

---

## Step 6 — Install Laravel dependencies

```bash
cd ~/public_html/locker.thekrtechnology.us/backend
composer install --no-dev --optimize-autoloader
```

---

## Step 7 — Create and configure the .env file

```bash
cd ~/public_html/locker.thekrtechnology.us/backend
cp .env.example .env
nano .env
```

Change these values:

```dotenv
APP_ENV=production
APP_DEBUG=false
APP_URL=https://locker.thekrtechnology.us
APP_KEY=          ← leave blank, generated in next step

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=YOUR_DB_NAME      ← your MySQL database name
DB_USERNAME=YOUR_DB_USER      ← your MySQL username
DB_PASSWORD=YOUR_DB_PASSWORD  ← your MySQL password

SANCTUM_STATEFUL_DOMAINS=locker.thekrtechnology.us
SESSION_DOMAIN=.thekrtechnology.us
```

Save with `Ctrl+O` then `Enter`, exit with `Ctrl+X`.

---

## Step 8 — Create MySQL database

If you have cPanel, create it in **cPanel → MySQL Databases**. Or via SSH:

```bash
mysql -u root -p
```

```sql
CREATE DATABASE gymlocker CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'gymlocker'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON gymlocker.* TO 'gymlocker'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

---

## Step 9 — Run Laravel setup commands

```bash
cd ~/public_html/locker.thekrtechnology.us/backend

# Generate app key
php artisan key:generate

# Run migrations and seed default data
php artisan migrate --seed

# Cache config for production (faster)
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Create storage symlink
php artisan storage:link
```

**Default admin login after seeding:**
- Email: `admin@gymlocker.com`
- Password: `Admin@1234`

---

## Step 10 — Fix folder permissions

```bash
cd ~/public_html/locker.thekrtechnology.us/backend

sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
```

---

## Step 11 — Install and configure Nginx

### 11a. Install Nginx (if not installed)

```bash
sudo apt-get install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
```

### 11b. Find your Linux username

```bash
whoami
# e.g. returns: kaziramjan
```

### 11c. Edit the Nginx config and replace YOUR_USER

```bash
# Copy the config to Nginx
sudo cp ~/public_html/locker.thekrtechnology.us/backend/../deploy/nginx-locker.conf \
    /etc/nginx/sites-available/locker.thekrtechnology.us

# Replace YOUR_USER with your actual username (e.g. kaziramjan)
sudo sed -i 's/YOUR_USER/kaziramjan/g' /etc/nginx/sites-available/locker.thekrtechnology.us

# Enable the site
sudo ln -sf /etc/nginx/sites-available/locker.thekrtechnology.us \
    /etc/nginx/sites-enabled/locker.thekrtechnology.us

# Remove default site if it exists
sudo rm -f /etc/nginx/sites-enabled/default

# Test config and reload
sudo nginx -t
sudo systemctl reload nginx
```

---

## Step 12 — Enable SSL (HTTPS)

```bash
# Install Certbot
sudo apt-get install -y certbot python3-certbot-nginx

# Get SSL certificate (make sure your domain DNS points to this server first)
sudo certbot --nginx -d locker.thekrtechnology.us
```

Certbot automatically:
- Gets a free SSL certificate from Let's Encrypt
- Updates your Nginx config for HTTPS
- Sets up auto-renewal

---

## Step 13 — Test your site

Open your browser and go to: **https://locker.thekrtechnology.us**

You should see the login page. Login with:
- Email: `admin@gymlocker.com`
- Password: `Admin@1234`

---

## Troubleshooting

### Blank page / 404 after login
Check Nginx error log:
```bash
sudo tail -50 /var/log/nginx/locker-error.log
```

### API calls failing (500 / 502 errors)
Check Laravel log:
```bash
tail -50 ~/public_html/locker.thekrtechnology.us/backend/storage/logs/laravel.log
```

Check PHP-FPM:
```bash
sudo systemctl status php8.2-fpm
sudo systemctl restart php8.2-fpm
```

### Permission denied on storage/
```bash
cd ~/public_html/locker.thekrtechnology.us/backend
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
```

### After making code changes (redeploy manually)

```bash
cd ~/public_html/locker.thekrtechnology.us/backend

# Pull latest if using git
git pull origin main

# Re-run these after any code change
php artisan config:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan migrate --force

sudo systemctl restart php8.2-fpm
```

For frontend changes, rebuild on your Mac and re-upload the `frontend/dist/` contents.

---

## Directory path quick reference

| Item | Path on server |
|------|---------------|
| Web root | `~/public_html/locker.thekrtechnology.us/` |
| React files | `~/public_html/locker.thekrtechnology.us/index.html` |
| Laravel app | `~/public_html/locker.thekrtechnology.us/backend/` |
| Laravel .env | `~/public_html/locker.thekrtechnology.us/backend/.env` |
| Laravel logs | `~/public_html/locker.thekrtechnology.us/backend/storage/logs/` |
| Nginx config | `/etc/nginx/sites-available/locker.thekrtechnology.us` |
| Nginx logs | `/var/log/nginx/locker-*.log` |
