Use Multiple PHP Versions with Virtualmin and Nginx

There are few major PHP versions out there such as PHP 5.6, PHP 7.2, PHP 7.4, PHP 8.0 and PHP 8.1. We should always try to use latest version of the software, sometimes we have to stick with an older version due to compatibility issues with the application we are running. If you’re in a such situation, this post will show you how to use multiple PHP versions with Virtualmin and Nginx server.

Why use multiple PHP versions with Virtualmin and Nginx?

I was in a such situation recently. I wanted to have WordPress and CPVLab installed on the same VPS. WordPress is compatible with PHP 7.2 but CPVLab isn’t. Only version of PHP CPVLab supported was PHP 5.6. While I could have both WordPress and CPVLab installed with PHP 5.6, that isn’t an optimal solution. So, I decided to install PHP 7.2 for WordPress and PHP 5.6 for CPVLab.

While my reason to use multiple PHP versions with Virtualmin and Nginx only involves PHP 5.6 and PHP 7.2, I’ll list instructions for all major PHP versions mentioned earlier. These are the PHP versions with ongoing support at the moment of writing.

And before we begin, I’m going to assume you have built your Ubuntu 20.04 server with Virtualmin and Nginx and have created at least one virtual server to host a website.

Add Ondřej Surý Repo to the server

Ubuntu 20.04 doesn’t offer newer versions of PHP than PHP v7.4 yet. We can get around this by adding ondrej/php PPA to the server. No matter which PHP version you’re planning to install, you should start by adding ondrej/php PPA to be able to use multiple PHP versions with Virtualmin and Nginx.

add-apt-repository ppa:ondrej/php

Update apt cache

apt update

Server is now ready to have multiple versions of PHP running at the same time. Let’s start with PHP 5.6. Note that Virtualmin, by default use latest version of PHP available on the server for new virtual servers. This can be changed within the virtual server settings itself after creation or you can specify a default PHP version for new servers from Virtualmin settings. More on that later on this tutorial.

Install PHP 5.6 and PHP v8.0 with Virtualmin and Nginx

We already have PHP v8.0 running on the server. Let’s install PHP 5.6. Following command will install PHP 5.6 and all essential PHP 5.6 modules.
apt install php5.6-memcache php5.6-imagick php5.6-redis php5.6-bcmath php5.6-intl php5.6-mcrypt php5.6-cgi php5.6-fpm php5.6-mysql php5.6-curl php5.6-gd php5.6-imap php5.6-tidy php5.6-xmlrpc php5.6-xml php5.6-xsl php5.6-mbstring php5.6-zip php5.6-cli php5.6-soap php5.6-gmp php5.6-sqlite3

Install PHP 7.0 and PHP v8.0 with Virtualmin and Nginx

And if you want PHP v7.0 on your server, following command will do the magic.
apt install php7.0-memcache php7.0-imagick php7.0-redis php7.0-bcmath php7.0-intl php7.0-mcrypt php7.0-cgi php7.0-fpm php7.0-mysql php7.0-curl php7.0-gd php7.0-imap php7.0-tidy php7.0-xmlrpc php7.0-xml php7.0-xsl php7.0-mbstring php7.0-zip php7.0-cli php7.0-soap php7.0-gmp php7.0-sqlite3

Install PHP 7.1 and PHP v8.0 with Virtualmin and Nginx

If the version of PHP you want is PHP 7.1, following command should take care of it.
apt install php7.1-memcache php7.1-imagick php7.1-redis php7.1-bcmath php7.1-intl php7.1-mcrypt php7.1-cgi php7.1-fpm php7.1-mysql php7.1-curl php7.1-gd php7.1-imap php7.1-tidy php7.1-xmlrpc php7.1-xml php7.1-xsl php7.1-mbstring php7.1-zip php7.1-cli php7.1-soap php7.1-gmp php7.1-sqlite3

Install PHP 7.2 and PHP v8.0 with Virtualmin and Nginx

And if you want PHP 7.2 on your server, use following command.
apt install php7.2-memcache php7.2-imagick php7.2-redis php7.2-bcmath php7.2-intl php7.2-mcrypt php7.2-cgi php7.2-fpm php7.2-mysql php7.2-curl php7.2-gd php7.2-imap php7.2-tidy php7.2-xmlrpc php7.2-xml php7.2-xsl php7.2-mbstring php7.2-zip php7.2-cli php7.2-soap php7.2-gmp php7.2-sqlite3

Install PHP 7.3 and PHP v8.0 with Virtualmin and Nginx

Following command will add PHP v7.3 to your server.
apt install php7.3-memcache php7.3-imagick php7.3-redis php7.3-bcmath php7.3-intl php7.3-mcrypt php7.3-cgi php7.3-fpm php7.3-mysql php7.3-curl php7.3-gd php7.3-imap php7.3-tidy php7.3-xmlrpc php7.3-xml php7.3-xsl php7.3-mbstring php7.3-zip php7.3-cli php7.3-soap php7.3-gmp php7.3-sqlite3

Install PHP 7.4 and PHP v8.0 with Virtualmin and Nginx

