Ubuntu 14.04 LTS has been out for a while now, and it offers some nice enhancements over 12.04 in its default repos: Apache 2.4, PHP 5.5, and MySQL 5.5.3x. Starting from a bare server image, here’s how to get an Ubuntu 14 web server up and running for PHP.
Install the Web Server
$ apt-get install apache2 mysql-server php5-mysql php5-fpm php5-gd php5-cli
Note: If you install Apache and PHP-FPM simultaneously, Apache will be configured automatically to use Event as its multi-processing module, although Prefork and Worker are still there if you need them. If you install Apache alone, it uses Prefork, and then even if you install a separate PHP interpreter you have to configure the Worker or Event MPM manually.
Configure the Web Server
$ /etc/apache2/mods-available# apache2ctl -M Loaded Modules: core_module (static) so_module (static) watchdog_module (static) http_module (static) log_config_module (static) logio_module (static) version_module (static) unixd_module (static) access_compat_module (shared) alias_module (shared) auth_basic_module (shared) authn_core_module (shared) authn_file_module (shared) authz_core_module (shared) authz_host_module (shared) authz_user_module (shared) autoindex_module (shared) deflate_module (shared) dir_module (shared) env_module (shared) filter_module (shared) mime_module (shared) mpm_event_module (shared) <----- negotiation_module (shared) setenvif_module (shared) status_module (shared)
Since we’re not using mod_php, we need to get Apache talking to PHP-FPM. Edit /etc/php5/fpm/pool.d/www.conf and ensure PHP-FPM is using a Unix socket:
listen = /var/run/php5-fpm.sock
Then enable the included mod_proxy_fcgi:
$ a2enmod proxy_fcgi # this also enables mod_proxy
Next, edit your virtual host to use the ProxyPassMatch directive. I just used the 000-default.conf file:
(etc.) ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ProxyPassMatch ^/(.*.php(/.*)?)$ unix:/run/php-fpm.sock|fcgi://localhost/var/www/html/
Warnings and Errors
Seems simple, right? Unfortunately, when you restart Apache, it fails with this error:
ProxyPass URL must be absolute! Action 'configtest' failed.
The problem here is that mod_proxy_fcgi in Apache 2.4.7 from the Ubuntu repos doesn’t support Unix sockets. Not very smooth. So for now you should set PHP-FPM to run on a TCP socket accessible from localhost. Set /etc/php5/fpm/pool.d/www.conf to
listen = 127.0.0.1:9000 and then change the ProxyPassMatch directive in your virtual host:
ProxyPassMatch ^/(.*.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1
Restart PHP-FPM and Apache, and then you should be able to run any PHP code in the location specified in 000-default (/var/www/html).
You might be tempted to autoremove Apache 2.4.7 and install a later version either from source or a PPA. Apache 2.4.8 and later include the UDS patch provided for Unix sockets support. However, due to an unrelated bug in proxy_fcgi, you’ll still need to run PHP-FPM over a TCP socket for now.
The proxy_fcgi socket error from Apache’s error.log:
[proxy:error] [pid 7559:tid 140628244846336] (2)No such file or directory: AH02454: FCGI: attempt to connect to Unix domain socket /var/run/php-fpm.sock (localhost) failed
Running Apache 2.4.9 won’t hurt you though (the standard warnings for non-official repos apply here, of course):
$ add-apt-repository ppa:ondrej/apache2 $ apt-get update $ apt-get install apache2 $ apache2 -v Server version: Apache/2.4.9 (Ubuntu) Server built: Apr 1 2014 08:39:50
(PPA:ondrej also includes the latest version of PHP, 5.5.12 as of this writing.)
PHP 5.5, included in the Ubuntu 14.04 repos, includes the Zend opcode cache which has proven faster than APC. phpinfo() will show “Opcode Caching” as “Up and Running” and you can control your environment easily by editing the provided /etc/php5/fpm/conf.d/05-opcache.ini file. Combined with the added efficiency of mpm_event, this is still a worthy upgrade even if you need to adjust your config a bit.