Home→Blog→How to Enable Multiple PHP Versions on VPS: Apache & Nginx Guide
πŸ“… August 15, 2021⏱️ 10 minutes min read

How to Enable Multiple PHP Versions on VPS: Apache & Nginx Guide

Complete guide to running multiple PHP versions simultaneously on a single VPS server. Learn how to configure Apache and Nginx with PHP-FPM to support different PHP versions for various websites and applications.

How to Enable Multiple PHP Versions on VPS: Apache & Nginx Guide

Running multiple PHP versions on a single VPS or server is no longer impossible - it's actually a common requirement in modern web development. Whether you're managing legacy applications that require older PHP versions or testing applications with newer PHP releases, having multiple PHP versions available simultaneously can save significant time and resources.

This comprehensive guide will show you how to configure multiple PHP versions on your VPS server using both Apache and Nginx web servers with PHP-FPM (FastCGI Process Manager).

Introduction to MultiPHP

PHP (HyperText Preprocessor) is one of the most popular server-side programming languages for web development. However, different applications often require different PHP versions due to:

  • Legacy compatibility requirements
  • Framework-specific version dependencies
  • Testing new features on latest PHP versions
  • Client-specific hosting requirements

MultiPHP allows you to run multiple PHP versions simultaneously on the same server, enabling you to:

βœ… Host multiple websites with different PHP requirements
βœ… Test applications across different PHP versions
βœ… Migrate applications gradually from older to newer PHP versions
βœ… Maximize server resource utilization instead of using multiple VPS instances

Prerequisites

Before starting the MultiPHP installation, ensure you have:

  • Root or sudo access to your VPS/server
  • Fresh server installation (Ubuntu 20.04/22.04, CentOS 7/8, Rocky Linux, AlmaLinux)
  • Basic knowledge of Linux command line
  • Web server (Apache or Nginx) - we'll cover installation if needed

Method 1: MultiPHP with Apache and PHP-FPM

Step 1: Install Dependencies and Repository

First, update your system and install the necessary repositories:

For CentOS/RHEL/Rocky/AlmaLinux:

bash
# Install EPEL repository
sudo dnf install -y epel-release

# Install Remi repository
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm

# Install utilities
sudo dnf install -y yum-utils

# Enable specific PHP version modules
sudo dnf module reset php

For Ubuntu/Debian:

bash
# Update system
sudo apt update && sudo apt upgrade -y

# Install software properties
sudo apt install -y software-properties-common

# Add Ondrej PHP repository
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

Step 2: Install Apache Web Server

If Apache is not already installed:

CentOS/RHEL/Rocky/AlmaLinux:

bash
sudo dnf install -y httpd httpd-tools
sudo systemctl enable httpd
sudo systemctl start httpd

Ubuntu/Debian:

bash
sudo apt install -y apache2 apache2-utils
sudo systemctl enable apache2
sudo systemctl start apache2

Step 3: Install Multiple PHP Versions

For CentOS/RHEL/Rocky/AlmaLinux:

bash
# Install PHP 7.4
sudo dnf install -y php74 php74-php-fpm php74-php-cli php74-php-common php74-php-mysql php74-php-mbstring php74-php-xml php74-php-gd php74-php-curl php74-php-zip

# Install PHP 8.1
sudo dnf install -y php81 php81-php-fpm php81-php-cli php81-php-common php81-php-mysql php81-php-mbstring php81-php-xml php81-php-gd php81-php-curl php81-php-zip

# Install PHP 8.2
sudo dnf install -y php82 php82-php-fpm php82-php-cli php82-php-common php82-php-mysql php82-php-mbstring php82-php-xml php82-php-gd php82-php-curl php82-php-zip

For Ubuntu/Debian:

bash
# Install PHP 7.4
sudo apt install -y php7.4 php7.4-fpm php7.4-cli php7.4-common php7.4-mysql php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl php7.4-zip

# Install PHP 8.1
sudo apt install -y php8.1 php8.1-fpm php8.1-cli php8.1-common php8.1-mysql php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl php8.1-zip

# Install PHP 8.2
sudo apt install -y php8.2 php8.2-fpm php8.2-cli php8.2-common php8.2-mysql php8.2-mbstring php8.2-xml php8.2-gd php8.2-curl php8.2-zip

Step 4: Configure PHP-FPM Services

Start and enable all PHP-FPM services:

CentOS/RHEL/Rocky/AlmaLinux:

bash
sudo systemctl enable php74-php-fpm
sudo systemctl enable php81-php-fpm
sudo systemctl enable php82-php-fpm

sudo systemctl start php74-php-fpm
sudo systemctl start php81-php-fpm
sudo systemctl start php82-php-fpm

Ubuntu/Debian:

bash
sudo systemctl enable php7.4-fpm
sudo systemctl enable php8.1-fpm
sudo systemctl enable php8.2-fpm

