Tuesday, April 26, 2016

Apache with PHP7 on Ubuntu

The newly released PHP 7 has fascinated me for a while. However, a quick googling reveals only a few tutorials that show how the new PHP version is hooked up with Apache. Moreover, most of them require installing untrusted PPAs. That's something I'd avoid, so I took a different approach. This is how I got it going.

I'm using the freshly released Ubuntu 16.04 since php7 is included in Xenial's main repository. Because the PHP 7 Apache module doesn't yet seem to be available, I'm going to run PHP using its CGI SAPI instead.

First install Apache and PHP:

sudo apt-get install apache2 php7.0 php7.0-cgi

Enable actions and cgi modules in Apache:

sudo a2enmod actions
sudo a2enmod cgi

Now, add the following lines in Apache config file (usually /etc/apache2/apache2.conf in Ubuntu):

ScriptAlias /local-bin /usr/bin
AddHandler application/x-httpd-php7 php
Action application/x-httpd-php7 /local-bin/php-cgi7.0
<Directory "/usr/bin">
    Require all granted
    AllowOverride All
</Directory>

ScriptAlias tells Apache that /usr/bin directory can be used to execute scripts. AddHandler maps files with a .php extension to a MIME type of application/x-httpd-php7. Action defines that when a file with this MIME type is encountered, it should pass it to /usr/bin/php-cgi7.0. Lastly, the directory block gives all requests the permission to access the /usr/bin/ folder.

Restart Apache with sudo service apache2 restart and your web server should be ready to process PHP!