I want to both keep the latest PHP provided with Ubuntu and to have a specific PHP version installed somewhere. Is it doable? Can I just compile the PHP in some folder and then somewhere in Apache configs (I mean the apache provided with Ubuntu) switch between PHP dirs when needed.
Yes, you can. Fisrt compile & install new php, and then define your new php as a cgi module to apache. open apache config file and add this lines
<Directory /your/desired/directory/> AddHandler my-php-script .php Action my-php-script /path/to/your/php_executable </Directory>
or you may system wide define new extension
AddHandler my-php-script .myphp Action my-php-script /path/to/your/php_executable