Multiple versions of php on apache (Ubuntu 18)
This is a follow up blog post to the one at https://srisoham.wordpress.com/2015/10/10/multiple-versions-of-php-on-apache/ .
On Ubuntu 18, libapache2-mod-fastcgi is not available. So, following are the steps to run multiple versions of php on Ubuntu 18 (and possibly future versions).
You can install multiple versions of php using phpbrew. Instructions for installation and usage of phpbrew can be found on the phpbrew wiki page. This is for my development machine. I went with regular user (non-root) installation.
I went with installing php 5.6.40 and 7.2.32 with fpm module. This is for apache 2.4.29 on ubuntu 18.04.
You will have to enable proxy_fcgi module of apache.
sudo a2enmod proxy_fcgi
This is for installing php 5.6.40.
phpbrew -d install 5.6.30 +default +dbs +mb +fpm
+default, +dbs etc. are the virtual groups for various libraries to be installed. You can get more information on these groups by running “phpbrew variants” in your terminal.
You can find the php config file (php.ini) and fpm config file (php-fpm.conf) for this version at ~/.phpbrew/php/php-5.6.40/etc/ . You have to edit the php-fpm.conf and make some changes. If you want to run php-fpm through tcp ports, then change the
listen = 127.0.0.1:9000
to
listen = 127.0.0.1:9056
where 56 in 9056 indicates the major.minor version of the php.
Now, for installing php 7.2.32
phpbrew -d install 7.2.32 +default +dbs +mb +fpm
The php and fpm config files can be found at ~/.phpbrew/php/php-7.2.32/etc/ directory. To run php-fpm through tcp, edit ~/.phpbrew/php/php-7.2.32/etc/php-fpm.conf and change
listen = 127.0.0.1:9000
to
listen = 127.0.0.1:9072
Now, modify the virtual host config files of your projects to specify the version of php to be run. Here is a sample config file.
<VirtualHost *:80>
ServerName project1.local
DocumentRoot "/home/soham/projects/project1"
<Directory /home/soham/projects/project1>
Options Indexes FollowSymLinks MultiViews
AllowOverride none
Require all granted
</Directory>
<FilesMatch "\.php$">
SetHandler "proxy:fcgi://127.0.0.1:9056"
</FilesMatch>
ErrorLog /var/log/apache2/project1_error.log
LogLevel Warn
CustomLog /var/log/apache2/project1_access.log combined
</VirtualHost>
The line of importance is
SetHandler "proxy:fcgi://127.0.0.1:9056"
You should add this to your virtual host config file to run the project under php 5.6.40. If you want to run the project under php 7.2.32, then the line should be
SetHandler "proxy:fcgi://127.0.0.1:9072"
Now, the last set of commands to get everything up and running
phpbrew use php-5.6.40 phpbrew fpm start # this will start php-fpm for php 5.6.40 phpbrew use php-7.2.32 phpbrew fpm start # this will start php-fpm for php 7.2.32 sudo service apache2 restart
Create a file test.php with contents
<?php phpinfo();
and add it to the document root each of the projects. Access the test.php from browser for each project. You should be able to see the correct version of php as configured for each site.