# Linux VPS Production Deployment Guide
## Ubuntu / Debian + Apache / Nginx + PHP 8.2+ (PHP-FPM) + MySQL 8.0

This guide details the complete, production-grade deployment process for **PeopleFlow HRIS** on a Linux Virtual Private Server (VPS) such as DigitalOcean, AWS EC2, Linode, Hetzner, or Contabo.

---

## 1. Server Prerequisites & System Update

Connect via SSH to your VPS:
```bash
ssh root@your_server_ip
```

Update system packages:
```bash
sudo apt update && sudo apt upgrade -y
```

Install common utilities:
```bash
sudo apt install -y curl git unzip zip software-properties-common ufw
```

---

## 2. Install PHP 8.2 or 8.3 & Required Extensions

Add the Ondrej Surý PPA for official PHP builds:
```bash
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
```

Install PHP 8.2 (or 8.3) with PHP-FPM and mandatory Laravel extensions:
```bash
sudo apt install -y php8.2 php8.2-fpm php8.2-mysql php8.2-common php8.2-mbstring \
php8.2-xml php8.2-bcmath php8.2-curl php8.2-zip php8.2-gd php8.2-cli php8.2-intl
```

Verify installation:
```bash
php -v
sudo systemctl status php8.2-fpm
```

---

## 3. Install Composer

```bash
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
composer --version
```

---

## 4. Install & Configure MySQL 8.0

Install MySQL Server:
```bash
sudo apt install -y mysql-server
sudo systemctl start mysql.service
```

Run security installation:
```bash
sudo mysql_secure_installation
```

Log in to MySQL and provision a dedicated database and user:
```sql
sudo mysql

CREATE DATABASE peopleflow_hris CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'peopleflow_user'@'localhost' IDENTIFIED BY 'StrongEnterprisePassword123#';
GRANT ALL PRIVILEGES ON peopleflow_hris.* TO 'peopleflow_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

---

## 5. Upload Codebase & Permissions

Deploy the codebase to `/var/www/peopleflow_hris`:
```bash
# Create directory
sudo mkdir -p /var/www/peopleflow_hris
sudo chown -R $USER:www-data /var/www/peopleflow_hris

# Copy project files into /var/www/peopleflow_hris
# (Via Git clone or rsync/scp)
git clone <your-private-git-repo> /var/www/peopleflow_hris
```

Set appropriate Linux permissions:
```bash
cd /var/www/peopleflow_hris
sudo chown -R www-data:www-data /var/www/peopleflow_hris/storage /var/www/peopleflow_hris/bootstrap/cache
sudo chmod -R 775 /var/www/peopleflow_hris/storage /var/www/peopleflow_hris/bootstrap/cache
```

---

## 6. Configure Environment & Dependencies

Install PHP vendor packages without development dependencies:
```bash
cd /var/www/peopleflow_hris
composer install --no-dev --optimize-autoloader
```

Set up `.env`:
```bash
cp .env.example .env
nano .env
```

Ensure production values are configured:
```ini
APP_NAME="PeopleFlow HRIS"
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL=https://hris.yourcompany.com

APP_TIMEZONE="Asia/Dhaka"
APP_CURRENCY="BDT"

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=peopleflow_hris
DB_USERNAME=peopleflow_user
DB_PASSWORD=StrongEnterprisePassword123#

SESSION_DRIVER=database
CACHE_DRIVER=file
QUEUE_CONNECTION=database
```

Generate application key, run database migrations, and seed initial corporate data:
```bash
php artisan key:generate
php artisan migrate --force --seed
php artisan storage:link
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

---

## 7. Web Server Configuration

### Option A: Nginx (Recommended)

Create virtual host file `/etc/nginx/sites-available/peopleflow.conf`:
```nginx
server {
    listen 80;
    server_name hris.yourcompany.com;
    root /var/www/peopleflow_hris/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;
    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

Enable configuration and restart Nginx:
```bash
sudo ln -s /etc/nginx/sites-available/peopleflow.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```

---

## 8. SSL Certificate via Let's Encrypt (HTTPS)

```bash
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d hris.yourcompany.com
```

---

## 9. Cron Job for Scheduled Tasks (Attendance & Reminders)

Edit crontab for user `www-data`:
```bash
sudo crontab -u www-data -e
```
Add the Laravel schedule runner:
```cron
* * * * * cd /var/www/peopleflow_hris && php artisan schedule:run >> /dev/null 2>&1
```

---

## 10. Automated Backups Script

Create a daily backup script `/root/backup_hris.sh`:
```bash
#!/bin/bash
BACKUP_DIR="/root/backups"
mkdir -p $BACKUP_DIR
DATE=$(date +"%Y%m%d_%H%M%S")

# Dump MySQL database
mysqldump -u peopleflow_user -p'StrongEnterprisePassword123#' peopleflow_hris | gzip > "$BACKUP_DIR/db_$DATE.sql.gz"

# Archive storage files
tar -czf "$BACKUP_DIR/storage_$DATE.tar.gz" -C /var/www/peopleflow_hris storage/app

# Delete backups older than 30 days
find $BACKUP_DIR -type f -mtime +30 -delete
```
Make executable and schedule in root crontab:
```bash
chmod +x /root/backup_hris.sh
(crontab -l 2>/dev/null; echo "0 2 * * * /root/backup_hris.sh") | crontab -
```
