# Deploy Laravel Invoice App to cPanel

## Pre-Deployment Checklist

### 1. **Prepare Your Application Locally**

```bash
# Clear all caches
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear

# Optimize for production
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

### 2. **Update .env for Production**

Change these settings:
```env
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
```

---

## Step-by-Step Deployment

### Step 1: Create Database in cPanel

1. Login to your cPanel
2. Go to **MySQL® Databases**
3. Create a new database (e.g., `yourusername_invoices`)
4. Create a database user with password
5. Add user to database with **ALL PRIVILEGES**
6. **Note down:** database name, username, and password

### Step 2: Upload Files via File Manager or FTP

**Option A: Using cPanel File Manager (Easier)**

1. Go to **File Manager** in cPanel
2. Navigate to your home directory (usually `public_html` or above)
3. Create a folder outside public_html: `laravel_app`
4. Upload your entire project as a ZIP file
5. Extract the ZIP file
6. Delete the ZIP file

**Option B: Using FTP (Faster for large files)**

1. Use FileZilla or any FTP client
2. Connect to your server
3. Upload entire project to `/home/yourusername/laravel_app`

### Step 3: Move Public Folder Contents

Your Laravel `public` folder needs to be in the web root:

1. Go to `laravel_app/public`
2. **Move** (not copy) all files to `public_html`
3. The structure should be:
   ```
   /home/yourusername/
   ├── laravel_app/          (Laravel application)
   │   ├── app/
   │   ├── bootstrap/
   │   ├── config/
   │   ├── database/
   │   ├── resources/
   │   ├── routes/
   │   ├── storage/
   │   ├── vendor/
   │   └── public/           (will be empty after moving)
   └── public_html/          (web root - public files here)
       ├── index.php
       ├── .htaccess
       └── images/
   ```

### Step 4: Update index.php Path

Edit `public_html/index.php` to point to your Laravel app:

**Find these lines:**
```php
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
```

**Change to:**
```php
require __DIR__.'/../laravel_app/vendor/autoload.php';
$app = require_once __DIR__.'/../laravel_app/bootstrap/app.php';
```

### Step 5: Create/Update .env File

1. In `laravel_app` folder, create or edit `.env` file
2. Update with your production settings:

```env
APP_NAME="Invoice Generator"
APP_ENV=production
APP_KEY=base64:YOUR_EXISTING_APP_KEY_HERE
APP_DEBUG=false
APP_URL=https://yourdomain.com

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=yourusername_invoices
DB_USERNAME=yourusername_dbuser
DB_PASSWORD=your_database_password

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="noreply@yourdomain.com"
MAIL_FROM_NAME="Amazon.com"

SESSION_DRIVER=database
QUEUE_CONNECTION=database
```

### Step 6: Install Composer Dependencies

**Option A: If you have SSH access (Recommended)**

```bash
cd ~/laravel_app
composer install --optimize-autoloader --no-dev
```

**Option B: If no SSH access**

1. Run locally: `composer install --optimize-autoloader --no-dev`
2. Upload the `vendor` folder (this is large, may take time)

### Step 7: Set Up Permissions

Using **cPanel File Manager**:

1. Right-click `laravel_app/storage` → Change Permissions → `755`
2. Right-click `laravel_app/bootstrap/cache` → Change Permissions → `755`

Using **SSH**:
```bash
cd ~/laravel_app
chmod -R 755 storage
chmod -R 755 bootstrap/cache
```

### Step 8: Run Database Migrations

**If you have SSH access:**
```bash
cd ~/laravel_app
php artisan migrate --force
```

**If no SSH access:**

1. Go to **phpMyAdmin** in cPanel
2. Select your database
3. Import your database dump (export from local first)

### Step 9: Generate Application Key (if needed)

If you see "No application encryption key" error:

```bash
php artisan key:generate
```

Or manually add to `.env`:
```env
APP_KEY=base64:YOUR_32_CHARACTER_RANDOM_STRING
```

### Step 10: Clear Caches in Production

```bash
cd ~/laravel_app
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

---

