In this tutorial we will set up multiple PHP Versions which is 5.6 & 7.2 on the Apache same machine.

it’s very practical and common to have multiple php versions and run it simultaneously with apache on a single server. Maybe you have a php script and want to test it with multiple php version. in such case this article is for you.

1- Install prerequisites

before installing and running two php versions, we need to install apache and some repository. so execute these commands:

Shell

1

2

3

4

5

yum install httpd

yum install epel-release

yum install yum-utils

2- Install multiple php versions

php-fpm is available in remi repository. so we install this repo and then after multiple php versions:

Shell

1

2

3

4

5

6

7

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

yum install php56

yum install php72

yum install php56-php-fpm

yum install php72-php-fpm

3- Configure SELinux

Install Selinux Policy Targeted

Shell

1

2

3

yum install selinux-policy-targeted

to allow selinux run php-fpms scripts, run these commands:

Shell

1

2

3

4

semanage port-a-thttp_port_t-ptcp9072

semanage port-a-thttp_port_t-ptcp9056

4- Configure php-fpm

by default, each php-fpm version is listening on port 9000. because we want to run multiple php versions, we need to change default port:

Shell

1

2

3

4

sed-i's/:9000/:9056/'/etc/opt/remi/php56/php-fpm.d/www.conf

sed-i's/:9000/:9072/'/etc/opt/remi/php72/php-fpm.d/www.conf

Start the php-fpm

Shell

1

2

3

4

systemctl start php72-php-fpm

systemctl start php56-php-fpm

now we need to make script wrapper to call php56-cgi and php72-cgi:

Shell

1

2

3

4

5

6

7

8

9

10

11

cat>/var/www/cgi-bin/php56.fcgi<<EOF

#!/bin/bash

exec/bin/php56-cgi

EOF

cat>/var/www/cgi-bin/php72.fcgi<<EOF

#!/bin/bash

exec/bin/php72-cgi

EOF

Then we set executable bit on both scripts:

Shell

1

2

3

4

sudo chmod755/var/www/cgi-bin/php56.fcgi

sudo chmod755/var/www/cgi-bin/php72.fcgi

5- Configure apache

Here we create two path. one for php-fpm56 and another for php-fpm72. you can change these paths with your own:

Php7.2 /var/www/html

Php5.6 /var/www/html56

Edit conf file /etc/httpd/conf.d/php.conf

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

ScriptAlias/cgi-bin/"/var/www/cgi-bin/"

AddHandler php56-fcgi.php

Action php56-fcgi/cgi-bin/php56.fcgi

Action php72-fcgi/cgi-bin/php72.fcgi

<Directory/var/www/html56>

DirectoryIndex index.php

AllowOverride all

Require all granted

AddHandler php56-fcgi.php

Action php56-fcgi/cgi-bin/php56.fcgi

</Directory>

<Directory/var/www/html>

DirectoryIndex index.php

AllowOverride all

Require all granted

AddHandler php72-fcgi.php

Action php56-fcgi/cgi-bin/php72.fcgi

</Directory>

#

# The following lines prevent .user.ini files from being viewed by Web clients.