How To Install and Use PHP Composer on Ubuntu 18.04

Introduction

PHP Composer is an application that tracks dependencies for a project. It does not replace system package managers like apt or yum.

Composer allows you to specify a set of libraries for a specific project then identifies the versions and dependencies and installs them.

This guide will show you how to install and get started with PHP Composer in Ubuntu 18.04.

tutorial on installing and using php composer on ubuntu

Prerequisites

Steps For Installing PHP Composer on Ubuntu

Step 1: Update Local Repository

Start by updating the local repository lists by enter the following in a command line:

sudo apt-get update

Step 2: Download the Composer Installer

To download the Composer installer, use the command:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

Step 3: Verify Integrity of the Download

1. Visit the Composer Public Keys page. Copy the Installer Signature (SHA-384).

2. Set the code shell variable:

COMPOSER=48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5

3. Run the script below to compare the official hash against the one you downloaded:

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$COMPOSER') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

The script will either tell you the download is verified, or that it has been corrupted. If it’s corrupted, re-download the file.

Step 4: Install PHP Composer

1. Installing PHP Composer requires curl, unzip, and a few other utilities. Install them by entering the following:

sudo apt-get install curl php-cli php-mbstring git unzip
install supporting software for composer

Install Composer as a command accessible from the whole system.

2. To install to /usr/local/bin. enter:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

The installer should output:

All settings correct for using Composer
Downloading...

Composer (version 1.6.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

3. Once the installer finishes, verify the installation:

composer --version

The system should output the version number installed, for example:

Composer version 1.8.6 2019-6-11 15:03:05

4. To remove the installer:

php -r “unlink(‘composer-setup.php’);”

Basic Composer Usage

Composer is designed to track dependencies on a per-project basis. This makes it easier for other users to create the same environment. Composer uses a composer.json file to keep track of required software and allowed versions.

It also uses a composer.lock file to maintain consistency if someone copies the directory. These files are automatically generated using the require command.

1. Open a terminal, and enter the following:

mkdir c_sample
cd c_sample

2. Next, you’ll need to choose a package to load. The website packagist.org has a broad range of different PHP packages for download. In this example, let’s use the monolog/monolog package. You can follow the instructions, or search the website for monolog.

3. In the terminal window, enter:

composer require monolog/monolog

The system will download the software and create the composer.json and composer.lock files.

Note: Monolog is a package for managing logfiles. The name before the slash is the vendor, and the name after the slash is the package name.

4. Once the process completes, list the contents of the directory:

ls -l

You should see the composer.json and composer.lock files, along with a vendor directory.

5. To view the contents of the composer.json file:

cat composer.json

The system will show you that it has added the monolog software. The carat ^ sign beside the version number indicates the minimum version of the software.

Setting Up Autoloading

PHP doesn’t automatically load classes. However, you can configure Composer to autoload classes for you. This makes working with dependencies much easier.

1. Create a new file using your favorite text editor:

sudo nano composer_sample.php

2. Enter the following into the file:

<?php

require __DIR__ . '/vendor/autoload.php';

use Monolog\Logger;

use Monolog\Handler\StreamHandler;

 

// create a log channel

$log = new Logger('name');

$log->pushHandler(new StreamHandler('/~/c_sample/text.log', Logger::WARNING));

 

// add records to the log

$log->warning('Foo');

$log->error('Bar');
example of autoloading classes

3. Write the file (Ctrl+O), and exit (Ctrl+X).

4. Now you can run the command to autoload monolog:

php composer_sample.php

Updating Dependencies

To update all the dependencies in your composer.json file enter the command:

composer update

This will update all dependencies according to the version specified in the file.

To update one (or more) dependencies individually:

composer update vendor/package vendor_b/package_b

Conclusion

Now you have a good understanding of how to install and start using PHP Composer. We also covered setting up autoloading and updating dependencies.

Installation and Configuration of Virtualmin/Webmin in Ubuntu 20.01 with Multiple PHP version and MariaDB

Install Virtualmin on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade
sudo apt install software-properties-common apt-transport-https wget

Step 2. Installing Virtualmin on Ubuntu 20.04.

Before installing Virtualmin, now we install Webmin because Webmin uses a plugin called Virtualmin to simplify the management of multiple virtual hosts through a single interface, similar to cPanel or Plesk.

Now install Webmin using the official repository on both Debian and non-Debian-based systems. We can install Webmin using the Webmin APT repository on Ubuntu as shown below:

sudo nano /etc/apt/sources.list
# Add these lines at last
deb http://download.webmin.com/download/repository sarge contrib
deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib

# Save and exit the editor

Next, install the GPG key as shown below. We need the GPG key to trust the repository:

wget -q http://www.webmin.com/jcameron-key.asc -O- | sudo apt-key add -

Now we can install the most recent version of Webmin using the following commands:

sudo apt update
sudo apt install webmin

Step 3. Configure Firewall.

By default, Webmin listens for connections on port 10000 on all network interfaces. You need to open the port in your firewall so that the Webmin interface is accessible from the Internet:

sudo ufw allow 10000
sudo ufw reload

Step 4. Accessing Webmin Web Interface.

To login to Webmin’s dashboard, open up your browser and browse your server’s IP as follows:

https://your-server-ip-address:10000/

Step 5. Installing Virtualmin on Ubuntu 20.04.

Virtualmin provides an install script that allows for an easy installation. Run the commands below to download the Virtualmin script:

curl -O http://software.virtualmin.com/gpl/scripts/install.sh

Once downloaded, make the script executable:

sudo chmod +x install.sh

Finally, run the commands below to install Virtualmin:

sudo ./install.sh

The script will display a warning message about existing data and compatible operating systems. Press y to confirm that you want to continue the installation.

Step 6. Accessing Virtualmin Web Interface.

Virtualmin web-based monitoring will be available on HTTPS port 10000 by default. Open your favorite browser and navigate to http://your-domain.com:10000 or http://server-ip-address:10000.

Congratulations! You have successfully installed Virtualmin. Thanks for using this tutorial for installing the Virtualmin on your Ubuntu 20.04 LTS Focal Fossa system.

Multiple PHP Versions

## Installing PHP 5.6 and/or 7.4 and/or 8.0 on Debian 9/10

# Enable the [DEB.SURY.ORG](https://deb.sury.org/#debian-dpa “DEB.SURY.ORG”) repository:

apt-get -y install apt-transport-https lsb-release ca-certificates curl
curl -sSL -o /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

# Update all repositories:

apt update

# Install PHP packages version 5.6 and/or 7.4 and/or 8.0:

apt-get install php5.6-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}
apt-get install php7.4-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}
apt-get install php8.0-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}

