EasyApache 4 & CLI based PHP utilities

With the release of EasyApache 4 in WHM 58 there are various changes to how PHP is managed. The most obvious being that EasyApache 4 brings support for installing multiple PHP versions alongside each other. However with multiple versions of PHP being installed on the server it’s easy to lose track of your command-line based PHP utilities and their PHP requirements.

Certain PHP packages like Composer, Drush, and WP-cli prefer to be run with the latest version of PHP. Unfortunately, having multiple PHP versions can mean not knowing which PHP will execute the utility when you call it directly. This article will detail a few methods to specify which PHP version should be used in instances like this.

Pre-flight Check:

  • These instructions are intended specifically for cPanel-based servers running WHM prior to version 58.
  • Command line access via SSH may be necessary to follow the examples.
  • The server will need to have EasyApache 4 active.
  • PHP 7 will be necessary to follow the provided examples.

Option #1: Directly Call the PHP Binary

The most basic option to workaround these issues is to directly call the PHP binary you need before executing the script. While this can be effective and will work as expected most of the time, certain utilities will not work when using this method.

You may want to attempt this method first and move on to Option #2 if this does not work as expected; generally though, this should work for executing basic PHP code.

/opt/cpanel/ea-php70/root/usr/bin/php someScript.php

The above command will specifically call the 7.0 version of PHP and will execute the someScript.php file within that version of PHP. If they are also installed on the server, the alternative PHP versions can be found at:

  • /opt/cpanel/ea-php55/root/usr/bin/php
  • /opt/cpanel/ea-php56/root/usr/bin/php
  • /opt/cpanel/ea-php70/root/usr/bin/php

Option #2: Use scl to Specify PHP Versions

As mentioned, certain PHP utilities are not provided as basic PHP files and are stored in the PHAR format. These utilities will not execute properly with the above method for specifying the PHP version. For these types of utilities you will need to specify the PHP version using a command called `scl`.

This command is a utility that allows running software in an environment packaged as a ‘Software Collection’. To be brief a ‘Software Collection’ is a way of defining an alternate location to a certain software. Fortunately, WHM provides predefined collections for the various versions of PHP supported by EasyApache 4.

The syntax of this command is as follows:

scl enable {SoftwareCollection} '{CommandToRun}'

Where you replace {SoftwareCollection} with the needed collection [ea-php55, ea-php56, or ea-php70], and you replace {CommandToRun} with the command, utility, or script you need to run.

A good way to highlight this is to review the difference in the following outputs:

root@noms [~]# php -v

PHP 5.6.25 (cli) (built: Aug 25 2016 17:00:38)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with the ionCube PHP Loader v4.7.5, Copyright (c) 2002-2014, by ionCube Ltd., and
with Zend Guard Loader v3.3, Copyright (c) 1998-2014, by Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies

root@noms [~]# scl enable ea-php70 'php -v'

PHP 7.0.10 (cli) (built: Aug 25 2016 18:04:55) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.10, Copyright (c) 1999-2016, by Zend Technologies

As you can see when calling PHP by itself we get the default version, but when using scl we explicitly called the PHP 7.0 collection and get results to match.

Example #1: Executing a utility [Composer]:

root@noms [~]# scl enable ea-php70 'composer --version'

Composer version 1.2.0 2016-07-19 01:28:52

Example #2: Running a PHP script:

root@noms [~]# scl enable ea-php70 './someScript.php'

With these key tips and tricks you should now be equipped with the necessary tools to run CLI utilities on a server using EasyApache 4. If you have any questions or are not comfortable making these changes yourself, please feel free to contact Heroic Support®.

How to Replace PHP GeoIP with MaxMindDB

Depending on the site or application, looking up geographic information related to an IP address can be a pretty common action. When doing IP geolocation in PHP usually the PHP GeoIP extension would be used to facilitate the retrieval of this information. Unfortunately, this particular plugin is no longer actively supported and has not been updated in a number of years.

With the go-to PHP extension of IP geolocation effectively being deprecated, new projects should begin to use the replacement options that are now provided by MaxMind. However, unlike the original GeoIP, which was shipped as a native PHP extension, the new solutions are provided as PHP-based library packages.

