Using Composer on Shared Hosting

posted in: Tools | 0

I am using Shared Hosting Can I use Composer?
Why not!!! You could certainly plan using composer with your Shared Hosting and let Composer manage your dependencies.

What is Composer?

Composer is a dependency management tool that takes care of required dependencies for PHP applications.

How can it help me?

Composer would initially install and later update the dependencies required by your project. You need not install them manually or later update by replacing the existing installation.

Let’s understand this by an example:

  1. Installing Dependencies
    Your project uses following dependencies

    • vendor/package
    • vendor/package2
    • vendor/package3

    For using these dependencies just need to create a file named composer.json at the root of your project directory and run the composer install command. Composer would install these packages for you.

    Below is the composer.json file for above packages.

    [javascript]{

    "require": {

    "vendor/package": "1.3.2",

    "vendor/package2": "1.*",

    "vendor/package3": "^2.0.3"

    }

    }[/javascript]

  2. Managing Other dependencies
    Now, if vendor/package2 also requires ‘vendor/package4’ composer would automatically install ‘vendor/package4’ without an explicit declaration.
  3. Updating dependencies.
    If there is an update for vendor/package, and you require to update to version 1.4 (as in above example). Just change the version to the required in the latest composer.json file and run the composer update command.  Composer would update the dependency and other dependencies which it relies on.

Installing Composer on Shared Hosting

Composer could also be installed on a shared hosting(cPanel Server) for a hassle free dependency management of PHP applications.

Pre-requisites:

  1. Shell SSH access : Navigate to cPanel and enable it, if supported by your hosting provider
  2. PHP v 5.2 or greater

Use the following commands to get composer:

  1. Get composer.phar

    [bash]curl -sS https://getcomposer.org/installer | php[/bash]

    If you are unable to the above command on your hosting, you could probably download composer.phar from the link below:

    getcomposer.org/composer.phar

    In both the cases place the ‘composer.phar’ file in the root of your hosting account.

  2. Add an alias

    Edit .bashrc file for your account and add the following

    [bash]alias composer='<path to php-cli> <path to composer>'[/bash]

    e.g.

    [bash]alias composer=’/usr/local/php56/bin/php-cli ~/composer.phar'[/bash]
  3. Using Composer

    Restart your SSH session or type in the following command to reload your bashrc file

    [bash]source ~/.bashrc[/bash]

    just type in composer for using it

This could be helpful in cases wherein you are working with PHP frameworks like Symfony or Applications like Magento 2.0

Leave a Reply

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