Thursday, March 23, 2017

Compiling multiple PHP FPM installations on Ubuntu Linux



In order to use some of the PHP features, we need to have some dependencies installed. Libjpeg, libpng f.e. for GD, but also libmysqlclient for mysql, libcurl and so on. This is a small set ob standard dependencies, that I use for every php installation, so I install this upfront and don't wait for error messages when configuring the php sources.

sudo apt-get install libxml2-dev libzip-dev libbz2-dev libcurl4-openssl-dev  libjpeg-dev libpng12-dev libtidy-dev libevent-dev libjpeg62-dev libmysqlclient-dev


First of all, you need to download the php sources. Go to and download the sources. I need always at least 3: The newest 5.4 release (5.4.7 as of this writing), 5.3.3 (as this is the minimum php version for Zend Framework 2) and the latest 5.3. release (5.3.17 as of this writing).
I will install PHP under "/opt/php/5.4.7", "/opt/php/5.3.3" and "/opt/php/5.3.17".

cd Downloads
tar xf php-5.4.7.tar.gz
cd php-5.4.7
./configure --prefix=/opt/php/5.4.7 --with-pear --with-zlib --enable-shmop --enable-xml --with-curl --enable-libxml --enable-session --with-pcre-regex --enable-exif --enable-inline-optimization --enable-soap --enable-sockets --with-xmlrpc --enable-mbstring --with-bz2 --with-tidy --enable-dom --with-pdo-sqlite --with-pdo-mysql=shared --with-gd=shared --with-zlib-dir --with-jpeg-dir --enable-fpm
make test
sudo make install

Repeat the above steps for every need php installation.
The only really important switch is "--prefix" and "--enable-fpm". The rest are standard options that I want to reuse accross installations.

Known issues

On Ubuntu 12.04.1 I receive the following error while configuring PHP 5.3.3.:
"error: libjpeg.(a|so) not found."

A quick workaround is:
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.a /usr/lib/libjpeg.a
sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/ 
sudo ln -s /usr/lib/x86_64-linux-gnu/libpng.a /usr/lib/libpng.a 
sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/ 

Symlink the binaries

I create a symlink for every installed php installation. Additional, I'll create a "default" symlink to use one as the default php installation. This will be PHP 5.4.7 in this case.
sudo ln -s /opt/php/5.4.7/bin/php /usr/local/bin/php-5.4.7
sudo ln -s /opt/php/5.3.3/bin/php /usr/local/bin/php-5.3.3
sudo ln -s /opt/php/5.3.17/bin/php /usr/local/bin/php-5.3.17

Installing Nginx


You can download and install nginx using apt:
sudo apt-get install nginx

Configuring Nginx

For reference here is my nginx.conf (/etc/nginx/nginx.conf)
user www-data;
worker_processes 4;
pid /var/run/;

events {
        worker_connections 768;
        # multi_accept on;

http {

        # Basic Settings

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        # Logging Settings

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        # Gzip Settings

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        zip_comp_level 2;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        # nginx-naxsi config
        # Uncomment it if you installed nginx-naxsi

        #include /etc/nginx/naxsi_core.rules;

        # nginx-passenger config
        # Uncomment it if you installed nginx-passenger

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        # Virtual Host Configs

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
Additional you need to change some settings in your fastcgi_params configuration (/etc/nginx/fastcgi_params)
fastcgi_param   QUERY_STRING            $query_string;
fastcgi_param   REQUEST_METHOD          $request_method;
fastcgi_param   CONTENT_TYPE            $content_type;
fastcgi_param   CONTENT_LENGTH          $content_length;
fastcgi_param   PATH_INFO               $fastcgi_script_name;

fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   REQUEST_URI             $request_uri;
fastcgi_param   DOCUMENT_URI            $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
fastcgi_param   SERVER_PROTOCOL         $server_protocol;

fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;

fastcgi_param   REMOTE_ADDR             $remote_addr;
fastcgi_param   REMOTE_PORT             $remote_port;
fastcgi_param   SERVER_ADDR             $server_addr;
fastcgi_param   SERVER_PORT             $server_port;
fastcgi_param   SERVER_NAME             $server_name;

fastcgi_param   HTTPS                   $https;

# This should be tuned according to your environment
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

Virtual Hosts

One config to rule them all