Composer (and Laravel) on Shared Hosting

Got shared hosting with SSH access? Let’s get Composer up and running, the same way we did with WP-CLI.

Getting Composer to Work

Download the latest composer.phar en upload it to your host’s home directory. Or, better still, download it straight from the command line:

cd
curl -O https://getcomposer.org/composer.phar

Make the file executable:

chmod +x composer.phar

Define a composer alias in .bashrc:

alias composer="~/composer.phar"

Reload .bashrc:

source .bashrc

Et voilà!

Installing Laravel …

Using Composer!

composer global require "laravel/installer"

Now go ahead and create a new project:

composer create-project --prefer-dist laravel/laravel my-project

Note: When using cPanel, you’ll notice the DocumentRoot for the main domain is always ~/public_html. That doesn’t mean it can’t work, though. Simply clone your laravel project into a folder one level above ~/public_html (e.g., ~/my-project). Then either copy all files in ~/laravel/public to ~/public_html and adapt the following two lines of code in ~/public_html/index.php

require __DIR__.'/../laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';

… or, and this is likely the cleaner option, simply remove ~/public_html and recreate it as a symlink!

ln -s /absolute/path/to/laravel/public /absolute/path/to/public_html

Also, additional cPanel domains behave a bit differently—well, they’re somewhat easier to work with—as you can freely choose their DocumentRoot, like ~/sub.mydomain.com/public.