sudo systemctl start php7.4-fpm
sudo systemctl start php8.1-fpm
sudo systemctl start php8.2-fpm

Step 5: Configure Apache for MultiPHP

Enable necessary Apache modules:

bash
sudo a2enmod actions fcgid alias proxy_fcgi rewrite
sudo systemctl restart apache2  # Ubuntu/Debian
# or
sudo systemctl restart httpd    # CentOS/RHEL/Rocky/AlmaLinux

Step 6: Create Virtual Hosts for Different PHP Versions

Create separate virtual host configurations for each PHP version:

PHP 7.4 Virtual Host (/etc/apache2/sites-available/php74-site.conf or /etc/httpd/conf.d/php74-site.conf):

apache
<VirtualHost *:80>
    ServerName php74.example.com
    DocumentRoot /var/www/php74-site
    
    <Directory /var/www/php74-site>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
    </FilesMatch>
    
    ErrorLog /var/log/apache2/php74_error.log
    CustomLog /var/log/apache2/php74_access.log combined
</VirtualHost>

PHP 8.1 Virtual Host (/etc/apache2/sites-available/php81-site.conf):

apache
<VirtualHost *:80>
    ServerName php81.example.com
    DocumentRoot /var/www/php81-site
    
    <Directory /var/www/php81-site>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
    </FilesMatch>
    
    ErrorLog /var/log/apache2/php81_error.log
    CustomLog /var/log/apache2/php81_access.log combined
</VirtualHost>

PHP 8.2 Virtual Host (/etc/apache2/sites-available/php82-site.conf):

apache
<VirtualHost *:80>
    ServerName php82.example.com
    DocumentRoot /var/www/php82-site
    
    <Directory /var/www/php82-site>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php8.2-fpm.sock|fcgi://localhost/"
    </FilesMatch>
    
    ErrorLog /var/log/apache2/php82_error.log
    CustomLog /var/log/apache2/php82_access.log combined
</VirtualHost>

Enable the sites (Ubuntu/Debian):

bash
sudo a2ensite php74-site.conf
sudo a2ensite php81-site.conf
sudo a2ensite php82-site.conf
sudo systemctl reload apache2

Method 2: MultiPHP with Nginx and PHP-FPM

Step 1: Install Nginx

CentOS/RHEL/Rocky/AlmaLinux:

bash
sudo dnf install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx

Ubuntu/Debian:

bash
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx

Step 2: Install PHP Versions (Same as Apache Method)

Follow Step 3 from the Apache method to install multiple PHP versions.

Step 3: Configure Nginx Virtual Hosts

Create separate server blocks for each PHP version:

PHP 7.4 Server Block (/etc/nginx/sites-available/php74-site or /etc/nginx/conf.d/php74-site.conf):