Pre-Flight Check

  • Basic familiarity with PHP coding is necessary to utilize Composer packages.
  • Command line access via SSH may be necessary to follow this tutorial.
  • Composer, Curl, funzip must be available on the server.

Step #1: What are my options now?

As mentioned previously, the new options are no longer provided as native PHP extensions, but rather they come as Composer-based PHP packages. The new options provided by MaxMind are either: GeoIP2-php or DB-Reader-php.

Both of the options provide the ability of IP geolocation with subtle differences; in a sense the GeoIP2-php package is an addition built on top of the DB-Reader-php package, it offers all the same features as DB-Reader-php with the addition of API access.

Additionally, it is important to note that only the DB-Reader option is provided without any additional costs. With the new options MaxMind now charges a subscription fee in order to access their APIs.

Generally for most use-cases the necessary features will be provided by the DB-Reader-php package, as such this article will focus on this option.

Step #2: Get started with MaxMind DB-Reader!

As mentioned in the pre-flight check, Composer will be required in order to follow these steps. If you do not have Composer setup on the server please feel free to review our Composer series here: ‘What is Composer?’.

To acquire the necessary DB-Reader package you will want to start by changing directory so that you are in the root folder of your domain (for this example we will just assume this is `public_html`), then you will run the following command:

[public_html] $ composer require maxmind-db/reader:~1.0

Running this command will download the package files into the current folder, as described in our Composer series. This will create a vendor folder if this is the first time using Composer in this site/project.

Next you will require the actual MaxMind database files which contain the geolocation data. To get these files you will execute the following commands:

[public_html] $ funzip <(curl -L http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz) > ./GeoLite2-Country.mmdb
[public_html] $ funzip <(curl -L http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz) > ./GeoLite2-City.mmdb

With the above commands executed you should now have the necessary components to do Geolocation using the DB-Reader plugin. All that’s left is to implement it in your code.

Step #3: Looking up your first IP

In order to ensure the geolocation features are working properly you may want to do a quick test. First we’ll confirm you have all the right pieces by running the following command:

$ ls -lah

You should see a file structure similar to this:

-rw-rw-r-- 1 someuser someuser 63 Aug 11 17:03 composer.json
-rw-rw-r-- 1 someuser someuser 2.4K Aug 11 17:03 composer.lock
-rw-rw-r-- 1 someuser someuser 73M Aug 11 17:04 GeoLite2-City.mmdb
-rw-rw-r-- 1 someuser someuser 19M Aug 11 17:04 GeoLite2-Country.mmdb
drwxrwxr-x 4 someuser someuser 4.0K Aug 11 17:03 vendor/

Now you are ready to do a quick test, you can do so by creating an index.php file with the following content:

<?php
require_once 'vendor/autoload.php';

use MaxMind\Db\Reader;
$ipAddress = '8.8.8.8';
$databaseFile = './GeoLite2-City.mmdb';

$reader = new Reader($databaseFile);

print_r($reader->get($ipAddress));

$reader->close();

This index file will simply do a quick test to ensure that the database file is being loaded, an IP can be checked, and the results are being provided. The test will be looking up the geographic information related to Google’s DNS server at 8.8.8.8.

Geo-location results for Google's 8.8.8.8 DNS server via MaxMind
Geo-location results for Google’s 8.8.8.8 DNS server via MaxMind

Having followed the directions correctly you should see output similar to the image above when loading the test index page.

Working with Composer & Examples

Tutorial: How to use Composer
I. Composer 101
II. Installing Composer on cPanel servers
III. Working with Composer & Examples

In the previous articles we worked through what composer is, who uses it, and how to install it. Here we will cover some basic use case examples of how to acquire packages using the composer tool we previously setup.

The example documented in this article can be done either locally, or on your Liquid Web Fully Managed cPanel server, in either case these directions should be run as the user owning the website files. On a cPanel server this would mean you’re running these via SSH logged in as the cPanel user and you would be starting from within public_html.

Example #1: Getting GuzzleHTTP using Composer

