ispmanager 6 lite, pro, host

PHP Composer

About Composer

Composer is a tool for managing dependencies in PHP projects. The Composer module in ispmanager allows installing and updating libraries and packages automatically.

For example, you need the monolog/monolog logging library. You can specify the package name and version range (or just the package name), and the Composer module will:

  1. Check if dependencies for monolog/monolog are installed and download and install them if necessary.
  2. Download and install the monolog/monolog library.
  3. Add dependency information to the project configuration file.
  4. Generate an autoload.php file to autoload classes from the library into your project.
  5. Update the dependency if necessary when a new version is released.

Composer requirements

To install Composer

  • Control panel plan: lite, pro, or host

To work with Composer

  • The PHP Composer option is enabled for users in the Access section
  • The Use PHP Composer option is enabled for websites

For the `install` and `update` commands to function

  • The server must have access to a package repository (Packagist by default)
  • Websites must be enabled

For the separate PHP Composer Update command

Composer installation

Composer is installed in the root directory of the site for which it is enabled.

The version of Composer depends on the PHP version for the site:

  • the latest version of Composer requires PHP 7.2 and higher
  • for PHP 5.3–7.1, Composer 2.2.X are installed

To install Composer:

  1. Log in to the panel with superuser rights.
  2. Navigate to Software configuration → Web-server (WWW).
  3. Check the PHP Composer box.
  4. Click Save.

Composer will be installed in the background mode.

To make Composer available for use, enable it for users and sites.

Enabling Composer

For users

  1. Log in to the panel with an administrator-level account or above.
  2. Navigate to the Users section.
  3. Select a user and click  Pencil icon — Edit Edit.
  4. In the form that opens, go to the Access block.
  5. Check the box PHP Composer to enable access.
  6. Save the changes.

    User form, Access block, PHP Composer option selected

For sites

Composer must be enabled for the user that owns the site.

  1. Go to the Websites section.
  2. Select a site and click  Pencil icon — Edit Edit.
  3. In the form that opens, go to the Handler (PHP) block.
  4. Check the Use PHP Composer box.
  5. Save the changes.

    Website form, Handler (PHP) block, Use PHP Composer option selected

Managing dependencies with Composer

Only one Composer operation can be performed at a time on a single site.

Dependency versions

When installing a dependency, you can specify either a particular version of it or a range of acceptable versions. For example:

  • if the 1.2.3 version is specified, this version will be installed
  • if the ^1.2.3 version is specified, the latest compatible version will be installed, but not lower than 1.2.3 and not higher than 2.0.0

For more information about dependency versions, see the official Composer documentation.

Installing dependencies

Dependencies and information about them are stored in the site directory. When installed:

  • dependencies are installed in the vendor/ directory
  • dependency information is written into the composer.json configuration file
  • information about the specific installed version is registered in the composer.lock file. The composer.lock file is used to lock the versions of dependencies

If the software you are installing depends on other software, it will also be installed automatically.

Select the desired installation method:

  1. Go to the Websites section.
  2. Select the desired site.
  3. Click Three vertical dots icon — Context menu to open the context menu.
  4. In the list, click View PHP Composer packages.
  5. Click  Arrow into server icon — Install on the toolbar.
  6. In the PHP package names field, enter the package names, placing each one on a new line. Specifying a version is optional.
  7. Click Install. 

By default, dependencies are installed from the Packagist repository. If you need to install a dependency from a different repository, do so via the composer.json configuration file, specifying the required repository within it.

Installation is performed from a non-standard repository or from a custom configuration file in JSON format.

  1. Go to the Websites section.
  2. Select the desired site.
  3. On the toolbar, click File with gear icon — Configuration files Configuration files. This will open the composer.json configuration file.

    Details

    The composer.json file is the main Composer configuration file which contains dependency settings: names, versions, repositories, etc.

    composer.json example
    {
        "require": {
                "monolog/monolog": ">=1.0.2 <2.1.2"
        },
        "description": "This example",
        "keywords": ["testing", "example"],
        "type": "project",
        "version": "1.0.0",
        "homepage": "https://example.com",
        "readme": "readme.md",
        "license": "MIT",
        "authors": [{
                "name": "User",
                "email": "user@example.com",
                "homepage": "https://www.example.com",
                "role": "Developer"
        }],
        "support": {
                "email": "support@example.org"
        }
    }
    Commentary
    • require — library name and version to be installed — required field
    • description — project description
    • keywords — keywords for project search in the repository
    • type — type: library, project, metapackage or composer-plugin
    • version — project version
    • homepage — project home page
    • readme — path to the readme file
    • license — project license type
    • authors — information about project authors:
      • name — author name
      • email — author email address
      • homepage — author home page
      • role — project role
    • support — support team email address
  4. Copy the contents of your file into the file in the panel.
  5. Save the changes.

If the file content has changed, the panel will validate it upon saving and execute the PHP Composer Update command (./composer.phar update). Composer will then update the dependencies and overwrite the composer.lock file. If the validation fails, the file will revert to its previous content. If the file content has not changed, Composer will not be triggered.

  1. Go to the Websites section.
  2. Select the desired site.
  3. Click Three vertical dots icon — Context menu to open the context menu.
  4. In the list, click PHP Composer install.
  5. In the Installing dependencies window, click Install.

The PHP Composer install command executes ./composer.phar install in the site's root directory, on behalf of the site owner.

If the composer.lock file exists, the dependency versions are installed based on it, and the file itself remains unchanged. If no composer.lock file exists, Composer determines the appropriate dependency versions and creates the file.

Installed dependencies are displayed on the Installed PHP packages page. To access it, select a site, click Three vertical dots icon — Context menu to open the context menu, then click View PHP Composer packages.

