Setup multiple versions of php in your machine

Nov 2, 2022 | 3 min read

There are various ways to set up multiple php versions in the same server in case of production as well as in the development environment. We will be discussing majorly three ways to host multiple php projects in a same server. We will also be discussing more on the pros and cons of each method, so you can choose and decide on your own as per your case.

This blog is a first blog on series : Various ways to setup multiple php versions projects in same server or host.

Using Docker Containers:

Docker allows you to create isolated containers for each project, each running its own version of PHP. You can specify the PHP version in the Dockerfile for each container, ensuring compatibility with the project's requirements. This approach provides a clean and portable solution, with minimal interference between projects. You can view blog: Setup Multiple PHP versions using Docker Container

Benefits :

  • Docker containers provide isolated environments for each PHP version so no concerns for interfering with one another
  • Docker helps you easily move applications between development, testing, and production environments without compatibility issues.
  • Docker containers can be scaled horizontally to handle varying workloads and traffic demands, making them highly suitable for production environments with high traffic volumes

Concerns:

  • Running multiple containers may introduce additional resource overhead, including CPU, memory, and storage
  • Managing Docker containers in can be complex in case of inexperience with the team
  • Docker containers share the host operating system kernel, sometimes raising potential security concerns if not properly configured or isolated.

Using Virtual Machines:

You can use virtualization software like VirtualBox or VMware to create separate virtual machines for each project, each with its own PHP version installed. This approach offers isolation similar to Docker containers but may require more system resources.

Benefits:

  • Isolation of environments similar to Docker container
  • Flexibility to developers as virtual machines can be setup in any OS or environment
  • Virtual machines can allocate dedicated resources to each applications so stable and predictable performance

Concerns

  • Running multiple virtual machines consumes significant resources, including memory and disk space sometimes difficult for production as well as dev environments
  • Managing and maintaining multiple virtual machines is complex and time-consuming
  • Since each instance of virtualbox consumes separate resource and kernels so these can reduce the performance and also can be overhead

Although , Virtual box and Docker container are similar in terms of benefits achieved like isolation , portablity , scalablility and consistency in environment but Docker container uses less resource , have a lower start up time and are more easier to setup and portable hence Docker container gains more benefits when comparison between virtual box and Docker container only.

Using PHP Version Managers:

PHP version managers like phpenv, phpbrew, allow you to install and switch between different PHP versions on your development machine. This approach is suitable for local development and testing but may not provide isolation between projects.

Benefits

  • Version managers help developers to easily switch between PHP versions , providing flexibility in development and testing workflows.
  • They simplify the settings of php versions, installation and management and also automate the process
  • PHP versions installed with Version managers are compatible and hence no conflict arises

Concerns

  • Version manager sometime can create conflict with other dependencies and softwares during installation
  • It may not provide similar level of isolation as docker container or virtual box
  • Managing dependencies and extensions across multiple PHP versions can be challenging, requiring careful management and configuration

Using Server configuration:

If you're managing your own server, you can configure it to run multiple versions of PHP simultaneously. Tools like Apache's mod_php or Nginx with PHP-FPM allow you to specify different PHP versions for different virtual hosts or directories. This approach requires more manual configuration but provides flexibility and control. You can learn more on: Setup Multiple PHP versions using Apache Server Configurations

Benefits

  • Its simplicity of configuration and minimal steps makes it best suited for development as well as small scale production
  • Development teams are more comfortable with configuration of web servers like apache and nginx reducing the learning curve and enabling quick setup.
  • No third party or libraries require to install and can go in a few steps.

Concerns

  • Isolation is not similar to the isolation as docker container and virtualbox which can sometimes lead to conflict and compatibility issues
  • It requires manual setup and configurations, hence when requiring to setup multiple php versions can increase risk of configuration errors and inconsistencies
  • Server configuration may not scale well for large-scale production environments

Which method is better?

Each of these options are equally applicable to setup multiple php in both environments(production or development). But choosing one out of the other completely depends on your use case and scenario ,it's your call , each of these have their own benefits and concerns, as discussed above. Evaluating the cost and benefits associated with each method , you can choose one over another. I generally prefer to use either Docker or Server configuration methods over others.

Also See,

Other blogs from this Series:

  1. Setup Multiple PHP versions using Docker Container
  2. Setup Multiple PHP versions using Apache Server Configurations
Author Profile Picture

Sagar Chapagain

I am a Software Engineer, a Solution Architect,a Mentor, a Trainor, a Technologist, Speaker, from land of Himalays, Enthusiasts in Tech, Investment and Economy, with a total years of experience in field of software and application development, Deployment .