Add the popular PHP v7.4 with following command.
apt install php7.4-memcache php7.4-imagick php7.4-redis php7.4-bcmath php7.4-intl php7.4-mcrypt php7.4-cgi php7.4-fpm php7.4-mysql php7.4-curl php7.4-gd php7.4-imap php7.4-tidy php7.4-xmlrpc php7.4-xml php7.4-xsl php7.4-mbstring php7.4-zip php7.4-cli php7.4-soap php7.4-gmp php7.4-sqlite3

Install PHP 8.1 and PHP v8.0 with Virtualmin and Nginx

If you need PHP v8.1 to your server,
apt install php8.1-memcache php8.1-imagick php8.1-redis php8.1-bcmath php8.1-intl php8.1-mcrypt php8.1-cgi php8.1-fpm php8.1-mysql php8.1-curl php8.1-gd php8.1-imap php8.1-tidy php8.1-xmlrpc php8.1-xml php8.1-xsl php8.1-mbstring php8.1-zip php8.1-cli php8.1-soap php8.1-gmp php8.1-sqlite3
You can run one or more of above commands and have one or more extra PHP versions running on your server. If you run all commands above, you’ll have all major versions of PHP available to you on the server.

Configure Virtualmin to use a Specific PHP Version

Virtualmin will use latest available version of PHP for new virtual servers. Old virtual servers will not be affected by above commands. If you need a different version of PHP for existing virtual server, you can go to Virtualmin > Domain Name > Server Configuration > PHP Version and select the PHP version you want from the list.

use multiple PHP versions with Virtualmin and Nginx

And if you want to change default PHP version for new virtual servers to anything than the latest version available on the server, you can change it from Virtualmin > System Settings > Server Templates > Default Settings > PHP Options. A drop-down list of available PHP versions will be shown to you.

virtualmin-change-php-version

Select the version you want and click save. Selected PHP version will be used for all newly created servers. Option to change PHP version from virtual server settings will also be available to all newly created servers.

Set Default PHP Version for Command Line

When you have multiple php versions on your server, system will use latest version to process PHP commands. Although you can change this on Virtualmin as shown above, it doesn’t change default PHP version for command line. phpinfo page on your browser might say PHP 5.6 but PHP -v command on Putty might say PHP 7.2. This can lead to conflicts in some cases. Especially if your application need to run cronjobs. Solution will be to change default PHP version for command line to match what your application is running. This command will display PHP versions available to you with their respective paths,
update-alternatives --list php
Following command should change default php version to PHP 7.1.
update-alternatives --set php /usr/bin/php7.1
Change the path to PHP as your software demands.

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

Related Articles

7 Comments

  1. Hi Tharindu

    I have followed your tutorial and everything thing went very nicely but I get a 404 page on every website that I have tried. Only the index.php page works, I get 404 on every other pages except for the administrator directory in a Joomla installation. In that case the administration worsk fine
    Lots of search on Google abut well known problem but couldn’t find the solution to it.
    I was wandering if you have any idea on how to fix that.

    Thank you!
    Operating system Ubuntu Linux 16.04.5
    Perl version 5.022001
    Path to Perl /usr/bin/perl
    BIND version 9.10
    Postfix version 3.1.0
    Mail injection command /usr/lib/sendmail -t
    Webalizer version 2.23-08
    Logrotate version 3.8.7
    MySQL version 10.0.36-MariaDB-0ubuntu0.16.04.1
    ProFTPD version 1.35
    SpamAssassin version 3.4.2
    ClamAV version 0.100.2

    server {
    	server_name shopnature.ca www.shopnature.ca;
    	listen i removed the ip;
    	root /home/shopnature/public_html;
       index index.php index.html index.htm;
    	access_log /var/log/virtualmin/shopnature.ca_access_log;
    	error_log /var/log/virtualmin/shopnature.ca_error_log;
    	fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    	fastcgi_param SERVER_SOFTWARE nginx;
    	fastcgi_param QUERY_STRING $query_string;
    	fastcgi_param REQUEST_METHOD $request_method;
    	fastcgi_param CONTENT_TYPE $content_type;
    	fastcgi_param CONTENT_LENGTH $content_length;
    	fastcgi_param SCRIPT_FILENAME /home/shopnature/public_html$fastcgi_script_name;
    	fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    	fastcgi_param REQUEST_URI $request_uri;
    	fastcgi_param DOCUMENT_URI $document_uri;
    	fastcgi_param DOCUMENT_ROOT /home/shopnature/public_html;
    	fastcgi_param SERVER_PROTOCOL $server_protocol;
    	fastcgi_param REMOTE_ADDR $remote_addr;
    	fastcgi_param REMOTE_PORT $remote_port;
    	fastcgi_param SERVER_ADDR $server_addr;
    	fastcgi_param SERVER_PORT $server_port;
    	fastcgi_param SERVER_NAME $server_name;
    	fastcgi_param HTTPS $https;
    	location ~ \.php$ {
    		try_files $uri $uri/ /index.php?$args;
    		fastcgi_pass unix:/var/php-nginx/154519082726286.sock/socket;
    	}
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button