# Installation Guide - Mosharef Grammar School Management System

## Step-by-Step Installation

### Prerequisites
- PHP 7.4+
- MySQL 5.7+
- Web server (Apache with mod_rewrite enabled)
- FTP or SSH access to hosting

### Step 1: Upload Files

Using FTP or cPanel File Manager:
1. Create directory: `public_html/school-management`
2. Upload all project files to this directory
3. Ensure this structure exists:
```
public_html/school-management/
├── admin/
├── config/
├── database/
├── includes/
├── public/
├── login.php
└── logout.php
```

### Step 2: Create Database

**Using cPanel:**
1. Go to cPanel → MySQL Databases
2. Create database: `mosharef_school`
3. Create MySQL user: `school_user`
4. Set password for the user
5. Add all privileges to the user for this database

**Using phpMyAdmin:**
1. Click on "New" or use SQL tab
2. Execute: `CREATE DATABASE mosharef_school;`
3. Select the database
4. Create a new user with appropriate privileges

### Step 3: Import Database Schema

**Using phpMyAdmin:**
1. Select `mosharef_school` database
2. Go to "Import" tab
3. Upload `database/schema.sql` file
4. Click "Go" to execute

**Using command line:**
```bash
mysql -h localhost -u school_user -p mosharef_school < database/schema.sql
```

### Step 4: Configure the Application

**Edit `config/database.php`:**
```php
define('DB_HOST', 'localhost');
define('DB_USER', 'school_user');        // Your MySQL username
define('DB_PASSWORD', 'your_password');   // Your MySQL password
define('DB_NAME', 'mosharef_school');
define('DB_PORT', 3306);
```

**Edit `config/constants.php`:**
```php
define('SITE_NAME', 'Mosharef Grammar School');
define('SITE_URL', 'http://yourdomain.com/school-management');
define('ADMIN_EMAIL', 'admin@yourdomain.com');
define('SCHOOL_PHONE', '01XXXXXXXXX');
define('SCHOOL_ADDRESS', 'Your school address');
define('BKASH_MERCHANT_ACCOUNT', '01629772919');  // Update with your bKash merchant number
```

### Step 5: Set File Permissions

**Using SSH:**
```bash
cd /home/username/public_html/school-management

# Set directory permissions
chmod 755 config/
chmod 755 includes/
chmod 755 admin/
chmod 755 public/

# Create and set upload directories
mkdir -p uploads/student_photos
mkdir -p uploads/teacher_photos
mkdir -p uploads/staff_photos
mkdir -p uploads/documents
mkdir -p uploads/csv_import

chmod 755 uploads/
chmod 755 uploads/student_photos
chmod 755 uploads/teacher_photos
chmod 755 uploads/staff_photos
chmod 755 uploads/documents
chmod 755 uploads/csv_import
```

### Step 6: Create First Admin Account

**Method 1: Through web interface**
1. Go to `http://yourdomain.com/school-management/login.php`
2. Click "Sign Up"
3. Register as Admin with email and password
4. The account will be in "Pending" status
5. Go to database and manually approve: `UPDATE users SET status = 'approved' WHERE role = 'admin';`

**Method 2: Direct database**
```sql
INSERT INTO users (email, password, role, first_name, last_name, status) VALUES (
  'admin@school.com',
  '$2y$12$YOUR_BCRYPT_HASH_HERE',
  'admin',
  'Admin',
  'User',
  'approved'
);
```

To generate bcrypt hash (using online tool or PHP):
```php
echo password_hash('YourPassword123', PASSWORD_BCRYPT, ['cost' => 12]);
```

### Step 7: Test Installation

1. Open `http://yourdomain.com/school-management/login.php`
2. Login with your admin credentials
3. Go to Dashboard - should load without errors
4. Try creating a class to test database connection

## Subdomain Setup (Optional)

To access via `school.yourdomain.com`:

1. **Create subdomain** in cPanel → Addon Domains
2. Point to `public_html/school-management`
3. Update `SITE_URL` in `config/constants.php` to `http://school.yourdomain.com`

## SSL Certificate Setup (Highly Recommended)

1. In cPanel, go to **AutoSSL** or **Let's Encrypt SSL**
2. Install free SSL certificate
3. Update `SITE_URL` in `config/constants.php` to use `https://`

## Backup and Maintenance

### Regular Backups
```bash
# Backup database
mysqldump -u school_user -p mosharef_school > school_backup.sql

# Backup files
tar -czf school_files_backup.tar.gz school-management/
```

### Database Optimization
```sql
OPTIMIZE TABLE users, students, marks, attendance, fees;
```

## Troubleshooting Installation

### Error: "Cannot connect to database"
- Check `config/database.php` credentials
- Verify MySQL user has database privileges
- Test connection using phpMyAdmin

### Error: "Permission denied" on upload directories
- Run: `chmod 755 uploads/` and subdirectories
- Verify web server user ownership

### Blank page or 500 errors
- Check `/var/log/php.log` or cPanel error logs
- Verify all PHP extensions are enabled (mysqli, PDO)
- Check file permissions on main directory (should be 755)

### bKash merchant account showing incorrectly
- Update `BKASH_MERCHANT_ACCOUNT` in `config/constants.php`
- Clear browser cache after update
- The merchant account is displayed on the admission form

## Next Steps After Installation

1. **Create Classes**: Admin → Classes Management → Add Classes I-X
2. **Create Sections**: For each class, create sections A, B, C
3. **Add Subjects**: Admin → Subjects Management → Add all subjects
4. **Assign Subjects**: Admin → Assign Subjects → Select classes and subjects
5. **Add Teachers**: Admin → Teachers → Add teacher profiles
6. **Open Online Admissions**: Publish `public/admission.php` link publicly

## Performance Optimization

For larger schools with 1000+ students:

1. **Add database indexes** (already done in schema)
2. **Enable query caching** in MySQL
3. **Use PHP OPcache** if available
4. **Compress CSS and JS** files before deployment
5. **Implement CDN** for static assets

## Support

For installation issues:
1. Check error logs in `/home/username/public_html/school-management/`
2. Review cPanel error logs
3. Verify file permissions and ownership
4. Test with simpler operations first (like adding a class)
