My *nix world

Use Composer without SSH access

If you have to deploy a PHP application to a remote web hosting server where you do not have access to a terminal like environment, then it seems that the only remaining deployment option is the File Transfer Protocol. You transfer the files from a local repository to the remote web hosting server. Fair enough.

Some PHP applications are written on top of a web framework like Symfony, Laravel, CodeIgniter which usually have a quite large disk footprint (+10-50MB or smaller in case micro-frameworks). So when you want to deploy an application written on top of such web frameworks you should deploy not only your own code but also its dependencies. This is why they built the Composer.

The only problem with Composer is that it does not have a web UI but instead it requires to be run within the CLI environment (ie. at a terminal). So how one could use Composer without remote SSH access to the host terminal? To answer this question I wrote symfony-shell which is a small PHP application that gives you access to Composer and/or Symfony's bin/console command both via a web UI or via CLI (yes, the CLI functionality wasn't really necessary but what a heck, it required only one line of code.

The application has only one core PHP script file (symfony-shell.php) with less than 300 lines of code (perhaps ~60 lines without comments and white-spaces), a lightweight prototype that can be extended.

To extend it to whatever suits to you is straightforward:

  • create a regular PHP script where you define and register your custom hook functions(*)
  • call the built-in run function that in turn will call all the registered hook functions and outputs their result on a built-in HTML terminal

(*) A hook function is a callable PHP function (anything that can be run via call_user_func where you can do whatever you want but additionally it should call either the built-in run_composer or run_symfony_console functions which allows you to run a Composer or Symfony command by specifying the command arguments.

Besides the symfony-shell.php core file the project comes with a demo extension called symfony-post-deploy.php which actually register 3 callbacks, ie. it has 3 different actions:

  • composer_install : which runs the composer install command
  • symfony_cache_clear : which runs the Symfony's cache:clear command
  • symfony_dump_assets : which runs the Symfony's assetic:dump command

For instance I run this extension right after deploying a Symfony web app via FTP to a remote web host where I don't have SSH access. I could simply enter the PHP script address in the browser but actually I automated a bit the whole process: I wrote a deployment Linux bash script which sends my files remotely via FTP then it just sends a GET request to http://whatever/domain/symfony-post-deploy.php which does what it was programmer to do (it checks and install the necessary dependencies, clear the Symfony's cache for the production environment and finally dumps the project assets to a public assets folder like assets/css or assets/js). If you want to make sure no one else can mess with your post-deployment script then just configure a .htaccess file (or alike) that restricts the access to it (by host/ip, password, etc).

In a nutshell, for a typical Symfony application this extension is all you need:

  • by using FTP you upload the minimal version of your project: your code together with project's requirements file (ie. composer.json file)
  • then you just run the http://your-domain/your-site/{extension-name}.php (eg. symfony-post-deploy.php) which supposedly will run the registered hook functions (in my example it will install the required vendor packages then will clear the cache for the production environment and finally will dump the project's assets, ie. the CSS and JS files).

More info about how to install or how to use it on its GitHub repository: https://github.com/eugenmihailescu/symfony-shell

Note: keep in mind that if your composer contains some post-install or post-update scripts that run interactively (expects inputs from user) then the execution might hang indefinitely and thus fail.

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

 
The following two tabs change content below.
Use Composer without SSH access

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
Use Composer without SSH access

Latest posts by Eugen Mihailescu (see all)

Tagged on: , , ,

2 thoughts on “Use Composer without SSH access

  1. Marius

    O.M.G. This is THE most underrated script ever created. I cannot thank you enough for sharing it. Saved my day getting PHPMailer running without SSH access after switching from htmlmimemail5 that won't run with php7.
    Thank you so much!

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: