Use Multiple PHP Versions With Virtualmin and Nginx

There are few major PHP versions out there such as PHP 5.6, PHP 7.0, PHP 7.1 and PHP 7.2. 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 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 four 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 16.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 16.04 doesn’t offer newer versions of PHP than PHP v7.3 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 7.3 with Virtualmin and Nginx

We already have PHP 7.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 memcached php-memcache php-pear php-imagick php5.6-cgi php5.6-mysql php5.6-curl php5.6-gd php5.6-imap php5.6-mcrypt php5.6-tidy php5.6-xmlrpc php5.6-xsl php5.6-mbstring php5.6-zip php5.6-cli

Install PHP 7.1 and PHP 7.3 with Virtualmin and Nginx

If the version of PHP you want is PHP 7.1, following command should take care of it.

apt install memcached php-memcache php-pear php-imagick php7.1-cgi php7.1-mysql php7.1-curl php7.1-gd php7.1-imap php7.1-mcrypt php7.1-tidy php7.1-xmlrpc php7.1-xsl php7.1-mbstring php7.1-zip php7.1-cli

Install PHP 7.2 and PHP 7.3 with Virtualmin and Nginx

And if you want PHP 7.2 on your server, use following command.

apt install memcached php-memcache php-pear php-imagick php7.2-cgi php7.2-mysql php7.2-curl php7.2-gd php7.2-imap php7.2-tidy php7.2-xmlrpc php7.2-xsl php7.2-mbstring php7.2-zip php7.2-cli

Install PHP 7.0 and PHP 7.3 with Virtualmin and Nginx

And if you want latest version (PHP 7.3) on your server, following command will do the magic.

apt-get install memcached php7.0-mysql php7.0-curl php7.0-gd php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php7.0-zip

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 three commands above, you’ll have all four version 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 other 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.

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 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 needs.

Show More


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


  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 {
    	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

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.