On the SitePoint PHP blog there's a new tutorial posted showing you how to properly deploy applications with SFTP and Git. In their examples they build a PHP-based deployment process that uses a few handy packages to make the flow simpler than a set of manual commands.
Uploading files is an integral aspect of any deployment process, and the underlying implementation can vary depending on the type of your server.[...] The PHPSECLIB (PHP Secure Communications Library) package has an awesome API for routine SFTP tasks: it uses some optional PHP extensions if they’re available, and falls back on an internal PHP implementation otherwise. You don’t need any additional PHP extension to use this package, the default extensions that are packaged with PHP will do. In this article, we will first cover various features of PHPSECLIB – SFTP, including but not limited to uploading or deleting files. Then, we will take a look at how we can use Git in combination with this library to automate our SFTP deployment process.
They start with a quick command (Composer) to get the phpseclib library installed but then quickly move into using it and some SSH keys to:
- authenticate to the server with public/private keys
- uploading a sample file
- automating the deployment with Git, pushing only changed files from a local git repo
- getting the contents of a specific commit
- the actual push of the files via SFTP
There's also a few other helpful hints included showing how to manage permissions on the remote server, execute remote commands and downloading files. The post ends with links to other similar tools if you're interested in more complete approaches.