DEV Community

Cover image for From FTP Deploys and Cron Scripts to a Real Pipeline
Muhammad Ali Mughal
Muhammad Ali Mughal

Posted on

From FTP Deploys and Cron Scripts to a Real Pipeline

Quick answer: Replace FTP deploys with Git-based atomic releases: build in CI, upload to a new timestamped release directory, symlink shared storage and .env, run migrations, then switch a "current" symlink. Move crontab entries one by one into Laravel's scheduler with locks and logging. Keep the old path working until each piece is proven.

In Part 4 we moved auth and sessions without logging anyone out. The next thing that tends to break legacy modernization is not the code. It is how the code reaches the server. If production is "whatever someone last uploaded with FileZilla", you cannot reason about what is running, you cannot roll back, and the new Laravel modules from Parts 1โ€“3 end up deployed the same fragile way.

I am a Principal Software Engineer in Karachi, and at Loop Origin we inherit a lot of these apps. I wrote this with help from AI tools and checked the commands against the Deployer, GitHub Actions and Laravel scheduler documentation.

๐Ÿ“š Series Navigation โ€” Modernizing Legacy PHP Without a Rewrite

Related earlier posts: Backups That Never Restored: Automated Restore Drills ยท Zero-Downtime Database Migrations in Laravel & Node.js SaaS

Old rack-mounted hardware unit with trailing cables, AI-generated

Step 0: find out what production actually is

Before changing anything, I take an inventory. Production often differs from any repository you were given.

# On the server, read-only
cd /var/www/legacy-app
find . -type f -newer composer.json -not -path "./storage/*" | head -50   # files edited after last known deploy
crontab -l -u www-data > ~/cron-inventory.txt
sudo ls /etc/cron.d/ >> ~/cron-inventory.txt
php -v; php -m > ~/php-modules.txt
~~~{% endraw %}

Then I download a full copy of the web root and diff it against the repository. Every difference is either a hotfix that never got committed (commit it) or junk like {% raw %}`index_old2.php`{% endraw %} (note it and remove it later). Uploaded user files and generated caches get listed separately; they must never live inside the code directory again.

## FTP deploys vs a real pipeline

| Concern | FTP / manual upload | Atomic release pipeline |
|---|---|---|
| What is running? | Unknown mix of files | One Git commit SHA per release |
| Half-uploaded state | Yes, for minutes | No, symlink switch is instant |
| Rollback | Re-upload old files from memory | Point symlink to previous release |
| Dependencies | Vendor folder uploaded by hand | {% raw %}`composer install --no-dev`{% endraw %} in CI |
| Secrets | Edited live in config.php | Shared .env outside releases |
| Audit trail | None | CI run, commit, who approved |

## The release directory layout

This is the layout I use on plain VPS servers. It works for old procedural PHP and Laravel alike.{% raw %}

~~~text
/var/www/app/
  current -> releases/20261122-0715-a1b2c3d
  releases/
    20261121-1930-9f8e7d6/
    20261122-0715-a1b2c3d/
  shared/
    .env
    storage/
    public/uploads/
~~~{% endraw %}

Each release is a fresh checkout. Shared folders and the env file are symlinked into it. Nginx points at {% raw %}`/var/www/app/current/public`{% endraw %}, and PHP-FPM is reloaded after the switch so OPcache drops stale paths.

## The CI pipeline

I build artifacts in CI, not on the server. Here is a trimmed GitHub Actions workflow:{% raw %}

~~~yaml
name: deploy
on:
  push:
    branches: [main]
jobs:
  build-deploy:
    runs-on: ubuntu-latest
    environment: production        # requires manual approval in repo settings
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with: { php-version: '8.3' }
      - run: composer install --no-dev --prefer-dist --optimize-autoloader
      - run: vendor/bin/phpunit --testsuite=smoke
      - run: tar czf release.tgz --exclude=.git --exclude=tests .
      - name: Upload and activate
        env:
          SSH_KEY: ${{ secrets.DEPLOY_KEY }}
        run: ./deploy/activate.sh release.tgz "$GITHUB_SHA"
~~~{% endraw %}

The {% raw %}`activate.sh`{% endraw %} script is short and boring on purpose:{% raw %}

