# Deploying BiznisOS to biznisos.com

This guide puts the app on one Ubuntu 24.04 VPS (4 vCPU / 8 GB recommended) at `https://app.biznisos.com`.
Run the commands as a user with `sudo`. Replace every `CHANGE_ME` value with your own.

> Shared hosting (cPanel) is not suitable: later phases need queue workers, cron, websockets and wildcard SSL.

## 1. Point the domain at the server

In GoDaddy → My Products → biznisos.com → DNS, add:

| Type | Name | Value |
| --- | --- | --- |
| A | `app` | your server's IP address |
| A | `*` | your server's IP address (for funnel subdomains, Phase 3) |

DNS changes can take up to an hour.

## 2. Install the server software

```bash
sudo apt update && sudo apt upgrade -y
sudo apt install -y software-properties-common unzip git curl
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
sudo apt install -y nginx mysql-server redis-server supervisor \
  php8.2-fpm php8.2-cli php8.2-mysql php8.2-sqlite3 php8.2-mbstring php8.2-xml \
  php8.2-curl php8.2-zip php8.2-gd php8.2-bcmath php8.2-intl php8.2-redis
curl -sS https://getcomposer.org/installer | php && sudo mv composer.phar /usr/local/bin/composer
```

## 3. Create the database

```bash
sudo mysql
```
```sql
CREATE DATABASE biznisos CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'biznisos'@'localhost' IDENTIFIED BY 'CHANGE_ME_strong_password';
GRANT ALL PRIVILEGES ON biznisos.* TO 'biznisos'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

## 4. Upload the code

```bash
sudo mkdir -p /var/www/biznisos && sudo chown $USER:www-data /var/www/biznisos
# Either clone your Git repository:
git clone CHANGE_ME_your_repo_url /var/www/biznisos
# or upload the ZIP and unzip it into /var/www/biznisos

cd /var/www/biznisos
composer install --no-dev --optimize-autoloader
cp .env.example .env
php artisan key:generate
nano .env        # fill in the values from SETUP_REQUIRED.md
php artisan migrate --force
php artisan storage:link
php artisan config:cache && php artisan route:cache && php artisan view:cache
sudo chown -R $USER:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
```

## 5. Configure Nginx

Create `/etc/nginx/sites-available/biznisos`:

```nginx
# Lead-capture forms (/f/...) must be embeddable on other websites; everything else must not be.
map $request_uri $frame_options {
    ~^/f/   "";
    default "SAMEORIGIN";
}

server {
    listen 80;
    server_name app.biznisos.com;
    root /var/www/biznisos/public;
    index index.php;
    client_max_body_size 20M;

    add_header X-Frame-Options $frame_options;
    add_header X-Content-Type-Options "nosniff";

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

    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; }
}
```

```bash
sudo ln -s /etc/nginx/sites-available/biznisos /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx
```

## 6. Turn on HTTPS

```bash
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d app.biznisos.com
```

## 7. Queue worker and scheduler

Create `/etc/supervisor/conf.d/biznisos-worker.conf`:

```ini
[program:biznisos-worker]
command=php /var/www/biznisos/artisan queue:work --sleep=3 --tries=3 --max-time=3600
user=www-data
numprocs=2
process_name=%(program_name)s_%(process_num)02d
autostart=true
autorestart=true
stopwaitsecs=3600
stdout_logfile=/var/www/biznisos/storage/logs/worker.log
```

```bash
sudo supervisorctl reread && sudo supervisorctl update
(sudo crontab -u www-data -l 2>/dev/null; echo "* * * * * cd /var/www/biznisos && php artisan schedule:run >> /dev/null 2>&1") | sudo crontab -u www-data -
```

## 8. Backups

```bash
sudo mkdir -p /var/backups/biznisos
(sudo crontab -l 2>/dev/null; echo "0 2 * * * mysqldump biznisos | gzip > /var/backups/biznisos/db-\$(date +\%F).sql.gz && find /var/backups/biznisos -mtime +14 -delete") | sudo crontab -
```

Copy the backup folder off the server regularly (for example to S3 or Google Drive).

## 9. Check it works

1. Open https://app.biznisos.com and create an account.
2. Invite a second email address and accept the invitation.
3. Try the email-code login and confirm the email arrives (needs real SMTP settings).
4. Check `storage/logs/laravel.log` for errors.

## Updating to a new version

```bash
cd /var/www/biznisos
php artisan down
git pull                     # or upload the new files
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache && php artisan route:cache && php artisan view:cache
sudo supervisorctl restart biznisos-worker:*
php artisan up
```

## Funnel pages and customer domains (Phase 3)

1. In GoDaddy DNS add an **A record** `pages` → your server IP, and set `BIZNISOS_PAGES_URL=https://pages.biznisos.com` in `.env`.
2. Add `pages.biznisos.com` to the Nginx `server_name` line and run `sudo certbot --nginx -d pages.biznisos.com`.
3. **Customer domains need automatic SSL.** Certbot cannot do this on demand, so put **Caddy** in front of Nginx for ports 80/443 (or replace Nginx with Caddy). Example `/etc/caddy/Caddyfile`:

```
{
    on_demand_tls {
        ask http://127.0.0.1:8080/internal/caddy/ask
    }
}

app.biznisos.com, pages.biznisos.com {
    reverse_proxy 127.0.0.1:8080
}

https:// {
    tls {
        on_demand
    }
    reverse_proxy 127.0.0.1:8080
}
```

   Then make Nginx listen on `127.0.0.1:8080` instead of port 80, and remove Certbot's 443 blocks. Caddy only issues a certificate for domains that BiznisOS has verified.

## Importing the database instead of migrating

You can import `database/biznisos.sql` into an empty database in phpMyAdmin (Import → choose file → Go) instead of running `php artisan migrate`. Do one or the other, not both.