Updating dependencies

The update will overwrite the composer.lock file. Please save a copy of the file before performing the update. To revert to previous versions of your dependencies, restore the file from your copy and run the PHP Composer install command.

After installing the dependencies, update their versions using one of the following methods:

  1. Go to the Websites section.
  2. Select the desired site.
  3. Click Three vertical dots icon — Context menu to open the context menu.
  4. In the list, click PHP Composer Update.
  5. In the Updating dependencies window, click Update.
  1. Go to the Websites section.
  2. Select the desired site.
  3. Click Three vertical dots icon — Context menu to open the context menu.
  4. In the list, click View PHP Composer packages.
  5. Click  Circular arrow icon — Update Update on the toolbar.

Composer will install the latest available versions of dependencies that satisfy the version constraints in composer.json and will update composer.lock.

Shell client

The Shell client allows running additional Composer commands. The terminal is launched on behalf of the site owner. When the terminal opens:

  • the working directory is changed to the site's home directory
  • the path to the executable file of the PHP version selected for the site is added to the PATH environment variable

Shell commands must contain the path to the composer.phar executable:

./composer.phar COMMAND

Removing dependencies

To remove a dependency:

  1. Go to the Websites section.
  2. Select the desired site.
  3. Click Three vertical dots icon — Context menu to open the context menu.
  4. In the list, click View PHP Composer packages.
  5. Select the package you want to remove.
  6. Click Cross in trash bin icon — Delete on the toolbar.
  7. Confirm the removal of the dependency.

Notes on removal:

  • an error will occur if you attempt to remove a dependency that is required by other dependencies
  • an error will occur if you attempt to remove a dependency while packages are being installed or updated
  • if one dependency has another dependency installed for it, both dependencies will be removed
Example

Assume you have the following packages installed:

  • intervention/image (a library for image manipulation)
  • guzzlehttp/guzzle (a dependency of intervention/image used for HTTP requests)

If you attempt to remove intervention/image:

  1. Composer will remove intervention/image.
  2. It will then check whether any other packages still require guzzlehttp/guzzle.
  3. If they do not (i.e., guzzle was installed solely for intervention/image), Composer will remove it as well.

Using dependencies in a project

To use the installed dependencies in your project, specify the path to the autoload.php file in the php section:

require_once('vendor/autoload.php')
Monolog dependency use example
<?php
require_once('vendor/autoload.php');
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logger = new Logger('channel-name');
$logger->pushHandler(new StreamHandler('app.log', Logger::DEBUG));
$logger->info('This is log');
$logger->warning('This is log warning');
$logger->error('This is log error');
?>

Backups

When you create a site backup, the site's Composer settings and installed libraries are backed up.

If Composer is disabled in the panel when you restore a user from a backup, the user and their site will be imported with dependencies already installed, but an error record will be added to the recovery log. For the user to continue using Composer, install it in the panel and enable it for the user and the site.

Composer logging

Operations with Composer are written to the logs:

  • /usr/local/mgr5/var/phpcomposerctl.log  — contains command calls and errors.
    To change the logging level, add the following line to the /usr/local/mgr5/etc/debug.conf configuration file:

    phpcomposerctl.*        9
  • /usr/local/mgr5/var/ispmgr.log — logs panel processes related to module operation.
    To change the logging level of the Composer module in the panel log, navigate to the Logging settings → ispmanager - PHP (ispmgr.php) section. 

Disabling Composer

After disabling Composer for a site or removing it in the Software configuration section, the installed dependencies are not removed.

Protecting Composer service data

When Composer is enabled, the following service files and directories are created in the site's home directory:

  • composer.phar — executable Composer file (only works with the CLI version of PHP)
  • composer.json — file describing all project dependencies
  • composer.lock — file with a list of all installed dependencies and their versions
  • vendor/ — directory to which dependencies are installed
  • .cache/ — directory storing cache of downloaded dependencies
  • .config/ — directory containing configuration settings and public keys
  • .local/ — directory that may store additional Composer data

To prevent service files and directories from being accessed via a browser, special includes are added to the configuration files of web servers for sites. If both Nginx and Apache are installed, includes are added to the configuration files of both.

After disabling Composer, if dependencies were installed for the site, includes in the configuration files are preserved. This is necessary so that service files and directories do not become accessible from a browser after disabling. If you remove the vendor/ directory or the composer.json file from the site directory, the includes will be deleted the next time the site settings are saved.

Example of an include:

The phpcomposer.conf files are only accessible on the server to the root user.

Contents of the /etc/nginx/modules-includes/phpcomposer/phpcomposer.conf file:

location ~* ^/composer\.(json|lock|phar)$|^/(vendor|\.config|\.cache|\.local)/ {
    deny all;
}
  • Debian-based OS — /etc/apache2/modules-includes/phpcomposer/phpcomposer.conf
  • RHEL-based OS — /etc/httpd/modules-includes/phpcomposer/phpcomposer.conf

File contents:

<LocationMatch "^/composer\.(json|lock|phar)$|^/(vendor|\.config|\.cache|\.local)/">
	<IfModule mod_authz_core.c>
		# Apache 2.4
		<RequireAny>
		Require all denied
		</RequireAny>
	</IfModule>
	<IfModule !mod_authz_core.c>
		# Apache 2.2
		Order Deny,Allow
		Deny from All
	</IfModule>
</LocationMatch>

Contents of the /usr/local/lsws/conf/modules-includes/phpcomposer/phpcomposer.conf file:

context exp: ^/composer\.(json|lock|phar)$|^/(vendor|\.config|\.cache|\.local)/ {
  allowBrowse 0
}