photo of a phone, note book and plant on a desk

Using Capistrano & Composer to Deploy to MediaTemple GridService

We love running servers and hosting site’s we’ve built for our own clients.  We get to set up our own developer tools and processes for deploying and managing sites.  Sometimes that isn’t possible and you end up using shared hosting. One sample of a big brand we have hosted is DaisySlots, a casino website that offers online gambling experience to all users anywhere in the world.

As a general rule there are two truths in the world that are unshakable.  One: web developers love tools and, two: they hate not having control of the server (aka shared hosting).

Generally, we build sites using WordPress, managing the code through Git and out dependencies through Composer. We set up VPS servers using Digital Ocean and then deploy sites to staging and production through Capistrano

We recently came up against a situation where we had to install a site we had developed on MediaTemple’s Grid Service Shared hosting platform. MediaTemple is a great company, with great support and a great platform, but they have to balance the needs of many, many users against just yours. To that end, you have to do some working around to get tools working.

Capistrano On MediaTemple Grid Service

(this assumes that you are comfortable using and installing Capistrano already)

First Things first, get access. Enable SSH access on your GridService Account.

Second, add the account in Capistrano’s config directory to the appropriate environment. Capistrano usually likes to use passwords SSH keys, but you can specify a password as well.  Add your SSH server, username and port as well.

server ‘ssh server', user: ‘SSH user name', port: 22, password: “#########”, roles: %w{web app db}

Now you need to adjust your deploy.rb settings. Login to your Grid Account via SSH and run the pwd command and get the directory you are in. Edit the deploy_to: setting in deploy.rb to reflect where you want to install your app on the Media Temple Server.

If you attempt your Capistrano deployment now it would run, assuming everything is configured correctly.  But it won’t really work.  MediaTemple has a strange environment where symlinks have been configured to work only with a full file path, no relative symlinks!

Craig Morris has written a clever little Ruby Gem (capistrano-gridservice) that changes how Capistrano creates its symlinks and fixes this issue for us.

Add. gem capistrano-gridserver to your Gemfile

Run bundle

Add require ‘capistrano/gridserver’ to your Capfile

Add after 'deploy:publishing', ‘gridserver:create_relative_symlinks' to the end of your deploy.rb

If you deploy now, all you have to do is symlink (remember to use a relative link) /current into your html directory and you’re good to go.

Adding Composer to Capistrano on MediaTemple GS

(assuming you’re using Capistrano/Composer already)

Now that we’ve got Capistrano pulling in our Git repo let’s get Composer to build it.

Due to limitations of the MediaTemple GS shared hosting platform you cannot edit the PHP.ini to governs it and any changes you make to your sites person PHP.ini will not be reflected here. This is a problem for Composer.

Composer needs to allow_url_fopen to be enabled in order to run correctly.  We can get around this by passing commands to PHP with the -d flag that lets us set runtime variables, so php -d allow_url_fopen=1 composer.par install should work.

Unfortunately, we aren’t running the command directly so we need to do some work to our deploy.rb.

The easiest way is to install Composer manually. SSH into your GS account, go to your Capistrano deployments’ shared folder and install composer. This is where the automated installer put it anyway.

curl -s https://getcomposer.org/installer | php -d allow_url_fopen=1 -d suhosin.executor.include.whitelist=phar’

Composer is now installed.

In your deploy.rb look for your SSHKit’s modification of the command map for Composer. Edit the command to reflect the new path (non-relative) and the new flags passed to php.

`SSHKit.config.command_map[:composer] = "php -d memory_limit=512M -d allow_url_fopen=1 -d suhosin.executor.include.whitelist=phar #{shared_path.join("composer.phar")}" `

You should now be able to deploy and build your app via Composer and Capistrano.

Stewart Ritchie Lead developer and founder of Powered By Coffee. Stewart has been building websites for 15 years and focusing on WordPress for 5. He founded Powered By Coffee in 2011 after finishing is masters degree. He lives in Guildford Surrey with his wife Sydney and their two cats.

Signup to our mailing list