Tell Apache to use a specific PHP version installed using phpbrew

30.9k views Asked by At

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?

5

There are 5 answers

2
c9s On BEST ANSWER

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.

1
aWebDeveloper On

Do look into Server Fault post How do I tell Apache which PHP to use?.

You need to specify the PHP version in Apache.

1
OB7DEV On

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, which is seen in /etc/apache2/mods-available/php*.load).

The .so file listed in those php*.load (either php5.load or php7.load) files is the version that gets loaded. To switch between a php5 and php7 version use a2enmod/a2dismod php5 php7.

You need to reload apache2 after changing the config : systemctl restart apache2. 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:

<FilesMatch \.php?>
    SetHandler application/x-httpd-php
</FilesMatch>

These instructions are intended for a development server. My personal script for this:

#!/bin/bash
i=1
c=1
options=()
while [ $c -gt 0 ]
do
    v=$(phpbrew list | sed -n "${i}p")
    if [ -z "$v" ]; then
        c=0
    elif [ -n "$v" ]; then
        options+=("$v")
    fi
    i=$[$i+1]
done

count=0
printf "\n"
echo 'Available versions:'
for i in "${options[@]}"
do
 echo "$i $[$count+1]"
 count=$[$count+1]
done

printf "\n"
echo 'Which version should be enabled?'
read selected

chosen="${options[$selected - 1]}"
chosen="$(echo -e "${chosen}" | tr -d '[:space:]')"
chosen="$(echo -e "${chosen}" | sed 's/\*//g')"
chosen="$(echo -e "${chosen}" | sed 's/php-//g')"
echo -e "php-${chosen} to be enabled."

source $HOME/.phpbrew/bashrc
phpbrew switch php-${chosen}

if [[ $chosen =~ ^5 ]]; then
    sudo a2dismod php7
    sudo a2enmod php5
    sudo service apache2 reload
elif [[ $chosen =~ ^7 ]]; then
    sudo a2dismod php5
    sudo a2enmod php7
    sudo service apache2 reload
else echo 'This script only works on php 5 and 7';
fi

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.

0
thaddeusmt On

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 the phpbrew fpm start command, install the Apache libapache2-mod-fastcgi package, and probably disable apache mod_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:

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  FastCgiIpcDir /var/lib/apache2/fastcgi

  AddType application/x-httpd-fastphp5 .php
  Action application/x-httpd-fastphp5 /php5-fcgi

  # This is for php 5.6.28
  FastCgiExternalServer /usr/lib/cgi-bin/php56-cgi -socket /home/{USERNAME}/.phpbrew/php/php-5.6.28/var/run/php-fpm.sock -pass-header Authorization

  # This is for php 7.0.13
  FastCgiExternalServer /usr/lib/cgi-bin/php70-cgi -socket /home/{USERNAME}/.phpbrew/php/php-7.0.13/var/run/php-fpm.sock -pass-header Authorization

  # this seems to be required by Apache 2.4.10
  <Directory /usr/lib/cgi-bin>
    Require all granted
  </Directory>
</IfModule>

Then in your apache "site" Virtualhost configuration you can specify which PHP version to run with an Alias like so:

<VirtualHost *:80>
  # ServerName, ServerAdmin, etc
  DocumentRoot /var/www/my-site-code
  # Then point the php5-fcgi handler to a specific version of PHP
  # Here is PHP7, as defined in the fastcgi.conf file
  Alias /php5-fcgi /usr/lib/cgi-bin/php70-cgi
</VirtualHost>
2
Luke Rodgers On

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:

phpbrew install php-5.6.16 +default +apxs2

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 request sudo privileges to restart Apache.

/home/luker/bin/phpbrewswitch

#!/usr/bin/env bash
VERSION=$1

SOFILE=/usr/lib/apache2/modules/libphp$VERSION.so
CONFFILE5=/etc/apache2/mods-available/php5.load
CONFFILE7=/etc/apache2/mods-available/php7.load

source ~/.phpbrew/bashrc

if [ -f $SOFILE ]; then
    phpbrew switch $VERSION
    phpbrew list

    if [[ $VERSION == 7* ]]; then
        FILECONTENTS="LoadModule php7_module $SOFILE"
        CONFFILE=$CONFFILE7
        sudo a2enmod php7
        sudo a2dismod php5
    else
        FILECONTENTS="LoadModule php5_module $SOFILE"
        CONFFILE=$CONFFILE5
        sudo a2enmod php5
        sudo a2dismod php7
    fi;

    echo $FILECONTENTS > $CONFFILE
    echo "AddType application/x-httpd-php .php" >> $CONFFILE
    echo "Updated $CONFFILE"
    sudo service apache2 restart

else
    echo $VERSION "is not configured for apache"
    phpbrew list
fi

Usage

Attempting to switch to a PHP version that wasn't built for Apache:

[21:02:55] luker [~]$ phpbrewswitch 5.4.45
5.4.45 is not configured for apache
  php-5.6.16
  php-5.6.10
* php-5.5.30
  php-5.4.45

Successfully changing to a PHP version that has an existing .so file:

[21:03:55] luker [~]$ phpbrewswitch 5.6.16
* php-5.6.16
  php-5.6.10
  php-5.5.30
  php-5.4.45
Updated /etc/apache2/mods-available/php5.load