# Going live on shared hosting (cPanel)

How to run School ERP on shared hosting with cPanel (Hostinger, BigRock, HostGator, GoDaddy, A2, etc.).
For your own server (VPS) use [DEPLOY.md](DEPLOY.md) instead. At the end, `php artisan erp:preflight` must pass.

Example domain below: `myerp.in`. Schools live at `https://<school>.myerp.in`, the Platform Owner panel at
`https://admin.myerp.in`. Replace `CPANELUSER` with your cPanel username.

## 1. What the hosting plan must have

Ask the host before buying, or check in cPanel:

| Need | Where to check in cPanel | Why |
|---|---|---|
| **PHP 8.2 or newer** | *Select PHP Version* / *MultiPHP Manager* | Laravel 12 |
| PHP extensions `mbstring, xml, curl, zip, intl, gd, bcmath, pdo_mysql, fileinfo, openssl` | *Select PHP Version → Extensions* | |
| **MariaDB 10.4+ or MySQL 8** | *MySQL Databases* (version shown in phpMyAdmin) | generated columns |
| **Cron jobs, every minute** (every 5 minutes also works, slower) | *Cron Jobs* | fines, DCR, reminders, backups, imports |
| Terminal or SSH (optional) | *Terminal* / *SSH Access* | not needed: the release zip and a cron job do the install |
| **Subdomains with their own document root** | *Domains* / *Subdomains* | one address per school |
| **Free SSL (AutoSSL / Let's Encrypt)** | *SSL/TLS Status* | HTTPS |
| PHP `memory_limit` 256M, `upload_max_filesize` 10M, `max_execution_time` 120 | *Select PHP Version → Options* | imports, photos |

Not needed: Redis, Node.js, a queue worker, root access.

## 2. Upload the code (no Terminal needed)

The application must **not** be inside `public_html`; only its `public` folder is served.

1. Download **`school-erp-vX.Y.Z-shared-hosting.zip`** from the latest release:
   https://github.com/trinixtech/school/releases (under *Assets*). It already contains everything (`vendor`), so
   Composer is not needed.
2. cPanel → *File Manager* → open your home folder `/home/CPANELUSER` (one level **above** `public_html`) →
   *Upload* the zip → right-click it → *Extract*. You now have `/home/CPANELUSER/school`.
3. cPanel → *MultiPHP Manager* (or *Select PHP Version*): choose **PHP 8.2 or newer** for your domain and subdomains.

*With Terminal instead:* `cd ~ && git clone https://github.com/trinixtech/school.git school && cd school && composer install --no-dev --optimize-autoloader`.

## 3. Subdomains: one per school (recommended on shared hosting)

Every subdomain gets the **same document root**: `/home/CPANELUSER/school/public`.

In cPanel → *Domains* → *Create A New Domain* (or *Subdomains*), untick "Share document root" and add:

| Subdomain | Document root |
|---|---|
| `admin.myerp.in` | `/home/CPANELUSER/school/public` |
| `abc.myerp.in` (first school) | `/home/CPANELUSER/school/public` |

**For every new school**, create its subdomain the same way **before** you create the school in the Platform panel
(it takes a minute). AutoSSL then issues its HTTPS certificate automatically (can take up to an hour; *SSL/TLS Status →
Run AutoSSL* speeds it up).

*Wildcard option:* if your host supports wildcard subdomains **and** wildcard SSL, add `*.myerp.in` once with the same
document root and new schools work immediately. Many shared hosts do not issue wildcard certificates; then use one
subdomain per school as above.

## 4. Database

1. cPanel → *MySQL Databases*: create a database (e.g. `CPANELUSER_erp`) and a user with a strong password.
2. *Add User To Database* → **ALL PRIVILEGES**.

## 5. The `.env` file

```bash
cd ~/school && cp .env.production.example .env
```

Edit `.env` (*File Manager* → right click → *Edit*). Besides the domain and database, set these for shared hosting:

```ini
APP_BASE_DOMAIN=myerp.in
APP_URL=https://admin.myerp.in
APP_SCHEME=https

DB_DATABASE=CPANELUSER_erp
DB_USERNAME=CPANELUSER_erp
DB_PASSWORD=...

# No always-on worker on shared hosting: the cron line runs queued jobs.
QUEUE_CONNECTION=database
QUEUE_VIA_SCHEDULER=true

# PHP cannot run mysqldump on most shared hosts: PHP writes the backup itself.
BACKUP_METHOD=php

SESSION_DRIVER=database
CACHE_STORE=database

# Mail: an email account created in cPanel → Email Accounts
MAIL_MAILER=smtp
MAIL_HOST=mail.myerp.in
MAIL_PORT=465
MAIL_SCHEME=smtps
MAIL_USERNAME=no-reply@myerp.in
MAIL_PASSWORD=...
MAIL_FROM_ADDRESS=no-reply@myerp.in

PLATFORM_OWNER_EMAIL=you@yourcompany.in
```

If pages keep redirecting or show "http" links behind the host's proxy or Cloudflare, add `TRUSTED_PROXIES=*`.

## 6. Install (with a cron job, no Terminal needed)

Put the first Platform Owner in `.env` before installing:

```ini
PLATFORM_OWNER_EMAIL=you@yourcompany.in
PLATFORM_OWNER_PASSWORD=a-long-password-you-choose
```

Then cPanel → *Cron Jobs* → *Add New Cron Job*, **Once Per Minute**, command:

```
cd /home/CPANELUSER/school && php artisan erp:install >> /dev/null 2>&1
```

Wait two minutes, then open `/home/CPANELUSER/school/storage/logs/install.log` in *File Manager*. It must end with
**"Done. You can delete the install cron job now."** Then **delete this cron job**.

`erp:install` creates the app key in `.env`, the database tables, plans, modules, board templates and permissions,
and the Platform Owner, then builds the caches. It is safe to run again (it never changes an existing app key), and
it is also how you **update** (step 11). No demo school is created.

**Copy the `APP_KEY` line from `.env` to a password manager now.** Without it, backups cannot be read (Aadhaar numbers,
2FA secrets and Razorpay keys are encrypted with it).

If `install.log` shows `FAILED`, the line above it says why (usually a wrong database name, user or password in `.env`).
If the log is not created at all, the cron line's `php` is older than 8.2: use the full path of PHP 8.2 from your host,
e.g. `/opt/cpanel/ea-php82/root/usr/bin/php`, in the cron lines.

`php artisan storage:link` is **not** needed: uploaded files are private and served through the app.

*With Terminal instead:* `cd ~/school && php artisan erp:install`.

## 7. The cron line (required)

cPanel → *Cron Jobs* → *Add New Cron Job*, **Once Per Minute** (`* * * * *`):

```
cd /home/CPANELUSER/school && php artisan schedule:run >> /dev/null 2>&1
```

Use the full PHP path from step 2 if `php` is not 8.2. This single line runs everything: late fines (night), DCR closing
at the Super Admin's time, Razorpay checks, reminders, backups, and large student imports (`QUEUE_VIA_SCHEDULER=true`).
If the host allows only every 5 minutes, it still works; DCRs then close up to 5 minutes late.

## 8. HTTPS everywhere

Once the certificates are issued, force HTTPS: edit `/home/CPANELUSER/school/public/.htaccess` and add right after
`RewriteEngine On`:

```apache
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
```

## 9. Backups

- `backup:database` runs every night (PHP dump, gzipped, kept 30 days) and `backup:files` every Sunday, into
  `/home/CPANELUSER/school/storage/app/backups`. Download them from *File Manager* when needed.
- Keep a copy away from this hosting (the plan asks for a different provider): set `BACKUP_COPY_DISK` to an
  S3-compatible bucket (Backblaze B2, Wasabi, AWS) or an FTP account elsewhere (see [DEPLOY.md](DEPLOY.md) §7).
- Also turn on the host's own daily backups if the plan has them (*JetBackup* / *Backup*).
- **Restore**: phpMyAdmin → select an empty database → *Import* the `.sql.gz` file (or `gunzip -c file.sql.gz | mysql ...`
  in Terminal), with the same `APP_KEY` in `.env`.

## 10. First login, schools, Razorpay

Same as [DEPLOY.md](DEPLOY.md) §6: open `https://admin.myerp.in`, log in, set up 2FA, create the schools (create their
subdomains first, step 3), and for online fees enter each school's Razorpay keys and the webhook
`https://<school>.myerp.in/fees/razorpay/webhook`. Start with Razorpay test keys.

## 11. Updating to a new version

1. Download the new `school-erp-vX.Y.Z-shared-hosting.zip` from the releases page.
2. In *File Manager*, rename `school` to `school-old` (your backup), upload and *Extract* the new zip, then copy
   **`.env`** and the folder **`storage/app`** from `school-old` into the new `school`.
3. Add the install cron job again (step 6), wait for "Done." in `storage/logs/install.log`, and delete it. It applies the
   new version's database changes and permissions.
4. Check the sites; delete `school-old` after a few days.

Do updates outside school hours.

*With Terminal:* `php artisan down && git pull && composer install --no-dev --optimize-autoloader && php artisan erp:install && php artisan up`.

The go-live check `php artisan erp:preflight` needs Terminal; without it, check by hand that the cron line runs
(late fines and DCRs appear), and that a backup file appears in `storage/app/backups` the next morning.

## Troubleshooting

| Problem | Fix |
|---|---|
| "500 Server Error" right after upload | Wrong PHP version for the domain (step 2), or `storage`/`bootstrap/cache` not writable (set folders to 755) |
| Page shows the directory list or the cPanel default page | The subdomain's document root is not `.../school/public` |
| "404 Not Found" from Laravel on a new school | The school's subdomain was not created in cPanel, or the school was not created in the Platform panel |
| Browser warns "Not secure" | AutoSSL has not issued the subdomain's certificate yet: *SSL/TLS Status → Run AutoSSL* |
| Fines, DCR or reminders never happen | The cron line is missing or uses PHP < 8.2; check *Cron Jobs* and `php artisan schedule:list` |
| A big student import stays "queued" | `QUEUE_VIA_SCHEDULER=true` is missing, or the cron line is not running |
| Backup fails with "mysqldump" | Set `BACKUP_METHOD=php` |
| Emails do not arrive | Check the cPanel email account and `MAIL_*` settings; some hosts need port 587 with `MAIL_SCHEME=null` |
