How to Use Composer on a Simple Hosting Instance

This tutorial describes how to use Composer , the popular PHP dependency manager, on a Simple Hosting PHP instance .

This tutorial does not cover creating the Simple Hosting instance or creating the site for your domain.

You will work from the command line interface of your computer, where you will perform all operations. Gandi CLI is not yet available for Gandi v5, so this tool will not be used here.

What is a Dependency Manager?

A dependency manager is a tool that makes it easy to include external code (dependencies) in a project. This is especially useful when these dependencies themselves include dependencies.

In order to avoid copying multiple files by hand (making it easy to accidentally duplicate files), a dependency manager such as Composer provides a simple tool for listing dependencies in a file, to launch a command that downloads and installs them for you.

In addition, popular dependency managers are attracting a growing number of developers who choose to publish compatible libraries, making it easier for their programs to be used. As a result, a large amount of good quality, ready-to-use libraries are available.

Prerequisites

For this tutorial you will need:

  • A Gandi account

  • A Simple Hosting PHP instance

  • Git and PHP installed on your computer

Getting Started

Start by creating the project folder on your computer.

Install Composer

An automated installation script that you can run directly from the command line is provided by Composer.

Copy and paste the following command into your terminal to create the composer.phar file at the root of site/vhost.

You can now run this file with php (composer php) to get usage instructions and verify installation.

The application used in this example is in a single 30-line file, containing PHP and HTML code. When the page is loaded, the PHP script makes an HTTP request to Gandi’s Status API and retrieves the current status of the services. The page will then display a text detailing the status, as well as a link for more information.

Declare and Install Dependencies

Instead of writing the HTTP request by hand, you will use Guzzle, a library that makes writing and handling requests easy.

Note

For PHP versions older than 7, you will need to use Guzzle version 5 instead of version 6.

Guzzle is available through Composer, so you can declare this dependency in a “composer.json” file, which you will create in site/vhost.


{
“require”: {

“guzzlehttp/guzzle”: “~6.0”

}

}

Now run the Composer command for installation:

The dependencies of the application, in this case Guzzle and its own dependencies, are now installed in the “vendor” folder of your project. You will also notice that Composer has created a “composer.lock” file in this folder. This file contains the exact versions of the libraries which were downloaded by Composer, and which will be the same which will be used by the Simple Hosting instance.

Create the index.php File

Your code will consist of a single file: index.php. It contains a “getGandiStatus()” function which retrieves the current status of Gandi services from the API, called by a “currentStatus()” function which returns the results in a human readable sentence. Then some HTML will produce a minimalistic web page, with some nested PHP, to output the resulting sentence from the “currentStatus()” function.

To include the Guzzle library, you will use the standard Composer “require” statement, which is used to include all the dependencies managed by the tool.

..code-block:: none

~/gstatus $ cd htdocs/ ~/gstatus/htdocs $ vim index.php

Commit to the Git Repository

Your application is now almost complete and you can save your changes to Git.

Creating a .gitignore file

Create a ”.gitignore” file at the root of your project to force Git to ignore the Composer executable (composer.phar), as well as the “vendor” folder where Composer installs the dependencies.

Git “commit” and “add” files to the tree

You can now add the other files (“stage”) to the Git tree and save your changes with “commit”.

..code-block:: none

~/gstatus $ git add . ~/gstatus $ git commit -m ‘First commit’

Configure Remote Git Repository

Simple Hosting PHP instances have a Git repository per vhost. They are also pre-configured with a vhost corresponding to a test URL provided by Gandi. In this example, you will deploy the code in this vhost and the application will be accessible at this test URL. You can find the URL for the git service in the “Sites” tab of your Simple Hosting instance on each site’s page. You can then run the following command, replacing the fields in parentheses with the correct values:

Code Push

After adding the “gandi” remote repository, you can simply push your code to the “master” branch.

Deploying the Code

Now is the time to deploy your code. A git checkout will automatically be performed in the target directory of your instance. All dependencies will also be installed using Composer. In this example, we will use the vhost 1234567890.testurl.ws.

The deployment script is quite verbose and will allow you to monitor the progress of the deployment. Once the deployment is complete, your code will be on the instance and accessible via the web.

Open Your Site in a Browser

You can now visit your VHOST URL to verify that the application is running on your Simple Hosting instance: 1234567890.testurl.ws