How To Install MultiPHP Version with Nginx on Ubuntu 16.04/18.04/19.04 LTS Server

DigitalOcean Referral Badge
Start your VPS now with FREE $100 credit.

If you’re running Nginx on an Ubuntu LTS Server, you might want to configure your server with MultiPHP Version. Of course, this is very useful if you have a site that is only compatible with old stable PHP versions, or some of your web applications require a different PHP version. For example, WordPress as the most popular open source CMS to date strongly recommends users to use the latest stable PHP 7.3 on their hosting server.

However, most problems come when you use a theme which is apparently only compatible with PHP 5.6 or PHP 7.0. Although WordPress recommends their users to use PHP 7.3, but the problem still comes if we use a theme that is not yet compatible with PHP 7.3. Well, the best solution is to setup MultiPHP Version for our server, so we can adjust the PHP version to fit our web theme requirements.

In this guide I will show you how to install and configure MultiPHP Version with FastCGI Process Manager (FPM) and Nginx on Ubuntu LTS Server. This tutorial applies to almost all Ubuntu LTS Servers, starting from 16.04.6 LTS (Xenial Xerus), 18.04 LTS (Bionic Beaver) and 19.04 LTS (Disco Dingo).

1.) Login to your server with root privileges via the SSH console (Windows users can use PuTTy). Then install the following packages:

$ apt-get install python-software-properties -y
$ apt-get install software-properties-common -y

2.) Next, we can use PPA (Personal Package Archives) from Ondrej Surý in order to install MultiPHP Version on our Ubuntu system. You don’t need to worry because ppa:ondrej/php is very safe to use on our Ubuntu LTS Server. Ok, let’s add the ppa:ondrej/php repository.

$ LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
$ apt-get update

3.) After we successfully add the ppa:ondrej/php repository to our Ubuntu system, we can go to the next step to install MultiPHP version.

$ sudo apt-get install php5.6 php5.6-common php5.6-mysql php5.6-xmlrpc php5.6-curl php5.6-cgi php5.6-cli php5.6-fpm php5.6-json php-pear php5.6-dev php5.6-imap php5.6-mcrypt php5.6-mbstring memcached php-memcache php5.6-xml php5.6-tidy php5.6-sqlite3 php5.6-xsl php5.6-gmp php5.6-gd zip unzip php5.6-zip php5.6-soap php-redis
$ sudo apt-get install php7.0 php-xdebug php-apcu php7.0-common php7.0-mysql php7.0-xmlrpc php7.0-curl php7.0-gd php7.0-cgi php7.0-json php7.0-cli php7.0-fpm php-pear php7.0-dev php7.0-imap php7.0-mcrypt php7.0-mbstring memcached php-memcache php7.0-xml php7.0-tidy php7.0-sqlite3 php7.0-xsl php7.0-gmp php7.0-zip php7.0-soap php-redis
$ sudo apt-get install php7.1 php-xdebug php-apcu php7.1-common php7.1-mysql php7.1-xmlrpc php7.1-curl php7.1-gd php7.1-cgi php7.1-json php7.1-cli php7.1-fpm php-pear php7.1-dev php7.1-imap php7.1-mcrypt php7.1-mbstring memcached php-memcache php7.1-xml php7.1-tidy php7.1-sqlite3 php7.1-xsl php7.1-gmp php7.1-zip php7.1-soap php-redis
$ sudo apt-get install php7.2 php-xdebug php-apcu php7.2-common php7.2-mysql php7.2-xmlrpc php7.2-curl php7.2-gd php7.2-cgi php7.2-json php7.2-cli php7.2-fpm php-pear php7.2-dev php7.2-imap php7.2-mcrypt php-mcrypt php7.2-mbstring memcached php-memcache php7.2-xml php7.2-tidy php7.2-sqlite3 php7.2-xsl php7.2-gmp php7.2-zip php7.2-soap php-redis
$ sudo apt-get install php7.3 php-xdebug php-apcu php7.3-common php7.3-mysql php7.3-xmlrpc php7.3-curl php7.3-gd php7.3-cgi php7.3-json php7.3-cli php7.3-fpm php-pear php7.3-dev php7.3-imap php7.3-mcrypt php-mcrypt php7.3-mbstring memcached php-memcache php7.3-xml php7.3-tidy php7.3-sqlite3 php7.3-xsl php7.3-gmp php7.3-zip php7.3-soap php-redis

For Ubuntu 18.04 LTS (Bionic Beaver) run the following command to install PHP 7.2:

$ apt-get install php7.2 php7.2-common php7.2-mysql php7.2-xmlrpc php7.2-curl php7.2-gd php7.2-cgi php7.2-json php7.2-cli php7.2-fpm php-pear php7.2-dev php7.2-imap php7.2-mbstring php7.2-xml php7.2-tidy php7.2-sqlite3 php7.2-xsl php7.2-gmp php7.2-zip php-redis php7.2-soap mcrypt gcc make autoconf libc-dev pkg-config libmcrypt-dev
$ sudo pecl install mcrypt-1.0.1

4.) Start all PHP-FPM services

$ service php5.6-fpm start
$ service php7.0-fpm start
$ service php7.1-fpm start
$ service php7.2-fpm start
$ service php7.3-fpm start

5.) Open the PHP-FPM configuration and edit each www.conf file. Now we can choose to use PHP-FPM to listen on TCP/IP socket or UNIX sockets.