~~~bash
#!/usr/bin/env bash
set -euo pipefail
REL="$(date +%Y%m%d-%H%M)-$(echo "$2" | cut -c1-7)"
ssh deploy@app "mkdir -p /var/www/app/releases/$REL"
scp "$1" deploy@app:/tmp/release.tgz
ssh deploy@app bash -s <<EOF
set -e
cd /var/www/app/releases/$REL && tar xzf /tmp/release.tgz
ln -sfn /var/www/app/shared/.env .env
rm -rf storage && ln -sfn /var/www/app/shared/storage storage
php artisan migrate --force
php artisan config:cache && php artisan route:cache
ln -sfn /var/www/app/releases/$REL /var/www/app/current
sudo systemctl reload php8.3-fpm
ls -1dt /var/www/app/releases/* | tail -n +6 | xargs rm -rf   # keep 5
EOF
~~~{% endraw %}

Two cautions. Migrations run before the switch, so they must be backward compatible with the release still serving traffic; that is the expand-and-contract pattern from my zero-downtime migrations post. And keep the deploy user's sudo rights limited to reloading PHP-FPM, nothing else. If you would rather use a tool, Deployer does the same release/symlink dance with less shell.

## Rollback is one command{% raw %}

~~~bash
ssh deploy@app 'cd /var/www/app && PREV=$(ls -1dt releases/* | sed -n 2p) && ln -sfn "$PWD/$PREV" current && sudo systemctl reload php8.3-fpm'
~~~

Rollback does not undo database migrations. That is another reason to keep migrations additive until the old code is gone.

## Taming the crontab

Old apps hide business logic in cron: invoice emails at 2 a.m., a CSV import every 10 minutes, a cleanup script that nobody dares touch. My rule is to move them one at a time into Laravel's scheduler, wrapping the old script first rather than rewriting it.{% raw %}

~~~php
// routes/console.php (Laravel 11+)
use Illuminate\Support\Facades\Schedule;

Schedule::exec('php /var/www/app/current/legacy/cron/send_invoices.php')
    ->dailyAt('02:00')
    ->timezone('Asia/Karachi')
    ->withoutOverlapping(30)
    ->onOneServer()
    ->appendOutputTo(storage_path('logs/cron-send-invoices.log'))
    ->onFailure(fn () => report(new RuntimeException('send_invoices failed')));
~~~{% endraw %}

Then the crontab shrinks to a single line, {% raw %}`* * * * * php /var/www/app/current/artisan schedule:run`{% endraw %}. For each migrated job I disable the old crontab line in the same change, never before and never after, so it cannot run twice or not at all. {% raw %}`withoutOverlapping`{% endraw %} fixes the classic bug where a slow import starts again before the last one finished. Later, each wrapped script can be rewritten as a proper command or queued job when someone actually needs to change it.

## Diagnostic checklist

- [ ] Production web root diffed against Git; hotfixes committed
- [ ] Uploads, logs and caches moved to a shared directory outside releases
- [ ] Secrets in a shared .env, not in committed config files
- [ ] CI builds the artifact and runs at least a smoke test suite
- [ ] Releases are timestamped directories with a {% raw %}`current`{% endraw %} symlink
- [ ] PHP-FPM is reloaded after each switch
- [ ] Rollback tested on staging this month
- [ ] Crontab reduced to {% raw %}`schedule:run`{% endraw %}, every job has locking and logs
- [ ] FTP access disabled once the pipeline is proven

## FAQ

### Can I do this on shared hosting without SSH?
Partly. Many hosts now offer SSH or Git deploy. If yours has neither, that is a strong reason to move to a small VPS; atomic releases depend on symlinks and shell access.

### What about apps that write files into the code directory?
Find every write path first, from the inventory step, and point them at {% raw %}`shared/`{% endraw %}. Until that is done, atomic releases will lose user data on each deploy.

### Do I need Docker for this?
No. Containers are a fine next step, but release directories plus CI give you most of the safety with far less change to an old app.

### How do I handle the first deploy safely?
Deploy the pipeline's first release to a new path, point a staging hostname at it, compare behaviour, then switch Nginx for production. Keep the old document root intact for a week.

### What if a cron job must never run twice?
Use `withoutOverlapping` with a reasonable expiry, `onOneServer` when you have more than one app server, and make the script itself idempotent where you can.

### When can I turn off FTP?
After two or three clean pipeline deploys and one tested rollback. Remove the FTP accounts rather than just changing passwords.

## What's next

Part 6, the finale, covers decommissioning the old app: shadow traffic, feature flags and the final cutover. If you would like help setting up deploys and servers for an inherited app, this is the [server setup and maintenance](https://looporigin.com/services/server-setup-maintenance?utm_source=devto&utm_medium=series&utm_campaign=ftp-deploys-cron-to-pipeline) work we do at Loop Origin, and our [pricing page](https://looporigin.com/pricing?utm_source=devto&utm_medium=series&utm_campaign=ftp-deploys-cron-to-pipeline) shows how we scope it. For the Laravel side of the modernization, see our [Laravel development services](https://looporigin.com/services/laravel-development?utm_source=devto&utm_medium=series&utm_campaign=ftp-deploys-cron-to-pipeline).

---

### Follow & Connect

- **Website:** [Loop Origin](https://looporigin.com/?utm_source=devto&utm_medium=series&utm_campaign=ftp-deploys-cron-to-pipeline)
- **Watch on YouTube:** [Resume Boost AI](https://www.youtube.com/@resume-boost-ai) ยท [PixelValut](https://www.youtube.com/@pixel-valut) ยท [Daily AI News By Ali](https://www.youtube.com/@AIDailyNewsByAli)
- **LinkedIn:** [Loop Origin](https://www.linkedin.com/company/loop-origin)
- **X:** [Muhammad Ali Mughal (@AliMughal887)](https://x.com/AliMughal887)
- **GitHub:** [Loop Origin (@LoopOrigin)](https://github.com/LoopOrigin)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)