Running multiple PHP versions in CentOS 7 without cPanel (port binding method)

Makresh NayakMakresh Nayak
3 min read

FPM (FastCGI Process Manager) is a primary PHP FastCGI implementation containing some features (mostly) useful for heavy-loaded sites.

Update your centos 7 server:

sudo yum update

Install Apache webserver:

sudo yum install httpd

Install REMI repository:

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Install therequired PHP versions (e.g., PHP 7.2 , PHP 7.3, PHP 7.4, PHP 8.0):

yum install php72-php-fpm
yum install php73-php-fpm 
yum install php74-php-fpm 
yum install php80-php-fpm

Install all extension for all versions of PHP:

yum install php72-php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,pear,bcmath,json,opcache,sodium,soap}
yum install php73-php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,pear,bcmath,json,opcache,sodium,soap}
yum install php74-php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,pear,bcmath,json,opcache,sodium,soap}
yum install php80-php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,pear,bcmath,json,opcache,sodium,soap}

Configure SELinux policy settings, including port definitions. In the command provided below for each PHP version:

semanage port -a -t http_port_t -p tcp 9072
semanage port -a -t http_port_t -p tcp 9073
semanage port -a -t http_port_t -p tcp 9074
semanage port -a -t http_port_t -p tcp 9008

Configure PHP-FPM file for port for each PHP version:

nano /etc/opt/remi/php72/php-fpm.d/www.conf
nano /etc/opt/remi/php73/php-fpm.d/www.conf
nano /etc/opt/remi/php74/php-fpm.d/www.conf
nano /etc/opt/remi/php80/php-fpm.d/www.conf

Find and update the listen parameter to use a different port number for different versions of php, such as 9072 for php 7.2.

Follow this step for conf file for all the versions of php you have installed on your server. Replace the php version with the php you have installed on your server. It should look like this

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on a specific port;
;   'port'                 - to listen on a TCP socket to all addresses (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9072

Enable and start all PHP-FPM versions:

systemctl enable php72-php-fpm
systemctl start php72-php-fpm

Configure Apache virtual host:

create a new Apache virtual host configuration file for your first domain:

nano /etc/httpd/conf.d/domain1.conf

add the following content in virtual host file:

<VirtualHost *:80>
    ServerName domain1.com
    DocumentRoot /var/www/domain1

<Directory /var/www/html/domain1>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9072" 
    </FilesMatch>
</VirtualHost>

Replace the domain1.com with your domain name and create directories according to domain name.

Repeat this step for all the domains you have created (e.g. domain2.conf, domain3.conf and vice versa)

Create the document root directories for your domains:

Repeat this for every domain you have created.

Set appropriate ownership and permissions for the document root directories:

chown -R apache:ftpusers /var/www/domain1/
chown -R apache:ftpusers /var/www/domain2/

Start and enable the Apache:

sudo systemctl start httpd
sudo systemctl enable httpd

Now, when you access domain1.com, Apache will use PHP 7.2 through the PHP-FPM configured on port 9072, and when you access domain2.com, it will use PHP 7.3 through the PHP-FPM configured on port 9073 and vice versa.

Test the Configuration:

Verify that the correct PHP version is being used for each domain by creating a PHP file (info.php) in each doamin's document root (/var/www/domain1/ , /var/www/doamin2 etc) with the following content:

<?php 
phpinfo(); 
?>

FEW IMPORTANT POINTS:

  • You can change the ports on virtual host configuration file to change the version of php that your domain uses.

  • The listen directive is in a php.ini config file of each php version specifies the address and port on which PHP-FPM should listen for FastCGI requests.

  • Make sure the ports that you are assigning to are free and not predifined by any processes of linux.

0
Subscribe to my newsletter

Read articles from Makresh Nayak directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Makresh Nayak
Makresh Nayak