## Common cPanel Issues & Fixes

### Issue 1: "500 Internal Server Error"

**Solution:** Check `.htaccess` in `public_html`:

```apache
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
```

### Issue 2: "File permissions error"

**Solution:**
```bash
chmod -R 755 ~/laravel_app/storage
chmod -R 755 ~/laravel_app/bootstrap/cache
```

### Issue 3: Images not loading

**Solution:** Make sure `public_html/images` folder exists with your logo files:
- `Amazon_logo.svg.webp`
- `instaband-Logowhite_-e1655787388999-1024x174.png`

### Issue 4: Routes not working (404 errors)

**Solution:** Enable `mod_rewrite` in cPanel or contact host support

### Issue 5: "No supported encrypter found"

**Solution:** Generate app key:
```bash
php artisan key:generate
```

---

## Post-Deployment Testing

### 1. Test Basic Access
- Visit: `https://yourdomain.com`
- Should show invoice list page

### 2. Test Database Connection
- Try creating a new invoice
- Should save without errors

### 3. Test Email
- Create invoice
- Click "Email Invoice"
- Check Mailtrap inbox

### 4. Test Admin Panel
- Visit: `https://yourdomain.com/admin/login`
- Login: `admin` / `password`
- Check map displays correctly

### 5. Test PDF Generation
- View any invoice
- Click "Print / Download"
- Should show Amazon-style invoice

---

## Updating Your Application

When you make changes locally and need to update production:

1. Upload changed files via FTP/File Manager
2. If `.env` changed:
   ```bash
   php artisan config:cache
   ```
3. If routes changed:
   ```bash
   php artisan route:cache
   ```
4. If views changed:
   ```bash
   php artisan view:cache
   ```
5. If database changed:
   ```bash
   php artisan migrate --force
   ```

---

## Security Recommendations

### 1. Change Admin Password

Edit `app/Http/Controllers/AdminController.php` and change:
```php
if ($request->username === 'admin' && $request->password === 'YOUR_NEW_STRONG_PASSWORD') {
```

### 2. Protect .env File

Add to `public_html/.htaccess`:
```apache
<FilesMatch "^\.env">
    Order allow,deny
    Deny from all
</FilesMatch>
```

### 3. Enable HTTPS

In cPanel:
1. Go to **SSL/TLS Status**
2. Enable AutoSSL or install Let's Encrypt certificate
3. Update `.env`: `APP_URL=https://yourdomain.com`

### 4. Hide Laravel Version

In `.env`:
```env
APP_DEBUG=false
```

---

## Alternative: Using Git Deployment (Advanced)

If your cPanel supports Git:

1. Initialize Git repo in cPanel
2. Push your code from local
3. Pull changes when updating
4. Run composer/artisan commands via SSH

---

## Need Help?

### Can't use SSH?
- Use cPanel Terminal (if available)
- Or contact your hosting provider to run commands

### Still having issues?
- Check `laravel_app/storage/logs/laravel.log` for errors
- Enable debug temporarily: `APP_DEBUG=true` in `.env`
- Check server error logs in cPanel

---

## Quick Reference

**File Locations:**
- Laravel App: `/home/yourusername/laravel_app/`
- Web Root: `/home/yourusername/public_html/`
- Logs: `laravel_app/storage/logs/laravel.log`

**Important Files:**
- `.env` - Configuration
- `index.php` - Entry point (in public_html)
- `.htaccess` - URL rewriting (in public_html)

**Key Commands:**
```bash
cd ~/laravel_app
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
chmod -R 755 storage bootstrap/cache
```

---

## Your Application URLs

After deployment:
- **Main App:** `https://yourdomain.com`
- **Create Invoice:** `https://yourdomain.com/invoices/create`
- **Admin Login:** `https://yourdomain.com/admin/login`
- **View Invoice:** `https://yourdomain.com/invoices/{id}`
- **Print Invoice:** `https://yourdomain.com/invoices/{id}/print`

---

## Success! 🎉

Your invoice application should now be live on your cPanel server!

**Test everything thoroughly before sharing with users.**