nginx
server {
    listen 80;
    server_name php74.example.com;
    root /var/www/php74-site;
    index index.php index.html index.htm;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

PHP 8.1 Server Block (/etc/nginx/sites-available/php81-site):

nginx
server {
    listen 80;
    server_name php81.example.com;
    root /var/www/php81-site;
    index index.php index.html index.htm;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

PHP 8.2 Server Block (/etc/nginx/sites-available/php82-site):

nginx
server {
    listen 80;
    server_name php82.example.com;
    root /var/www/php82-site;
    index index.php index.html index.htm;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

Enable the sites (Ubuntu/Debian):

bash
sudo ln -s /etc/nginx/sites-available/php74-site /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/php81-site /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/php82-site /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Testing Your MultiPHP Setup

Step 1: Create Document Roots

Create directories for each PHP version:

bash
sudo mkdir -p /var/www/php74-site
sudo mkdir -p /var/www/php81-site  
sudo mkdir -p /var/www/php82-site

# Set proper ownership
sudo chown -R www-data:www-data /var/www/  # Ubuntu/Debian
# or
sudo chown -R apache:apache /var/www/      # CentOS/RHEL/Rocky/AlmaLinux

Step 2: Create Test Files

Create index.php files in each directory to test PHP versions:

bash
# PHP 7.4 test file
echo "<?php echo '<h1>PHP Version: ' . phpversion() . '</h1>'; phpinfo(); ?>" | sudo tee /var/www/php74-site/index.php

# PHP 8.1 test file  
echo "<?php echo '<h1>PHP Version: ' . phpversion() . '</h1>'; phpinfo(); ?>" | sudo tee /var/www/php81-site/index.php

# PHP 8.2 test file
echo "<?php echo '<h1>PHP Version: ' . phpversion() . '</h1>'; phpinfo(); ?>" | sudo tee /var/www/php82-site/index.php

Step 3: Configure Hosts File (Local Testing)

Add entries to your /etc/hosts file for local testing:

bash
echo "127.0.0.1 php74.example.com php81.example.com php82.example.com" | sudo tee -a /etc/hosts

Step 4: Test Your Setup

Open your web browser and visit:

  • http://php74.example.com - Should show PHP 7.4 information
  • http://php81.example.com - Should show PHP 8.1 information
  • http://php82.example.com - Should show PHP 8.2 information

You should see each site displaying the correct PHP version in the phpinfo() output.

Advanced Configuration

PHP-FPM Pool Management

For better resource management, you can create separate PHP-FPM pools for each website:

Create custom pool (/etc/php/8.1/fpm/pool.d/custom-site.conf):

ini
[custom-site]
user = www-data
group = www-data
listen = /var/run/php/php8.1-custom-site.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off

Command Line PHP Switching

To use different PHP versions from the command line:

Ubuntu/Debian:

bash
# Set default PHP version
sudo update-alternatives --config php

# Use specific version directly
/usr/bin/php7.4 script.php
/usr/bin/php8.1 script.php
/usr/bin/php8.2 script.php

CentOS/RHEL/Rocky/AlmaLinux:

bash
# Use specific version directly
/opt/remi/php74/root/usr/bin/php script.php
/opt/remi/php81/root/usr/bin/php script.php
/opt/remi/php82/root/usr/bin/php script.php

Troubleshooting Common Issues

Issue 1: PHP-FPM Socket Not Found

Solution:

bash
# Check PHP-FPM status
sudo systemctl status php8.1-fpm

# Verify socket exists
ls -la /var/run/php/

# Restart PHP-FPM if needed
sudo systemctl restart php8.1-fpm

Issue 2: Permission Denied Errors

Solution:

bash
# Fix ownership and permissions
sudo chown -R www-data:www-data /var/www/
sudo chmod -R 755 /var/www/

# Check PHP-FPM pool configuration
sudo nano /etc/php/8.1/fpm/pool.d/www.conf

Issue 3: 502 Bad Gateway (Nginx)

Solution:

bash
# Check Nginx error logs
sudo tail -f /var/log/nginx/error.log

# Verify PHP-FPM is running
sudo systemctl status php8.1-fpm

# Test Nginx configuration
sudo nginx -t

Performance Optimization

1. PHP-FPM Process Management

Configure pm settings based on your server resources:

ini
; For small VPS (1-2 GB RAM)
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 5

; For larger servers (4+ GB RAM)
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15

2. Enable OPcache

Configure OPcache for better performance:

ini
; /etc/php/8.1/fpm/conf.d/10-opcache.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=2
opcache.fast_shutdown=1

3. Monitor Resource Usage

Use tools to monitor your MultiPHP setup:

bash
# Monitor PHP-FPM pools
sudo watch -n 1 'ps aux | grep php-fpm'

# Check memory usage
htop

# Monitor Apache/Nginx processes
sudo systemctl status apache2 nginx

Security Considerations

1. Disable Dangerous Functions

Add to PHP configuration:

ini
disable_functions = exec,passthru,shell_exec,system,proc_open,popen

2. Hide PHP Version

Apache (.htaccess):

apache
Header always unset X-Powered-By
Header unset X-Powered-By

Nginx:

nginx
server_tokens off;
more_clear_headers 'X-Powered-By';

3. Set File Upload Limits

Configure appropriate limits:

ini
upload_max_filesize = 10M
post_max_size = 12M
max_execution_time = 30
max_input_time = 60

Maintenance and Updates

Updating PHP Versions

Ubuntu/Debian:

bash
sudo apt update && sudo apt upgrade

CentOS/RHEL/Rocky/AlmaLinux:

bash
sudo dnf update

Backup Configuration

Create backups of important configuration files:

bash
sudo tar -czf multiphp-backup-$(date +%Y%m%d).tar.gz \
  /etc/apache2/sites-available/ \
  /etc/nginx/sites-available/ \
  /etc/php/*/fpm/pool.d/

Conclusion

Setting up multiple PHP versions on your VPS is straightforward with PHP-FPM and proper web server configuration. This setup provides:

βœ… Flexibility to run different applications with their required PHP versions
βœ… Resource efficiency by utilizing a single server instead of multiple VPS instances
βœ… Easy management with separate pools and configurations
βœ… Scalability to add more PHP versions as needed

Whether you choose Apache or Nginx, both solutions offer excellent performance and reliability for hosting multiple PHP applications. The key is proper configuration of PHP-FPM pools and web server virtual hosts.

Key Benefits of This Setup:

  • Cost Effective: One server hosts multiple PHP versions
  • Easy Switching: Change PHP versions per website easily
  • Better Testing: Test applications across different PHP versions
  • Future-Proof: Easy to add new PHP versions as they're released

For production environments, consider implementing additional security measures, monitoring, and backup strategies to ensure optimal performance and security.

If you need professional VPS management or setup services, consider consulting with experienced system administrators who specialize in MultiPHP configurations and server optimization.

Tags:

Related Posts