# MiMSMS Voter System - cPanel Deployment Guide

This guide will help you deploy the MiMSMS Voter System to your cPanel shared hosting.

## Prerequisites

- cPanel/WHM hosting account
- PHP 8.1+ support
- MySQL/MariaDB database
- SSH access (recommended)
- Composer installed on your hosting (or locally)

## Step 1: Prepare Your Local Environment

Before uploading to cPanel, you need to install dependencies locally:

```bash
cd /path/to/mimsms_laravel
composer install --no-dev
```

## Step 2: Upload Files to cPanel

### Option A: Using File Manager (Recommended for beginners)

1. Log in to cPanel
2. Go to **File Manager**
3. Navigate to your domain's public_html folder
4. Create a new folder called `mimsms` (or your preferred name)
5. Upload all project files to this folder
6. Extract the uploaded files if needed

### Option B: Using SSH (Recommended for developers)

```bash
# Connect to your server
ssh username@your-domain.com

# Navigate to public_html
cd public_html

# Clone or upload the project
git clone your-repo-url mimsms
# OR upload via SCP
scp -r /path/to/mimsms_laravel/* username@your-domain.com:~/public_html/mimsms/

# Install dependencies
cd mimsms
composer install --no-dev
```

## Step 3: Create Database

1. Log in to cPanel
2. Go to **MySQL Databases**
3. Create a new database (e.g., `mimsms_voter`)
4. Create a new MySQL user (e.g., `mimsms_user`)
5. Assign the user to the database with all privileges
6. Note the database name, username, and password

## Step 4: Configure Environment

1. In your project folder, copy `.env.example` to `.env`:
   ```bash
   cp .env.example .env
   ```

2. Edit `.env` file with your configuration:
   ```
   APP_NAME="MiMSMS Voter System"
   APP_ENV=production
   APP_DEBUG=false
   APP_KEY=base64:YOUR_APP_KEY_HERE
   APP_URL=https://your-domain.com/mimsms

   DB_CONNECTION=mysql
   DB_HOST=localhost
   DB_PORT=3306
   DB_DATABASE=mimsms_voter
   DB_USERNAME=mimsms_user
   DB_PASSWORD=your_database_password

   WHMCS_API_URL=https://your-whmcs-domain.com/includes/api.php
   WHMCS_API_USERNAME=admin
   WHMCS_API_KEY=your_whmcs_api_key

   ADMIN_EMAIL=admin@yourdomain.com
   ```

3. Generate application key:
   ```bash
   php artisan key:generate
   ```

## Step 5: Set Permissions

```bash
# Set proper permissions for storage and bootstrap/cache
chmod -R 755 storage
chmod -R 755 bootstrap/cache
chmod -R 644 storage/*
chmod -R 644 bootstrap/cache/*

# Make sure the directories are writable
chmod -R 777 storage
chmod -R 777 bootstrap/cache
```

## Step 6: Run Migrations

```bash
# Create all database tables
php artisan migrate

# (Optional) Seed sample data
php artisan db:seed
```

## Step 7: Configure Web Server (htaccess)

Create or update `.htaccess` file in your `public` folder:

```apache
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
```

## Step 8: Set Up Cron Job (Optional but Recommended)

For scheduled tasks, add a cron job in cPanel:

1. Go to **Cron Jobs**
2. Add a new cron job:
   ```
   */5 * * * * cd /home/username/public_html/mimsms && php artisan schedule:run >> /dev/null 2>&1
   ```

## Step 9: Test Your Installation

1. Visit your domain: `https://your-domain.com/mimsms`
2. You should see the MiMSMS Voter System home page
3. Register a new account
4. Test the area selection and voter data display
5. Create a test order

## Step 10: Create Admin User

To create an admin user, use SSH:

```bash
php artisan tinker

# In the Tinker shell:
$user = new App\Models\User();
$user->name = 'Admin User';
$user->email = 'admin@yourdomain.com';
$user->password = bcrypt('secure_password_here');
$user->role = 'admin';
$user->save();

exit
```

## Step 11: Import Sample Data

1. Log in as admin
2. Go to Admin Dashboard
3. Click "Import Voter Data"
4. Upload your Excel file with voter data

## Troubleshooting

### 500 Internal Server Error

- Check error logs: `storage/logs/laravel.log`
- Verify file permissions
- Ensure PHP version is 8.1+

### Database Connection Error

- Verify database credentials in `.env`
- Check if MySQL is running
- Ensure user has proper privileges

### Blank Page

- Check `storage/logs/laravel.log`
- Verify `APP_DEBUG=true` temporarily in `.env`
- Clear cache: `php artisan cache:clear`

### WHMCS Integration Not Working

- Verify WHMCS API credentials
- Check WHMCS API is enabled
- Review WHMCS API logs

## Maintenance

### Regular Backups

```bash
# Backup database
mysqldump -u mimsms_user -p mimsms_voter > backup_$(date +%Y%m%d).sql

# Backup files
tar -czf mimsms_backup_$(date +%Y%m%d).tar.gz /home/username/public_html/mimsms
```

### Update Dependencies

```bash
composer update
php artisan migrate
```

### Clear Cache

```bash
php artisan cache:clear
php artisan config:clear
php artisan view:clear
```

## Security Recommendations

1. Keep `.env` file outside public_html if possible
2. Disable `APP_DEBUG` in production
3. Use strong database passwords
4. Regularly update Laravel and dependencies
5. Set up SSL certificate (HTTPS)
6. Restrict admin panel access by IP if possible
7. Use strong admin passwords

## Support

For issues or questions, contact your hosting provider or refer to Laravel documentation at https://laravel.com/docs
