Actions

Install Different Version of PHP on Ubuntu

From InfoTechPedia

You can install different versions of PHP on Ubuntu. This will also show how to install the versions and use them on Virtualmin for different virtual servers on a Virtualmin/Webmin install.

Add the Repository

Ubuntu 16.06

Use the following on the command line.

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

Ubuntu 18.04

Use the following on the command line.

sudo add-apt-repository ppa:ondrej/php
sudo apt update

Additional Info

After the updates you can use sudo apt-get upgrade for Ubuntu 16.04 or sudo apt upgrade for Ubuntu 18.04. This should pull down the very latest version of PHP from the ondrej/php repository. Currently, as of this writing is PHP 7.4.

Install Specific Versions & Modules

Right now I am using the following. You can substitute 7.3 or 7.2 for 7.4. Just check the modules available per PHP version noted below.

Time to install.

Ubuntu 16.04

sudo apt-get install php7.4 php7.4-cli php7.4-common php7.4-cgi

Then the following modules (these are the ones I generally install):

sudo apt-get install php7.4-bcmath php7.4-bz2 php7.4-curl php7.4-dba php7.4-enchant php7.4-gd
php7.4-gmp php7.4-imap php7.4-interbase php7.4-intl php7.4-ldap php7.4-mbstring php7.4-mysql
php7.4-odbc php7.4-pgsql php7.4-pspell php7.4-snmp php7.4-soap php7.4-tidy php7.4-xml php7.4-xmlrpc 
php7.4-xsl php7.4-zip

Ubuntu 18.04

sudo apt install php7.4 php7.4-cli php7.4-common php7.4-cgi

Then I add the following modules as a start:

sudo apt  install php7.4-bcmath php7.4-bz2 php7.4-curl php7.4-dba php7.4-enchant php7.4-gd
php7.4-gmp php7.4-imap php7.4-interbase php7.4-intl php7.4-ldap php7.4-mbstring php7.4-mysql
php7.4-odbc php7.4-pgsql php7.4-pspell php7.4-snmp php7.4-soap php7.4-tidy php7.4-xml php7.4-xmlrpc 
php7.4-xsl php7.4-zip

Version Check

Check your version. Usually, the highest version is available with:

user@YourServer:~$ php -v
PHP 7.4.9 (cli) (built: Aug  7 2020 14:29:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies
user@YourServer:~$ php7.3 -v
PHP 7.3.21-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Aug  7 2020 14:43:42) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.21, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.21-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
user@YourServer:~$

Adding Modules

Check what modules are installed.

user@YourServer:~$ php7.4 -m
[PHP Modules]
apc
apcu
bcmath
bz2
calendar
Core
...truncated...
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
imagick
imap
interbase
intl
json
ldap
libxml
mbstring
mcrypt
mysqli
mysqlnd
...truncated...
xml
xmlreader
xmlrpc
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

user@YourServer:~$ 

Now check what modules are available in your apt-cache for the specific PHP version, 7.4 or 7.3 or 7.2, etc.

user@YourServer:~$ sudo apt-cache search php7.4
php-amqp - AMQP extension for PHP
php-apcu - APC User Cache for PHP
php-geoip - GeoIP module for PHP
php-igbinary - igbinary PHP serializer
php-imagick - Provides a wrapper to the ImageMagick library
php-mongodb - MongoDB driver for PHP
php-msgpack - PHP extension for interfacing with MessagePack
php-oauth - OAuth 1.0 consumer and provider extension
php-redis - PHP extension for interfacing with Redis 
...truncated...
php7.4-soap - SOAP module for PHP
php7.4-sqlite3 - SQLite3 module for PHP
php7.4-sybase - Sybase module for PHP
php7.4-tidy - tidy module for PHP
php7.4-xml - DOM, SimpleXML, XML, and XSL module for PHP
php7.4-xmlrpc - XMLRPC-EPI module for PHP
php7.4-xsl - XSL module for PHP (dummy)
php7.4-zip - Zip module for PHP

Need a module? From the list what is available in your apt-cache:

user@YourServer:~$ sudo apt-get install php7.4-tidy

For Ubuntu 18.04

user@YourServer:~$ sudo apt install php7.4-tidy

Now you have multiple versions of PHP installed on your server.

Virtualmin

If you are running Webmin w/Virtualmin it is pretty easy to make the versions available for specific virtual hosts (websites) on your Virtualmin Install. This pertains to the latest Webmin 1.954 w/Virtualmin 6.11.

  • Log on to the Webmin admin https://yourserver.com:10000
  • System Settings -> Re-check configuration
  • All installed versions of PHP should be found.
  • Manage each Virtual Server -> select the proper Virtual Server from Dropdown list at top of Navigation Menu, example, yourdomain.org, you want to manage.
  • Then select Server Configuration -> PHP Versions
  • Select the version of PHP for the Virtual Server. Then "Save Versions" button.

All the correct php.ini files and directories should be created in your /home/{username}/ directory.