One of the most popular PHP HTTP clients, Guzzle is a library that can make sending HTTP requests simple and easy. As a widely used and well-documented library, Guzzle is an easy package for any developer or designer to take advantage of.

To try out Guzzle run the following commands:

$ mkdir guzzleTest
$ cd ./guzzleTest/
$ composer require guzzlehttp/guzzle

Then create an index.php file in the same folder with the following content:

<?php
require_once 'vendor/autoload.php';
$client = new GuzzleHttp\Client();

// Make a request
$res = $client->request('GET', 'http://www.timeapi.org/utc/now.json');

// Output the status code of the response
echo 'Page response code: '.$res->getStatusCode();
echo "<hr/>";
// "200"

// Output headers of the response
echo 'Response Content-Type header: ';
print_r( $res->getHeader('content-type') );
echo "<hr/>";
// 'application/json; charset=utf8'

// Output the actual content (body) of the response
echo 'Response Body content: ';
echo $res->getBody();
echo "<hr/>";
?>
In the above PHP code, lines starting with ‘//’ are considered comments and are just there to help detail each step of script.

Opening the new index.php file in your browser should yield a page that shows: the HTTP response code, the ‘Content-Type’ header of the response provided, and the actual content of the response.

Example #2: Getting a framework

While composer is mainly used to get specific libraries and packages needed for a site to run, it’s also possible for a whole framework or CMS to be provided using composer. Laravel is one of many popular PHP frameworks that use composer to distribute their core files. An example of using composer to get Laravel can be done with the following commands:

$ composer create-project --prefer-dist laravel/laravel ./laraTest

Once this command has been executed composer will do a number of things for you; it will create the “laraTest” folder, initialize the composer.json file, get any necessary dependencies and then setup the Laravel-specific files.

To verify this example you will need some familiarity with the Laravel framework, however you can verify that composer did its job by checking the file structure. To check the file structure run the following command:

$ ls -lah

You should see a structure similar to:

total 200K
drwxr-xr-x 11 user users 4.0K Aug 8 13:17 .
drwxr-xr-x 10 user nginx 4.0K Aug 8 13:16 ..
drwxr-xr-x 10 user users 4.0K Apr 27 09:01 app
-rwxr-xr-x 1 user users 1.7K Apr 27 09:01 artisan
drwxr-xr-x 3 user users 4.0K Apr 27 09:01 bootstrap
-rw-r–r– 1 user users 1.3K Apr 27 09:01 composer.json
-rw-r–r– 1 user users 111K Aug 8 13:17 composer.lock
drwxr-xr-x 2 user users 4.0K Apr 27 09:01 config
drwxr-xr-x 5 user users 4.0K Apr 27 09:01 database
-rw-r–r– 1 user users 458 Aug 8 13:17 .env
-rw-r–r– 1 user users 423 Apr 27 09:01 .env.example
-rw-r–r– 1 user users 61 Apr 27 09:01 .gitattributes
-rw-r–r– 1 user users 73 Apr 27 09:01 .gitignore
-rw-r–r– 1 user users 503 Apr 27 09:01 gulpfile.js
-rw-r–r– 1 user users 212 Apr 27 09:01 package.json
-rw-r–r– 1 user users 1.1K Apr 27 09:01 phpunit.xml
drwxr-xr-x 2 user users 4.0K Apr 27 09:01 public
-rw-r–r– 1 user users 1.9K Apr 27 09:01 readme.md
drwxr-xr-x 5 user users 4.0K Apr 27 09:01 resources
-rw-r–r– 1 user users 567 Apr 27 09:01 server.php
drwxr-xr-x 5 user users 4.0K Apr 27 09:01 storage
drwxr-xr-x 2 user users 4.0K Apr 27 09:01 tests
drwxr-xr-x 29 user users 4.0K Aug 8 13:17 vendor

Installing Composer on cPanel servers

Tutorial: How to use Composer
I. Composer 101
II. Installing Composer on cPanel servers
III. Working with Composer & Examples

