Table of Contents
Using Composer with Simple Hosting PHP
This tutorial will show you how to use Composer, the popular dependency manager, with Simple Hosting PHP.
The tutorial covers a full project using Composer, from instance creation to installing and using Composer, from coding the application to opening it in your web browser.
You'll be working on your own computer's command line interface, from where you'll run all the commands. Some commands use Gandi CLI, our home-made tool that makes it easier to work with Gandi products from a Terminal. We also provide the standard SSH and git command examples.
What is dependency management
A dependency manager is a tool that makes it easy to include external code (dependencies) into a projet. It is particularly useful when those dependencies include external dependencies themselves.
Instead of copying many files (possibly duplicates), by hand, a dependency manager such as Composer provides a simple tool, that allows you to list your dependencies in a file and run a command to download and install them for you.
In addition, successful dependency managers attract growing numbers of developers who choose to publish compatible libraries to make it easier for users to adopt them. As a result, the quality and quantity of the code that can be included with such tools is very attractive to modern developers.
Requirements
To complete this tutorial, you'll need:
- A coupon code, an accepted payment method or enough credit in your Prepaid account to purchase your Simple Hosting instance
- PHP 5.3+, git and an SSH client installed and running on your system
- Gandi CLI version 0.17 or above (Gandi CLI usage is optional but recommended)
Getting started
Start by creating the project's directory on your computer.
~ $ mkdir gstatus ~ $ cd gstatus ~/gstatus $ git init . ~/gstatus $ mkdir htdocs
Install Composer
Composer provides a handy automated installer script that you can execute directly from the command-line using PHP.
Copy-paste the following command in your terminal to create a “composer.phar” file in the root folder.
~/gstatus $ php -r "readfile('https://getcomposer.org/installer');" | php
Now you can run the file with “php” to get usage instructions and make sure it was properly installed.
~/gstatus $ php composer.phar
Write the application
The example application consists of a single file with about 30 lines PHP and HTML code. When the page is loaded, the PHP script will make an HTTP request to the Gandi Status API and retrieve the current overall status of Gandi's services. The page will then display a short text to describe the current status as well as a link to the Gandi Status website.
Declare and install dependencies
Instead of writing all the HTTP request logic by hand, you'll use Guzzle, a handy library that makes it easy to write requests. Guzzle is available through Composer, so you can just declare it as a dependency in a “composer.json” file that you'll create in the root folder.
Note: Require Guzzle version 5 on PHP 5.4 instances, rather than version 6 .
~/gstatus $ vim composer.json
{ "require": { "guzzlehttp/guzzle": "~6" } }
Now, run Composer's install command;
~/gstatus $ php composer.phar install
The application's dependencies, namely Guzzle and its own dependencies, are now installed in your project's “vendor” directory.
You'll also notice that Composer has also created a “composer.lock” file in the root folder. This file contains the exact versions that were downloaded by Composer and that will be used by Simple Hosting to install the same versions for you.
Create the index.php file
Your code will consist of a single file, index.php. It contains a “getGandiSetup()” function that fetches Gandi's current status from the Status API, called by a “currentStatus()” function that returns a human-readable sentence according to its result.
Then, some HTML code displays a minimal webpage and uses a bit of PHP to print the sentence resulting from the “currentStatus()” function.
To include the Guzzle library, you can use the standard “require” statement offered by Composer to include all the dependencies.
~/gstatus/htdocs $ vim index.php
<?php require '../vendor/autoload.php'; function getGandiStatus() { $client = new GuzzleHttp\Client(); $res = $client->get('https://status.gandi.net/api/status'); $gandi_status = json_decode($res->getBody(), true); return $gandi_status["status"]; }; function currentStatus() { switch(getGandiStatus()) { case "SUNNY": return "All Gandi services are operational"; default: return "Gandi is experiencing a bit of trouble"; }; }; ?> <html> <head> <title>Gandi Status Check</title> </head> <body> <h1><?php echo currentStatus(); ?></h1> <p><a href="http://status.gandi.net">More info</a></p> </body> </html>
Commit to git
Your application is now pretty much complete and you can save your changes to git.
Create a .gitignore file
Create a ”.gitignore” file at the root of the project to tell git to ignore the Composer executable, composer.phar, as well as the “vendor” folder, where Composer installed your dependencies.
~/gstatus/htdocs $ cd .. ~/gstatus $ vim .gitignore composer.phar vendor
Add and commit your files to the tree
Now you can stage all the other files and commit your changes.
~/gstatus $ git add . ~/gstatus $ git commit -m 'First commit'
Publish your application to Simple Hosting
You can easily create and manage your instance from the command line using Gandi CLI or from the web Admin interface.
We recommend that you use Gandi CLI to work from the command line. It's an open project in constant development that makes it really easy to work with Gandi's products with simple commands.
Create the Simple Hosting instance
You can easily create a PHP instance on Gandi's website and manage it from the web Admin interface.
The example project does not use a database, so you can select any PHP instance type.
To create your instance with Gandi CLI, run the following command from your terminal:
~/gstatus $ gandi paas create gstatus --type phpmysql --size S --datacenter LU-BI1 Password:
That will start the creation process, which should take about a minute to complete if you have enough credit in your prepaid account. Otherwise, you might notice takes too long to complete, or fails. In that case, please login to the Admin interface to complete the payment. The operations should then go through.
Setup the git remote
PHP instances offer one git repository per VHOST, and come with a pre-configured VHOST corresponding to a test URL provided by Gandi. In this example, you will deploy your code to this VHOST, and the application will be accesible at the target URL.
Using Gandi CLI, you can retrieve the VHOST and use it to create your git remote.
~/gstatus $ gandi paas info gstatus | grep vhost vhost : 1234567890.testurl.ws
~/gstatus $ gandi paas attach gstatus --vhost 1234567890.testurl.ws
Without Gandi CLI, you can access your VHOST's git URL in the instance admin page. Then you'll be able to run the following command, replacing the placeholders with the correct values:
~/gstatus $ git remote add gandi ssh+git://{login}@git.{datacenter_id}.gpaas.net/1234567890.testurl.ws.git
Push your code
After adding the “gandi” remote, you can simply push your code to its “master” branch.
~/gstatus $ git push gandi master
Deploy your code
Now you need to checkout the files from your VHOST's repository into your instance's VHOST folder. The “deploy” script will do just that, and will also run Composer and install your application's dependencies in the process.
To run the deploy script with Gandi CLI, simply execute the following command:
~/gstatus $ gandi deploy
Alternatively, you can run the deploy command via SSH directly. Use the example below and replace the placeholders with the same values used for the git remote URL.
~/gstatus $ ssh {login}@git.{datacenter_id}.gpaas.net 'deploy 1234567890.testurl.ws.git'
The deploy script is pretty verbose and will allow you to follow the progress of the deployment. Once the deployment is complete, your code will be in your instance and accessible via the Web.
Open in your browser
Now you can visit your VHOST's URL to check out the app running on your Simple Hosting instance: http://1234567890.testurl.ws
You should see the current status of Gandi's services. Congratulations :)