martincodes-de / php-ftp-deploy-kit
PHP FTP Deploy Kit: thin layer over dg/ftp-deployment for FTP-only hosting: presets, remote database backup/pull/push, one deploy.ini as the only config.
Package info
github.com/martincodes-de/php-ftp-deploy-kit
pkg:composer/martincodes-de/php-ftp-deploy-kit
Requires
- php: >=8.2
- ext-curl: *
- dg/ftp-deployment: ^3.7
- symfony/console: ^6.4 || ^7.0
- symfony/process: ^6.4 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.5 || ^11.0
Suggests
- ext-ssh2: Needed for sftp:// deployments (dg/ftp-deployment); also requires curl built with libssh2
Provides
None
Conflicts
None
Replaces
None
README
Deploy a PHP app to FTP-only hosting in a couple of minutes. A thin layer over dg/ftp-deployment: presets, safe SQLite pull/push and a few helpers. dg does the deploying.
- For: classic shared hosts (bplaced and similar), no SSH, no Composer on the server. You build locally and upload the result.
- Not for: hosts with SSH. A tool that runs commands on the server (for example Deployer) fits better.
- Presets: Symfony (with or without SQLite) and static. More are welcome, see CONTRIBUTING.md.
Quick start
Needs PHP 8.2+ with ext-curl. Install pfdk in its own folder, so composer install --no-dev during a deploy cannot remove it.
mkdir ftp-deployment && cd ftp-deployment composer require martincodes-de/php-ftp-deploy-kit php vendor/bin/pfdk init symfony # or: symfony-sqlite, static
init creates deploy.ini and ignores it in git. Put your connection into it (remote = "ftps://USER:PASSWORD@HOST/PATH"), then run from the project root:
php ftp-deployment/vendor/bin/pfdk deploy --dry # preview
php ftp-deployment/vendor/bin/pfdk deploy
| Scheme | Meaning |
|---|---|
ftps:// |
FTP over TLS (recommended) |
ftp:// |
Plain FTP, password unencrypted; pfdk warns |
sftp:// |
Needs the ssh2 PHP extension and curl built with libssh2 |
file:///path |
A local folder, to rehearse a deploy |
First deploy (Symfony)
- Create
.env.prod.localin the project root with the production secrets (APP_SECRET, API keys). Git ignores it, the deploy uploads it. - With SQLite: the deploy never uploads
var/, so put your database on the server once withpfdk db:init. It creates the database folder on a fresh server and uploads your local database, and it refuses to overwrite a live one. - If the host runs an older PHP, set
"config": {"platform": {"php": "8.2"}}in the project'scomposer.json. - Run
pfdk deploy --dry, check the file list, thenpfdk deploy.
Commands
| Command | What it does |
|---|---|
pfdk init [preset] |
Install a preset (asks if you give none) |
pfdk deploy [--dry] [--pull] [--no-db] |
Run dg. For Symfony projects with a database it offers to migrate first; --no-db never touches the database (see Databases) |
pfdk backup |
Download the live database into the backup folder |
pfdk db:pull |
Replace the local database with the live one (the local one is kept) |
pfdk db:init |
Fresh server: create the database folder and upload the local database; never overwrites a live one |
pfdk db:push [-y] |
Overwrite the live database with the local one (the live one is saved first) |
pfdk db:migrate [-y] |
Symfony: pull, run the Doctrine migrations locally, push back |
pfdk remote:ls [path] |
List a folder below the remote path |
pfdk remote:mkdir <path> |
Create a folder and its missing parents |
pfdk remote:upload <local> <remote> [-y] |
Upload a file (an existing one is replaced, after a question) |
pfdk remote:download <remote> <local> [-y] |
Download a file; an existing local file is only replaced after a question |
pfdk remote:rm <path> [-r] [-y] |
Delete a file or an empty folder; -r deletes a folder with everything in it. Asks first, refuses the remote root and .. |
pfdk exec -- <command> |
Run a command in the project, through DDEV when it runs |
-c <ini> picks the ini. Without it pfdk uses deploy.ini or the only deploy*.ini one level down, and asks if there are several. A connection or login error always exits non-zero, so a failing backup aborts a deploy.
Configuration
One deploy.ini, which dg reads too (dg ignores the pfdk_* keys).
| Key | Meaning |
|---|---|
pfdk_db |
SQLite file, relative to the project and to the remote root. Enables the database commands |
pfdk_db_local |
Local SQLite file if it differs from the live one. Default: same as pfdk_db |
pfdk_backups |
Backup folder (default backups) |
pfdk_backups_keep |
Keep the newest N pfdk backups (default 10, 0 keeps all) |
pfdk_retries |
Attempts for a transfer that fails to connect or times out (1 to 10, default 3, 1 = no retry; anything else is an error) |
pfdk_finally[] |
Steps that run after every deploy, also a failed one. See Cleanup steps |
pfdk_type = symfony |
Enables db:migrate |
pfdk_migrate_command |
Replaces the default php bin/console doctrine:migrations:migrate --no-interaction |
pfdk_ddev = off |
Never use DDEV (default: use it when it runs) |
Building assets, clearing caches and the like are plain before[] / after[] jobs, see dg's documentation. local (.. in the presets) is the project folder, and relative paths are resolved against it. The folder name ftp-deployment is only a convention.
Databases
- Only SQLite files can be pulled and pushed over FTP. MariaDB and MySQL are not supported by these commands; deploying the code works for any app.
- A missing folder of the database file (for example
var/databaseon a fresh server) is created by the firstdb:push, also withfile://. - Deploy without touching the database: once real data lives in the live database, run
pfdk deploy --no-db. There is no migration offer, and pfdk setsPFDK_NO_DB=1for dg, so apfdk backupstep inbefore[]does nothing instead of downloading the database, anddb:pull,db:push,db:initanddb:migraterefuse. The variable works on its own too (PFDK_NO_DB=1 pfdk db:pushrefuses), for CI or an alias. Only the value1counts. db:pushuploads to<name>.pfdk-upload, checks the size, then renames it over the live file, so an aborted upload leaves no truncated database. A server that reports no file size is not trusted: the push is aborted, the live file untouched.- If the rename's answer gets lost, pfdk reads the state back instead of guessing; if even that fails it stops and names the two files to check. It deletes the old live file only when the server explicitly refuses to rename over it, and then keeps the complete new file as
<name>.pfdk-uploadif the last rename fails. db:pushreplaces the whole file. Run it when nobody writes to the live app.backupanddb:pushneed the folder of the database file to exist; on a fresh server rundb:initfirst (a missing folder looks like an access error to the server).- No remote shell: migrations run on your machine against a copy. Run
db:migratebefore deploying code that needs the new schema. - Different local and live file (for example
var/data_dev.dblocally,var/data_prod.dblive): setpfdk_db_local.db:pullreplaces it,db:pushuploads it,db:migratemigrates it. An empty value counts as not set. - With
pfdk_db_localthe app'sDATABASE_URLmust point at that file, for examplesqlite:///%kernel.project_dir%/var/data_dev.db. If the migration leaves the file unchanged,db:migratewarns and does not push, so an unmigrated copy never replaces the live database.
Flaky connections
backup,db:pullanddb:pushtry again when the connection fails or times out (curl errors 7, 10, 28, and 55, 56 in the middle of a download or of the temporary upload): up topfdk_retriesattempts, 3 s apart, with a short message on screen.- Wrong password and missing files are never retried.
Backups
backup,db:pullanddb:pushsave intopfdk_backups:pfdk-database-<stamp>.db,pfdk-database-pulled-<stamp>.dbandpfdk-database-pre-push-<stamp>.db.pfdk_backups_keepdeletes only files with thatpfdk-database-prefix, oldest stamp first.- Other files are never touched, including older pfdk backups named
database-*.db. Remove those yourself. - Data on the server damaged? Copy the newest backup over your local database and run
pfdk db:push.
Cleanup steps
pfdk_finally[] undoes what before[] did locally, for example restoring dev packages after composer install --no-dev. The Symfony presets use it for exactly that.
pfdk_finally[] = "php ftp-deployment/vendor/bin/pfdk exec -c ftp-deployment/deploy.ini -- composer install"
- Runs after every
pfdk deployonce dg started: success, failure and--dry. - All steps run even if one fails. The deploy's own result still counts, and a failing step makes the command exit non-zero.
- Also runs when dg could not connect (the exit code cannot tell that apart from a later failure).
- Does not run when pfdk stops before dg starts (failed
--pullor migration). - Ctrl-C and SIGTERM during dg are waited out so the steps still run. This needs the
pcntlextension; without it an interrupted deploy skips them, so run them by hand. - dg skips its own
after[]steps when a deploy stops midway, which is why cleanup belongs here.
DDEV
If the project has .ddev/config.yaml and DDEV is running, pfdk exec and the default migration go through ddev exec. Steps in pfdk_finally[] are plain shell commands in the shell pfdk runs in, with no DDEV detection of their own. Start a step with pfdk exec --, as the presets do, to follow the DDEV rule like before[] steps.
Several targets
One ini per target (deploy.staging.ini, deploy.production.ini), or one ini with sections [staging] and [production] and --section production. With sections pfdk never guesses: it asks or fails listing them. Give each target its own pfdk_backups folder.
Presets
symfony (production build, prod flag, cache purge, .htaccess files), symfony-sqlite (adds the database settings and a backup before the build) and static. Install your own from a folder: pfdk init ./my-presets/wordpress, see CONTRIBUTING.md.
Web root and .htaccess (Apache): the Symfony presets put two editable files next to your deploy.ini and upload both. Which one does the work depends on your hosting:
- The web root points to
public/(best):htaccess.publicbecomespublic/.htaccessand sends requests toindex.php. - You can only put the whole folder into the web root:
htaccess.rootbecomes.htaccessthere and rewrites every request intopublic/, so.env.prod.local,src/andvar/cannot be downloaded.
The file you do not need stays unused. If you want neither, delete the two upload: lines, the two .htaccess ignore entries and the files. The folder is called public in the files; change it there if yours is named differently. Other web servers than Apache need their own rules.
Development
ddev start && ddev composer install && ddev exec vendor/bin/phpunit
Changes are specified with OpenSpec (openspec/), and the full test suite must pass before a change is archived. MIT licensed; use it, fork it, change it.