Lastly, see the information on [configuring the second PHP version](#toc-configuring-the-second-php-version-in-virtualmin-ooq1ksVU “configuring the second PHP version”) below.

## Installing PHP version 5.6 and/or 7.3 and/or 8.0 on Ubuntu 16.04, 18.04 and 20.04

# Install the PPA:

add-apt-repository ppa:ondrej/php && apt-get update

# Install the PHP packages:

apt-get install php5.6-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}
apt-get install php7.3-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}
apt-get install php8.0-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}

Lastly, see the information on [configuring the second PHP version](#toc-configuring-the-second-php-version-in-virtualmin-ooq1ksVU “configuring the second PHP version”) below.

## Configuring the second PHP Version in Virtualmin

Once you have completed the installation of a second PHP version on your server, you can verify that Virtualmin sees it by logging into Virtualmin, and clicking System Settings -> Re-Check Config. You should see something like this:

The following PHP versions are available : 5.3.3 (/usr/bin/php-cgi), 5.6.25 (/opt/rh/rh-php56/root/usr/bin/php-cgi), 7.0.10 (/opt/rh/rh-php70/root/usr/bin/php-cgi)

You can configure which one is the default PHP version used on new Virtual Servers. The default is to use the newest available. You can change that default in System Settings -> Server Templates -> Default -> PHP Options, and on that screen you can set the default PHP version to use in the field **Default PHP version**.

## Configuring Individual Virtual Servers

You can configure the PHP version being used for a specific Virtual Server by selecting Server Configuration -> PHP Versions.

The first line there specifies what PHP version will be used by default.

If you wish, you can specify a different PHP version to be used for a specific directory.

Source: https://idroot.us/install-virtualmin-ubuntu-20-04/

https://www.virtualmin.com/multiplephp/

PHP script not executing on Apache server

Update for php7.x (tested on Ubuntu 16.04, 16.10, 18.04, 18.10)

Thanks to the comments, I update the answer for php7.x.

Install:

sudo apt-get install apache2 php7.x libapache2-mod-php7.x 

Verify:

a2query -m php7.x

Load:

sudo a2enmod php7.x

Restart apache:

sudo service apache2 restart

Multiple PHP Versions on Virtualmin

Multiple PHP Versions


Introduction

If you have multiple PHP versions installed, Virtualmin allows you to choose which version to use for a given domain, or even per-directory.

While this feature can work with any Linux distribution, it works best with 64 bit [CentOS (6 and above)](#toc-installing-php-72-on-centos-6-and-7-yTjimn_w “CentOS (6 and above)”) using the CentOS Software Collections repository, or on [Debian 10](#toc-installing-php-56-70-71-72-73-on-debian-10-buster-EkAj9qSJ “Debian 10”) using a PPA, or on [Ubuntu 14.04](#toc-configuring-the-second-php-version-in-virtualmin-ooq1ksVU “Ubuntu 14.04”) using a PPA.

Installing PHP 7.3 on CentOS 6 and 7

Notes about CentOS

  •  CentOS has a special repository called “Software Collections”, which is a system for installing multiple software versions on one server. Using packages provided by in the Software Collections repository, it’s possible to install more recent PHP versions /opt/, while keeping the default PHP version installed in /usr/.
  • The repository providing these packages currently only provides 64 bit packages. If you are using 32 bit CentOS, there are unfortunately no PHP packages for that.

Install the SCL Repo

yum -y install centos-release-scl

Install the PHP packages

yum -y install rh-php73-php-{cli,pdo,fpm,zip,gd,xml,mysqlnd,opcache}

Installing PHP 7.4 and/or 8.0 on CentOS 7

Install Remi Release repo and clear cache

yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm && yum clean all

Install PHP packages version 7.4 and/or 8.0

yum -y install php74-php-{cli,pdo,fpm,zip,gd,xml,mysqlnd,opcache}
yum -y install php80-php-{cli,pdo,fpm,zip,gd,xml,mysqlnd,opcache}

Installing PHP 7.3, 7.4 and/or 8.0 on CentOS 8

Install Remi Release repo and clear cache

dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm && dnf clean all

Install PHP packages version 7.3 and/or 7.4

dnf -y install php73-php-{cli,pdo,fpm,zip,gd,xml,mysqlnd,opcache}
dnf -y install php74-php-{cli,pdo,fpm,zip,gd,xml,mysqlnd,opcache}
dnf -y install php80-php-{cli,pdo,fpm,zip,gd,xml,mysqlnd,opcache}

Installing PHP 5.6 and/or 7.4 and/or 8.0 on Debian 9/10

Enable the deb.sury.org repository

apt-get -y install apt-transport-https lsb-release ca-certificates curl
curl -sSL -o /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

Update all repositories

apt update

Install PHP packages version 5.6 and/or 7.4 and/or 8.0

apt-get install php5.6-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}
apt-get install php7.4-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}
apt-get install php8.0-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}