With a tool like Composer it is generally best to have the ability to run it as any user on the server and from any directory. This is generally referred to as being ‘globally installed’ as any user can access the tool from any location. In this guide we will detail how to install Composer globally on a cPanel based server.

Pre-Flight Check

  • These instructions are intended specifically for installing Composer on a cPanel based server running WHM prior to version 58.
  • We’ll be logging in as root to a Liquid Web Fully Managed cPanel server.
As of WHM version 58 cPanel is now including a version of Composer by default, this can be accessed by simply calling composer from command line.
For more details see: What’s New in WHM 58 & What to Look For

Step #1: Get the installer

Acquire the Composer installer script with the following commands:

$ EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '$EXPECTED_SIGNATURE') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

The above commands will: get the installer’s signature, get the installer file, verify the download by checking the signature and finally confirm if the installer is valid, or corrupt. You should see output similar to:

Installer verified

Step #2: Run the installer

To run the installer in a manner that will install composer globally run the following command:
php ./composer-setup.php --install-dir=/usr/local/bin --filename=composer

With this command run as root composer should now be globally installed on the server.

Step #3: Verify the install

In order to verify the composer installation was successfully you will want to do the following to test. First, as root, run the following command:
# composer -V

You should see something similar to:

Composer version 1.2.0 2016-07-19 01:28:52

If that checks out, you will then want to verify Composer works for the users as well. To confirm composer is working for the cPanel accounts SSH into your server as a cPanel user and do the same:

$ composer -V

You should see something similar to:

Composer version 1.2.0 2016-07-19 01:28:52

Composer 101

Tutorial: How to use Composer

Composer is a dependency manager for PHP, written in PHP. Specifically, it’s used to simplify the process of using PHP libraries in your projects. The use can range from getting a framework, including a library class, or open source projects; generally these packages are downloaded by Composer and then implemented by a developer in a website’s code.

Examples being: Silex MicroFramework, the infamous PHPMailer class, Laravel Framework, and many more – all of which can be found on Composer’s main repository Packagist.

To find a more documented list of classes, libraries, or frameworks that are available you can review the Packagist site.

So How Do I Use This Tool?

To use Composer you will want to be logged in via SSH, or TTY, and you will run the following command:

$ composer require {somepackage}

This will issue a command for Composer that essentially states you are requiring the provided package name in the current folder. What this does is requests the package given; if found, it then creates the following:

  • composer.json
  • /vendor
  • composer.lock

Ok, so that downloaded the necessary PHP files to use the library, or package, you are requesting. It created a “/vendor” folder where it downloaded all the ‘library’ files. Great, you’ve got the necessary files, but now what?

How Do I Use the Composer-provided Libraries?

To use Composer-provided libraries you may need to understand Composer a little bit better first; specifically you’ll need to understand how Composer’s autoloader works. Think of this as your ‘crash course’ to the autoloader.

To make more sense of this concept let’s go back to our ‘What is Composer’ section; as we know Composer is a “Dependency Manager for PHP”. This means it’s getting you a library, or some code, needed for your site or project. It will also grab any libraries, or other requirements, needed by that library to function – hence the name “dependency management tool”, it’s doing that part in the background for you. In order to make use of this tool, you need to make sure you’re properly including everything that is required.

That’s where Composer’s Autoloader comes in, the autoloader is a script which includes and loads any needed dependencies. Long story short, Composer will create for you a file called: “autoload.php” that is located in the “/vendor” folder it creates. This file is all you need to use/re-use the libraries, classes, or etc, that you are getting from Composer.

Using the Autoloader

In order to use a Composer-provided library you will simply need to add the following to your script:
require 'vendor/autoload.php';

Keep in mind that the part within single quotes should be the relative path to this file; this may need adjusting depending on how you are doing things.

Final Tips When Using Composer

Ultimately, including the `require autoloader.php` line allows you to utilize the packages, classes, libraries, etc, that you are asking composer to get for you. These dependencies are downloaded locally to where the ‘require’ command was run.

When working on cPanel servers you should only run and use composer as the cPanel user requiring those dependencies. Doing this will ensure the files are downloaded in a manner that keeps proper user ownership and permissions. However, if you accidentally run Composer as the root user you will simply need to adjust the file permissions and ownership accordingly.

