{"id":1807,"date":"2026-09-04T11:51:10","date_gmt":"2026-09-04T06:21:10","guid":{"rendered":"https:\/\/cpanelfree.com\/blog\/how-to-install-multiple-php-versions-ubuntu\/"},"modified":"2026-09-04T11:53:15","modified_gmt":"2026-09-04T06:23:15","slug":"how-to-install-multiple-php-versions-ubuntu","status":"publish","type":"post","link":"https:\/\/cpanelfree.com\/blog\/how-to-install-multiple-php-versions-ubuntu\/","title":{"rendered":"How to Install and Switch Multiple PHP Versions on Ubuntu VPS (Apache &amp; Nginx)"},"content":{"rendered":"<h2>Why Multi-PHP Environments Are Crucial for Production VPS Servers<\/h2>\n<p>Modern web infrastructure frequently requires running legacy web applications alongside modern PHP frameworks. While modern applications like Laravel 11 or Symfony 7 demand PHP 8.2 or 8.3 with JIT compilation and strict typing, older legacy enterprise applications, custom client portals, and older WordPress plugin suites may still require PHP 7.4 or PHP 8.1. Running multiple simultaneous PHP versions on a single high-performance Linux VPS allows you to isolate application dependencies, prevent breaking updates, and optimize server resource utilization without requiring costly separate VPS instances.<\/p>\n<p>In this comprehensive enterprise tutorial, we will walk step-by-step through configuring Ondrej Sury&#8217;s official PHP PPA repository on Ubuntu, installing PHP 7.4, 8.1, 8.2, and 8.3 with production-grade FPM (FastCGI Process Manager) extensions, managing global CLI defaults via Debian alternatives, and configuring virtual host routing across both Nginx and Apache web servers.<\/p>\n<h2>Step 1: Adding Ondrej Sury&#8217;s Production PHP PPA Repository<\/h2>\n<p>Ubuntu&#8217;s default universe repository only packages a single fixed version of PHP corresponding to the Ubuntu release version (e.g., PHP 8.1 on Ubuntu 22.04 LTS or PHP 8.3 on Ubuntu 24.04 LTS). To access all active and legacy PHP branches, we add the trusted and signed Ondrej Sury PPA repository:<\/p>\n<pre><code># Update package metadata and install software properties common\nsudo apt update &amp;&amp; sudo apt install -y software-properties-common ca-certificates lsb-release apt-transport-https\n\n# Add the official Ondrej Sury PHP repository\nsudo add-apt-repository ppa:ondrej\/php -y\nsudo apt update<\/code><\/pre>\n<h2>Step 2: Installing Coexisting PHP Versions with Essential Extensions<\/h2>\n<p>For production deployments, we always install PHP-FPM alongside standard core modules including MySQL drivers, cURL, MBString, XML, GD image manipulation, Zip, BCMath, and OPcache. Execute the following bulk installation command to deploy PHP 7.4, 8.1, 8.2, and 8.3:<\/p>\n<pre><code># Install PHP 7.4, 8.1, 8.2, and 8.3 with high-performance FPM sockets\nsudo apt install -y   php7.4-fpm php7.4-cli php7.4-mysql php7.4-curl php7.4-mbstring php7.4-xml php7.4-zip php7.4-gd php7.4-bcmath php7.4-opcache   php8.1-fpm php8.1-cli php8.1-mysql php8.1-curl php8.1-mbstring php8.1-xml php8.1-zip php8.1-gd php8.1-bcmath php8.1-opcache   php8.2-fpm php8.2-cli php8.2-mysql php8.2-curl php8.2-mbstring php8.2-xml php8.2-zip php8.2-gd php8.2-bcmath php8.2-opcache   php8.3-fpm php8.3-cli php8.3-mysql php8.3-curl php8.3-mbstring php8.3-xml php8.3-zip php8.3-gd php8.3-bcmath php8.3-opcache\n\n# Verify all four PHP-FPM systemd services are active and running\nsudo systemctl status php7.4-fpm php8.1-fpm php8.2-fpm php8.3-fpm --no-pager<\/code><\/pre>\n<h2>Step 3: Managing Global CLI Default PHP Version<\/h2>\n<p>When running command-line utilities, Composer builds, or global cron jobs, Ubuntu uses symbolic links managed by the <code>update-alternatives<\/code> subsystem. To switch your default command-line PHP binary interactively or programmatically:<\/p>\n<pre><code># Interactive selection modal\nsudo update-alternatives --config php\n\n# Programmatic instant switch to PHP 8.3 for CLI\nsudo update-alternatives --set php \/usr\/bin\/php8.3\nsudo update-alternatives --set phar \/usr\/bin\/phar8.3\nsudo update-alternatives --set phar.phar \/usr\/bin\/phar.phar8.3\n\n# Confirm active CLI runtime\nphp -v<\/code><\/pre>\n<h2>Step 4: Nginx Multi-PHP Virtual Host Routing Configuration<\/h2>\n<p>In Nginx, multi-PHP isolation is achieved by directing <code>fastcgi_pass<\/code> directives to the specific Unix domain socket created by each PHP-FPM pool in <code>\/run\/php\/<\/code>. Here is how to configure separate virtual hosts for different PHP runtimes:<\/p>\n<pre><code># Virtual Host 1: Modern App running PHP 8.3 (\/etc\/nginx\/sites-available\/modern.example.com)\nserver {\n    listen 80;\n    server_name modern.example.com;\n    root \/var\/www\/modern.example.com\/public;\n    index index.php index.html;\n\n    location \/ {\n        try_files $uri $uri\/ \/index.php?$query_string;\n    }\n\n    location ~ \\.php$ {\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/run\/php\/php8.3-fpm.sock;\n    }\n}\n\n# Virtual Host 2: Legacy Portal running PHP 7.4 (\/etc\/nginx\/sites-available\/legacy.example.com)\nserver {\n    listen 80;\n    server_name legacy.example.com;\n    root \/var\/www\/legacy.example.com\/public_html;\n    index index.php index.html;\n\n    location \/ {\n        try_files $uri $uri\/ \/index.php?$query_string;\n    }\n\n    location ~ \\.php$ {\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/run\/php\/php7.4-fpm.sock;\n    }\n}<\/code><\/pre>\n<h2>Step 5: Apache Multi-PHP Virtual Host Routing Configuration<\/h2>\n<p>If you are utilizing the Apache web server, ensure <code>mod_proxy<\/code> and <code>mod_proxy_fcgi<\/code> are enabled, and route each VirtualHost using <code>SetHandler<\/code>:<\/p>\n<pre><code># Enable required Apache proxy modules\nsudo a2enmod proxy proxy_fcgi\nsudo systemctl restart apache2\n\n# Virtual Host config for PHP 8.2 (\/etc\/apache2\/sites-available\/app82.conf)\n&lt;VirtualHost *:80&gt;\n    ServerName app82.example.com\n    DocumentRoot \/var\/www\/app82\n    &lt;Directory \/var\/www\/app82&gt;\n        AllowOverride All\n        Require all granted\n    &lt;\/Directory&gt;\n    &lt;FilesMatch \\.php$&gt;\n        SetHandler \"proxy:unix:\/run\/php\/php8.2-fpm.sock|fcgi:\/\/localhost\"\n    &lt;\/FilesMatch&gt;\n&lt;\/VirtualHost&gt;<\/code><\/pre>\n<h2>Comprehensive Multi-PHP Feature &amp; Resource Matrix<\/h2>\n<table style=\"width: 100%;border-collapse: collapse;margin: 20px 0;border: 1px solid #334155\">\n<thead>\n<tr style=\"background-color: #0f172a;color: #38bdf8\">\n<th style=\"padding: 12px;border: 1px solid #334155\">PHP Version<\/th>\n<th style=\"padding: 12px;border: 1px solid #334155\">FPM Socket Path<\/th>\n<th style=\"padding: 12px;border: 1px solid #334155\">JIT Engine<\/th>\n<th style=\"padding: 12px;border: 1px solid #334155\">Typical Use Case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background-color: #1e293b;color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><strong>PHP 7.4<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>\/run\/php\/php7.4-fpm.sock<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">No<\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Legacy ERPs, unmaintained themes, vintage scripts<\/td>\n<\/tr>\n<tr style=\"background-color: #0f172a;color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><strong>PHP 8.1<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>\/run\/php\/php8.1-fpm.sock<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Yes (v1)<\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Stable enterprise apps, WooCommerce 7.x\/8.x<\/td>\n<\/tr>\n<tr style=\"background-color: #1e293b;color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><strong>PHP 8.2<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>\/run\/php\/php8.2-fpm.sock<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Yes (Optimized)<\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Read-only classes, modern high-traffic blogs<\/td>\n<\/tr>\n<tr style=\"background-color: #0f172a;color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #334155\"><strong>PHP 8.3<\/strong><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\"><code>\/run\/php\/php8.3-fpm.sock<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Yes (Latest JIT)<\/td>\n<td style=\"padding: 10px;border: 1px solid #334155\">Cutting-edge Laravel 11, typed class constants<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Production Best Practices for Multi-PHP Server Tuning<\/h2>\n<ul>\n<li><strong>Tune <code>pm.max_children<\/code> Per Pool:<\/strong> Calculate your RAM budget carefully. If you run 4 PHP-FPM pools, divide your available memory (e.g., 4GB RAM) so that idle pools do not trigger Linux OOM killer panic.<\/li>\n<li><strong>Enable OPcache JIT on PHP 8.2\/8.3:<\/strong> Set <code>opcache.jit=tracing<\/code> and <code>opcache.jit_buffer_size=128M<\/code> in <code>\/etc\/php\/8.3\/fpm\/php.ini<\/code> for peak computational throughput.<\/li>\n<li><strong>Isolate Log Files:<\/strong> Configure distinct error logs in <code>\/var\/log\/php7.4-fpm.log<\/code> and <code>\/var\/log\/php8.3-fpm.log<\/code> for rapid debugging.<\/li>\n<li><strong>Harden <code>php.ini<\/code> Directives:<\/strong> Enforce <code>expose_php = Off<\/code>, <code>display_errors = Off<\/code>, and <code>max_execution_time = 60<\/code> across all installed versions.<\/li>\n<\/ul>\n<div style=\"background-color: #0f172a;border-left: 4px solid #38bdf8;padding: 18px 24px;margin: 30px 0;border-radius: 8px\">\n<h3 style=\"color: #38bdf8;margin-top: 0\">Recommended Related Technical Guides<\/h3>\n<ul style=\"margin-bottom: 0;color: #cbd5e1\">\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-fix-502-bad-gateway-nginx-openlitespeed-php-fpm\/\" style=\"color: #38bdf8;text-decoration: underline\">How to Fix 502 Bad Gateway with Nginx and PHP-FPM<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-deploy-nodejs-express-app-linux-vps-nginx-pm2\/\" style=\"color: #38bdf8;text-decoration: underline\">Deploying Node.js and PM2 Applications on Ubuntu VPS<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-setup-free-ssl-certificate-lets-encrypt-certbot-apache-nginx\/\" style=\"color: #38bdf8;text-decoration: underline\">Securing Multi-Domain Virtual Hosts with Let&#8217;s Encrypt SSL<\/a><\/li>\n<\/ul>\n<\/div>\n<div style=\"background: linear-gradient(135deg, #0284c7 0%, #0369a1 100%);color: #ffffff;padding: 28px;border-radius: 12px;margin: 35px 0;text-align: center\">\n<h3 style=\"color: #ffffff;margin-top: 0;font-size: 22px\">Deploy High-Speed Cloud VPS with Instant PHP Switching<\/h3>\n<p style=\"color: #e0f2fe;font-size: 15px;max-width: 650px;margin: 0 auto 18px auto\">Experience unthrottled NVMe storage, dedicated RAM, and 10Gbps uplinks with 100% free hosting and scalable cloud VPS servers configured for peak PHP performance.<\/p>\n<p>  <a href=\"https:\/\/cpanelfree.com\/\" style=\"background-color: #ffffff;color: #0284c7;font-weight: 700;padding: 12px 28px;border-radius: 8px;text-decoration: none;display: inline-block\">Get Free Cloud Hosting Today &rarr;<\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Why Multi-PHP Environments Are Crucial for Production VPS Servers Modern web infrastructure frequently requires running legacy web applications alongside modern PHP frameworks. While modern applications like Laravel 11 or Symfony 7 demand PHP 8.2 or 8.3 with JIT compilation and strict typing, older legacy enterprise applications, custom client portals, and older WordPress plugin suites may [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1806,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-1807","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/comments?post=1807"}],"version-history":[{"count":1,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1807\/revisions"}],"predecessor-version":[{"id":1822,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1807\/revisions\/1822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media\/1806"}],"wp:attachment":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media?parent=1807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/categories?post=1807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/tags?post=1807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}