Installing PHP version 5.6 and/or 7.3 and/or 8.0 on Ubuntu 18.04 and 20.04

Install the PPA

add-apt-repository ppa:ondrej/php && apt-get update

Install the PHP packages

apt-get install php5.6-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}
apt-get install php7.3-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}
apt-get install php8.0-{cli,pdo,fpm,zip,gd,xml,mysql,cgi}

Configuring the second PHP Version in Virtualmin 

Once you have completed the installation of a second PHP version on your server, you can verify that Virtualmin sees it by logging into Virtualmin, and clicking System Settings -> Re-Check Config. You should see something like this:

The following PHP versions are available : 5.3.3 (/usr/bin/php-cgi), 5.6.25 (/opt/rh/rh-php56/root/usr/bin/php-cgi), 7.0.10 (/opt/rh/rh-php70/root/usr/bin/php-cgi)

You can configure which one is the default PHP version used on new Virtual Servers. The default is to use the newest available. You can change that default in System Settings -> Server Templates -> Default -> PHP Options, and on that screen you can set the default PHP version to use in the field Default PHP version.

Configuring Individual Virtual Servers

You can configure the PHP version being used for a specific Virtual Server by selecting Server Configuration -> PHP Options.

The first line there specifies what PHP version will be used by default.

If you wish, you can specify a different PHP version to be used for a specific directory.

phpoffice/phpspreadsheet[1.18.0, …, 1.20.0] require ext-zip * -> it is missing from your system. Install or enable PHP’s zip extension.

composer require <vendor><package name>

Error:

phpoffice/phpspreadsheet[1.18.0, …, 1.20.0] require ext-zip * -> it is missing from your system. Install or enable PHP’s zip extension.

Answer:

Install using (to install the default version):
sudo apt install php-zip

Or, if you’re running a specific version of PHP:

# For php v7.0
sudo apt-get install php7.0-zip

# For php v7.1
sudo apt-get install php7.1-zip

# For php v7.2
sudo apt-get install php7.2-zip

# For php v7.3
sudo apt-get install php7.3-zip

# For php v7.4
sudo apt-get install php7.4-zip