I had the PHP, MySQL, and Apache stack installed for development. That installation is using configuration files from:
/etc/apache2/
/etc/php5/
Later I installed multiple PHP version using phpbrew
. All versions are accessible and switchable from CLI. But Apache always stays on the default version that was not installed using phpbrew.
Here is a list of my installed PHP versions.
$ phpbrew list
Installed versions:
php-5.4.13 (/home/admin1/.phpbrew/php/php-5.4.13)
+default -- --with-bz2=/usr
php-5.5.5 (/home/admin1/.phpbrew/php/php-5.5.5)
php-5.3.27 (/home/admin1/.phpbrew/php/php-5.3.27)
I have tried changing configuration file paths so they point to phpbrew's PHP. But nothing seems to be working.
How can I tell Apache to use phpbrew's PHP version?
I scripted this, because it was annoying me.
By default
phpbrew switch
will change the CLI version. To update Apache, you will have to tell it to use the newly generated.so
file. On Ubuntu this file will be created like/usr/lib/apache2/modules/libphp$VERSION.so
.For this
.so
file to be generated, you have to install PHP like:Anyway, here's the shell script I use to switch PHP versions. The switch will fail if the
.so
file cannot be found, and it will requestsudo
privileges to restart Apache./home/luker/bin/phpbrewswitch
Usage
Attempting to switch to a PHP version that wasn't built for Apache:
Successfully changing to a PHP version that has an existing
.so
file:The solution I found for managing multiple PHP versions with an Apache server is to run PHP-FPM and FastCGI instead of
mod_php
. This way I can have multiple versions of PHP running, and select which sites on my development machine I want to run which version of PHP.You will need to recompile your PHP versions with the
+fpm
phpbrew flag instead of+apxs2
, start them with thephpbrew fpm start
command, install the Apachelibapache2-mod-fastcgi
package, and probably disable apachemod_php
- but it's pretty slick once it's working. I can test the same site with multiple versions of PHP just by configuring a different Virtual host pointing to the same code, but different PHP-FPM sockets.Here's an example of an Apache 2.4
/etc/apache2/mods-enables/fastcgi.conf
configuration file with 2 versions of PHP installed via phpbrew:Then in your apache "site" Virtualhost configuration you can specify which PHP version to run with an Alias like so:
Do look into Server Fault post How do I tell Apache which PHP to use?.
You need to specify the PHP version in Apache.
You need to build a PHP with
apxs2
:1) Ensure your have installed
sudo apt-get install apache2-dev
.2) Run
phpbrew install 5.4.22 +apxs2=/usr/bin/apxs2
Then you should see the built module file in your Apache configuration file.
If phpbrew successfully installs php version with +apxs2 ext, you should have a new ".so" file it inside apache2's module library (usr/lib/apache2/modules not /etc/apache2/mods-available).
There should be a php5.load or php7.load created inside /etc/apache2/mods-available folder that points to the mentioned .so file.
The .so file listed in those .load files is the version that get's loaded.
To switch between a php5 and php7 version use a2enmod/a2dismod php5 php7.
You need to reload apache2 after changing the config.
If php files are rendering as plain text, you need to either add this to the php*.load file:
AddType application/x-httpd-php .php
OR to avoid having to edit the php load files everytime you install a new version, you can set this globally in your apache2 config file:
SetHandler application/x-httpd-php
These instructions are intended for a development server.
My personal script for this:
The script runs phpbrew list on your behalf and let's you select the version using number keys. It then runs phpbrew switch and also switches the apache2 modules on or off and restarts the server.