PHP Composer on shared hosting

One of my goals with PHP is to simplify and streamline my workflow as much as possible. Ideally this helps prevent wasting enormous amounts of my time on stupid crap like autoloaders, library dependencies and re-inventing the wheel.

I’m using a few libraries on IOP’s site, but as much work as these have saved, I ended up with an ugly mishmash of autoloading and require code.

Enter the Composer project.

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

Together with Packagist, which is sort of a nascent RubyGems or PyPI equivalent, using libraries in PHP just got a whole lot cleaner. Composer even generates a complete autoloader, meaning a bunch of annoying code can be replaced with a simple composer.json file and this:

require 'vendor/autoload.php';

Except when they don’t. Because, sometimes, they won’t.

There’s always a catch, isn’t there?

The problem I ran into was the dated default PHP configuration on our shared cPanel host. Trying to run curl -s | php threw this back at me:

#!/usr/bin/env php
Some settings on your machine make Composer unable to work properly.
Make sure that you fix the issues listed below and run this script again:

The phar extension is missing.
Install it or recompile php without --disable-phar

The allow_url_fopen setting is incorrect.
Add the following to the end of your `php.ini`:
    allow_url_fopen = On

The ionCube Loader extension is incompatible with Phar files.
Remove this line (path may be different) from your `php.ini`:
    zend_extension = /usr/lib/php5/20090626+lfs/

* sigh * These are not settings most shared hosts grant users access to–and if you ask they’ll probably just try to upsell you a VPS. Thankfully though, many cPanel installations now allow users to select different versions of PHP (A2 does). And thankfully again, these alternate versions are often compiled with different flags.

That’s great for web pages, but Composer’s installer uses the command line. To use an alternate version of PHP in a shell, we need to know where the alternate PHP binaries are. An easy way is simply to switch the PHP Version via cPanel, then load a page containing phpinfo();. The actual PHP binary path isn’t listed, but it’s easy enough to figure out from what’s there. Mine was /opt/php/php-5.4.0/bin/php.

Once you’ve got that, you can fiddle with your $PATH (it’s searched from left to right) or just alias the alternate PHP binary:

alias php='/opt/php/php-5.4.0/bin/php'

I aliased php in my .bashrc file (reasons previously discussed) and composer installed flawlessly.

6 Responses to “PHP Composer on shared hosting” Comments Feed for PHP Composer on shared hosting

  • Just FYi: ionCube is just broken with phars so we can’t do much about that, but in the long run the allow_url_fopen stuff should not be necessary anymore.

    • Good to hear, I’m really happy with Composer.

  • Worked for me. Thanks. I am also on A2 Hosting.

  • I’m using hostlatte, and switching the cli to use 5.3 allowed me to install composer (and the correct version of drush, and now my drush aliases work). Really helpful tip, thanks!

  • Hi, just update your php version to 7.0, I didn’t even have to install composer again in my domain it worked by just upgrading php

  • It is better to stay away from shared hosting because of security and performance concerns. A better alternative that is safe and better in performance is a vps for host your PHP apps. There are a lot of vps providers that are easily affordable. However, you need to have sysadmin knowledge to setup a server on them. If you don’t have such skills, then you can use Cloudways PHP hosting platform for that. This platform will automatically setup a server with OS, stack and necessary packages already installed for you.

Leave a Reply