52

I'm used to using python's virtualenv tool to create separate environments that can mimic deployment environments for projects I write.

Now, I'm going to be working on a php project and I'm wondering if there's any equivalent to that? Specifically I'm hoping to be able to run one virtualhost on apache with one (older) version of php, while everything else runs on the normal up to date version.

My development machine is running ubuntu 11.04, so solutions that work on that platform would be preferred.

Zxaos
  • 7,791
  • 12
  • 47
  • 61
  • Why you would want to have something like it? You see, PHP is made to be backward-compatible. Even between different 5.x versions, there are rarely differences which would be annoying, and even if those happen, those are usually easy to fix. For example between PHP 5.3 and 5.2 not many changes hapenned (http://pl2.php.net/manual/en/migration53.incompatible.php) - just two new keywords and some invalid code no longer working... – Konrad Borowski Sep 06 '11 at 19:45
  • 6
    @GlitchMr I'm trying to exactly replicate a deployment environment on a development server so that there are no surprises when I deploy, but I don't want to set up a whole new virtual machine for every deployment environment. If I could have different virtualhost in apache use different versions of PHP, I'd be a long way toward accomplishing this. – Zxaos Sep 07 '11 at 16:46
  • Docker is your solution. Vagrant for another solution. – JREAM Apr 06 '17 at 16:14

8 Answers8

13

Assuming that you are using mod_php, there is no way to load multiple different versions into the same Apache instance. You can run multiple different versions if you're running PHP as CGI or FastCGI, but this will itself introduce some differences in behavior from mod_php.

  • 2
    Separate virt.environments gives ability to select different versions of interpreter within IDE to make linting, code validation etc. So it is not only about running applications with different interpreter. – funberry Mar 16 '21 at 12:37
9

Another alternative to virtual machines is docker.

mozey
  • 2,222
  • 2
  • 27
  • 34
  • This is actually what I was looking for. Do you have more specifics? Just spit balling here, but is there a specific Docker container, which includes everything needed to run PHP tests? And extra points for a container setup to do Laravel unit and feature tests. – MikeyE Jun 14 '23 at 22:30
7

virtPHP is a tool for creating and managing multiple isolated PHP environments on a single machine. It's like Python's virtualenv, but for PHP. (README)

https://github.com/virtphp/virtphp

ecurbelo
  • 333
  • 3
  • 11
  • 2
    As of July 2015, though, virtPHP targets only the command line (php-cli) instance of php. Unfortunately this wouldn't help with the problem as stated. – Zxaos Jul 21 '15 at 18:56
7

As loading different versions of php within apache with mod_php seems not to be posible, the easiest way of mimicking deployment and development setups will be with a virtualmachine, which you stated you would like to avoid.

One way of making the burden of vm's for developers a bit easier is to use something like vagrant. With two files (the vagrant file, and the chef/puppet file) you can "version" your vm's, easily create them and destroy them for each project and when needed.

ashwoods
  • 2,209
  • 1
  • 22
  • 36
5

You might be interested in this: https://github.com/phpenv/phpenv

smg628
  • 179
  • 1
  • 5
2

As of now, there is no out of the box solution to this problem. The only solution which comes close is Vagrant and puPHPet.com as discussed here: https://drupal.org/node/2055947 Why can't we have an environment like python's virtualenv or ruby's rbenv? It makes up a nice open source project. Multiple instances of PHP can be handy if we want to test out some libraries in sandboxes rather than globally. We can install dependencies for different projects using a package manager like Composer.

2

(Haven't coded php in years, so this might be outdated)

As far as I remember you just had to point to another directory where your libraries reside (include PATH), using something like:

include_path = .:/usr/local/lib/php:./include (this goes in php.ini, default libararies)

and in your php files:

ini_set("include_path", ".:../:./include:../include");

PHP never really had a robust packaging system and library repository like perl/python/ruby has, PEAR was trying to move in that direction but it is very closed in comparison and was hard to configure in multiproject environments.

Phark is trying to build a brew/bundler port for php, https://github.com/lox/phark, although it's not deployment ready.

ashwoods
  • 2,209
  • 1
  • 22
  • 36
  • This might work for library version, but it doesn't seem like it would work to change the core PHP version used. – Zxaos Sep 28 '11 at 17:43
  • true. was just stating the virtualenv equivalent. (a pythonbrew alternative is more what you are looking for). – ashwoods Sep 30 '11 at 14:01
-1

Cloudlinux with PHP Selector has this for ages. It is integrated with popular control panels like CPanel, DirectAdmin etc.

Each linux account can have its own version of php and select any extensions they sit fit.

https://www.cloudlinux.com/php-selector