While the usage of Composer is mainly done by developers, if you are experiencing any issues with file ownership, permissions, or if you have any questions feel free to contact our Heroic Support®.

Fully Managed Templates Now Optimized with FCGI

Your fully managed cPanel server has several different PHP handlers from which to choose, including DSO, SuPHP, and FCGI. These handlers are responsible for reading/interpreting PHP code, then compiling and executing it.

Selecting the best PHP handler for your server is critical to overall performance, but it’s not always an easy choice to make. While FCGI currently is the best-performing PHP handler available on cPanel, it can be somewhat difficult to properly configure.

Since we’re the best Managed Hosting company around, we decided to take care of the initial configuration for you to help make your decision that much easier.

Liquid Web has spent the past few months optimizing FCGI settings to achieve the best performance gains possible while lowering resource utilization and further improving stability. Today, we’re thrilled to announce that we’ve updated our Fully Managed CentOS 6 and CentOS 7 templates to include these optimizations by default!

What type of performance gain does FCGI provide over SuPHP? We’re glad you asked!

To measure performance, we installed a default WordPress site on a Zone C 16 GB Storm VPS, then used Apache Benchmark to hit the server with 10 concurrent connections, and a total of 1,000 requests.

PHP with FCGI and OPcache performed 5 times faster than suPHP in this simpla Apache Benchmark test

With SuPHP as the PHP handler, we were able to get around 30 requests per second. Once we switched the PHP handler to FCGI with our optimizations (which include enabling and configuring OPcache), we were able to get more than 150 requests per second! In this case we saw a 500% increase in performance!

These optimizations are not reserved for new Storm® VPS and Dedicated servers, either. If you have an existing fully managed cPanel server and would like to take advantage of the performance gains FCGI and OPcache can bring to your sites, let us know! Our Heroic Support® technicians can quickly examine your server’s current settings and walk you through any changes necessary to help your server realize its full potential with FCGI and OPcache.

How to Install XCache on Ubuntu 15.04

Pre-Flight Check

  • These instructions are intended specifically for installing XCache, an open-source opcode cacher, on Ubuntu 15.04.
  • I’ll be working from a Liquid Web Self Managed Ubuntu 15.04 server with Apache and PHP installed, and I’ll be logged in as non-root user. If you need more information then visit our tutorial on How to Add a User and Grant Root Privileges on Ubuntu 15.04.

Continue reading “How to Install XCache on Ubuntu 15.04”

How to Install XCache on Ubuntu 14.04 LTS

Pre-Flight Check

  • These instructions are intended specifically for installing XCache, an open-source opcode cacher, on Ubuntu 14.04 LTS.
  • I’ll be working from a Liquid Web Core Managed Ubuntu 14.04 LTS server with Apache and PHP installed, and I’ll be logged in as non-root user. If you need more information then visit our tutorial on How to Add a User and Grant Root Privileges on Ubuntu 14.04 LTS.

Continue reading “How to Install XCache on Ubuntu 14.04 LTS”

How to Install XCache on Ubuntu 12.04 LTS

Pre-Flight Check
  • These instructions are intended specifically for installing XCache, an open-source opcode cacher, on Ubuntu 12.04 LTS.
  • I’ll be working from a Liquid Web Core Managed Ubuntu 12.04 LTS server with Apache and PHP installed, and I’ll be logged in as non-root user. If you need more information then visit our tutorial on How to Add a User and Grant Root Privileges on Ubuntu 12.04 LTS.

Continue reading “How to Install XCache on Ubuntu 12.04 LTS”

How to Install XCache on Fedora 21

Pre-Flight Check
  • These instructions are intended specifically for installing XCache, an open-source opcode cacher.
  • I’ll be working from a Liquid Web Self Managed Fedora 21 server with Apache and PHP installed, and I’ll be logged in as non-root user. If you need more information then visit our tutorial on How to Add a User and Grant Root Privileges on Fedora 21.

Continue reading “How to Install XCache on Fedora 21”