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
orrun_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 calledsymfony-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.
Eugen Mihailescu
Latest posts by Eugen Mihailescu (see all)
- Dual monitor setup in Xfce - January 9, 2019
- Gentoo AMD Ryzen stabilizator - April 29, 2018
- Symfony Compile Error Failed opening required Proxies - January 22, 2018
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!
If you like it then make sure you rate it with 5 stars 🙂
Hi,
i tried to run the symfony-post-deploy.php in browser but it always show error
php /home/xxxx/domains/book.xxxxx.pl/public_html/composer.phar install --optimize-autoloader --no-interaction --verbose
Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in phar:///home/xxxx/domains/book.xxxx.pl/public_html/composer.phar/bin/composer on line 9
It happend on php 5.6 and php 7.08, CGI/FastCGI
If the cause of this would be the `symfony-post-deploy.php` script then normally the error should refer the script and not composer.phar.
Open the composer.phar script at line 9 and check what happens there. Install the PHP XDEBUG extension and check the error trace, you will see when happens and which is the real source that generates this flow of errors.