$ nano /etc/php/5.6/fpm/pool.d/www.conf
$ nano /etc/php/7.0/fpm/pool.d/www.conf
$ nano /etc/php/7.1/fpm/pool.d/www.conf
$ nano /etc/php/7.2/fpm/pool.d/www.conf
$ nano /etc/php/7.3/fpm/pool.d/www.conf

Please note that PHP-FPM connections with UNIX sockets are much faster than TCP/IP sockets. However, overall UNIX sockets are less scalable compared to TCP/IP sockets.

READ:  How To Install phpMyAdmin Manually on CentOS 7 with NGINX & PHP-FPM

Okay, let’s set it to run PHP-FPM with TCP/IP sockets.

For PHP 5.6 >> listen = 127.0.0.1:9000
For PHP 7.0 >> listen = 127.0.0.1:9001
For PHP 7.1 >> listen = 127.0.0.1:9002
For PHP 7.2 >> listen = 127.0.0.1:9003
For PHP 7.3 >> listen = 127.0.0.1:9004

Or if you want to make PHP-FPM listen on UNIX sockets, make sure to uncomment the following line:

For PHP 5.6 >> listen = /run/php/php5.6-fpm.sock
For PHP 7.0 >> listen = /run/php/php7.0-fpm.sock
For PHP 7.1 >> listen = /run/php/php7.1-fpm.sock
For PHP 7.2 >> listen = /run/php/php7.2-fpm.sock
For PHP 7.3 >> listen = /run/php/php7.3-fpm.sock

6.) Open your nginx domain serverblock (vhosts). Replace yourdomain.com with your actual domain name. Well, in this guide I have commented on all PHP-FPM versions (5.6 – 7.3) both with UNIX socket and TCP/IP socket connections.

$ nano /etc/nginx/sites-available/yourdomain.com
# Fastcgi-Cache Config
location ~ \.php$ {
         try_files $uri =404;
         #fastcgi_pass 127.0.0.1:9000;
         #fastcgi_pass 127.0.0.1:9001;
         #fastcgi_pass 127.0.0.1:9002;
         #fastcgi_pass 127.0.0.1:9003;
         #fastcgi_pass 127.0.0.1:9004;
         #fastcgi_pass unix:/run/php/php5.6-fpm.sock;
         #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
         #fastcgi_pass unix:/run/php/php7.1-fpm.sock;
         #fastcgi_pass unix:/run/php/php7.2-fpm.sock;
         #fastcgi_pass unix:/run/php/php7.3-fpm.sock;
         include fastcgi_params;
         include fastcgi.conf;
         fastcgi_param HTTP_PROXY "";
         fastcgi_index index.php;
         #fastcgi_intercept_errors on;
         fastcgi_cache_bypass $no_cache;
         fastcgi_no_cache $no_cache;
         fastcgi_cache microcache;
         fastcgi_cache_valid 200 60m;
}

For example, we want to use PHP7.3-FPM and make it to listen on UNIX socket connection. Now make sure to uncomment “fastcgi_pass unix:/run/php/php7.3-fpm.sock;” line. If you want to use the PHP5.6-FPM version, you must uncomment “fastcgi_pass 127.0.0.1:9000;” or “fastcgi_pass unix:/run/php/php5.6-fpm.sock;” lines.

# Fastcgi-Cache Config
location ~ \.php$ {
         try_files $uri =404;
         #fastcgi_pass 127.0.0.1:9000;
         #fastcgi_pass 127.0.0.1:9001;
         #fastcgi_pass 127.0.0.1:9002;
         #fastcgi_pass 127.0.0.1:9003;
         #fastcgi_pass 127.0.0.1:9004;
         #fastcgi_pass unix:/run/php/php5.6-fpm.sock;
         #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
         #fastcgi_pass unix:/run/php/php7.1-fpm.sock;
         #fastcgi_pass unix:/run/php/php7.2-fpm.sock;
         fastcgi_pass unix:/run/php/php7.3-fpm.sock;
         include fastcgi_params;
         include fastcgi.conf;
         fastcgi_param HTTP_PROXY "";
         fastcgi_index index.php;
         #fastcgi_intercept_errors on;
         fastcgi_cache_bypass $no_cache;
         fastcgi_no_cache $no_cache;
         fastcgi_cache microcache;
         fastcgi_cache_valid 200 60m;
}

7.) Restart Nginx and PHP-FPM Services

$ service nginx restart
$ service php5.6-fpm restart
$ service php7.0-fpm restart
$ service php7.1-fpm restart
$ service php7.2-fpm restart
$ service php7.3-fpm restart

CONCLUSION

We can install Nginx without panels on the Ubuntu LTS server and make it run on the MultiPHP version with FastCGI Process Manager (FPM). Although overall it will be very complicated for a beginner to configure MultiPHP-FPM with Nginx on an Ubuntu LTS server without a control panel.

READ:  Enabling PHP zip extension in cPanel Hosting

However, there are many benefits that we will get with this configuration. One of them greatly saves server resources, including memory (RAM) and CPU consumption. Especially if you currently use a Virtual Private Server (VPS) or Cloud Server with limited resources, both vCPU and vRAM. Since it’s a virtualization server, resources must be shared with other users, even if you use KVM.

So running Nginx with MultiPHP-FPM version on an Ubuntu LTS server is the right solution to save server resources. The most important thing is that your server’s performance will be much more stable and balanced compared to the server configuration with control panel. Finally, of course, you will have a much lighter web server configuration.

Leave a Comment