Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raihan-uddin/f05256bed155895300a831e5068b981b to your computer and use it in GitHub Desktop.
Save raihan-uddin/f05256bed155895300a831e5068b981b to your computer and use it in GitHub Desktop.
Install Multiple PHP Versions run parallel USING .htaccess / separate VIRTUAL HOST for each, Ubuntu

Multiple PHP version install in Ubuntu. Apache2, Apache. Changing php version using .htaccess & keeping or settingup using separe virtual host for each php version.

Commands and points are below

sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update -y
sudo apt-get install php5.6 php5.6-fpm php5.6-mysql libapache2-mod-php5.6 -y
sudo systemctl start php5.6-fpm 
sudo systemctl status php5.6-fpm
sudo apt-get install php7.0 php7.0-fpm php7.0-mysql libapache2-mod-php7.0 -y
sudo systemctl start php7.0-fpm
sudo apt-get install php7.1 php7.1-fpm php7.1-mysql libapache2-mod-php7.1 -y
sudo systemctl start php7.1-fpm
sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 -y
sudo systemctl start php7.2-fpm
sudo apt-get install php7.3 php7.3-fpm php7.3-mysql libapache2-mod-php7.3 -y
sudo systemctl start php7.3-fpm
sudo apt-get install php7.4 php7.4-fpm php7.4-mysql libapache2-mod-php7.4 -y
sudo systemctl start php7.4-fpm
sudo apt-get install php8.1 php8.1-fpm php8.1-mysql libapache2-mod-php8.1 -y
sudo systemctl start php8.1-fpm
sudo apt-get install php8.2 php8.2-fpm php8.2-mysql libapache2-mod-php8.2 -y
sudo systemctl start php8.2-fpm

Enable Mode

sudo a2enmod actions fcgid alias proxy_fcgi

To ENABLE .htaccess through change version

sudo gedit /etc/apache2/apache2.conf

Find :: Directory /var/www Change "AllowOverride None" To "AllowOverride All" and save it

[Directory /var/www/]
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
[/Directory]

##Restart APACHE web server sudo systemctl restart apache2

MODE 1: Using .htaccess

NOTE : ************ Change/Replace "Square Brackets [ & ] " tags to "Less than" and "Greater than" characters in your site file. change 5.6 to your necessary version like php7.4, php8.1 etc

[FilesMatch \.php$]
    Apache 2.4.10+ can proxy to unix socket
    SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost"
[/FilesMatch]

==================================================

MODE 2: In Terminal Mode : check installed php version & switch to other version

sudo update-alternatives --config php

Mode 3: for VIRTUAL HOST setup

sudo gedit /etc/hosts

127.0.5.6 php56.local 127.0.7.2 php72.local 127.0.7.4 php74.local 127.0.8.1 php81.local

FOR Virtual host Need to create sites in below location

sudo gedit /etc/apache2/sites-available/php74.local.conf sudo gedit /etc/apache2/sites-available/php81.local.conf sudo gedit /etc/apache2/sites-available/php74.local.conf

Code for each virtual host sites: NOTE : ************ Change/Replace "Square Brackets [ & ] " tags to "Less than" and "Greater than" characters in your site file. change 56 to your site version like 81,74,72 like that.

Change this "php5.6-fpm" To "php8.1-fpm" or php7.4-fpm

[VirtualHost *:80]
     ServerAdmin admin@php56.local
     ServerName php56.local
     DocumentRoot /var/www/php56.local
     DirectoryIndex info.php

     [Directory /var/www/php56.local]
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     [/Directory]
  [IfModule dir_module]
     DirectoryIndex index.php index.html 
     [/IfModule]

    [FilesMatch \.php$]
      For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server
      SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
    [/FilesMatch]

     ErrorLog ${APACHE_LOG_DIR}/php56.local_error.log
     CustomLog ${APACHE_LOG_DIR}/php56.local_access.log combined
[/VirtualHost]

Restart APACHE web server

sudo systemctl restart apache2

Additional Extenstions (curl, xml, gd library, mbstring, initl (for normalisation class issue fix in Laravel))

sudo apt-get install php5.6-curl php5.6-xml php5.6-gd php5.6-intl php5.6-mbstring -y
sudo apt-get install php7.2-curl php7.2-xml php7.2-gd php7.2-intl php7.2-mbstring -y
sudo apt-get install php7.4-curl php7.4-xml 7.4-gd php7.4-intl php7.4-mbstring -y
sudo apt-get install php8.1-curl php8.1-xml 8.1-gd php8.1-intl php8.1-mbstring -y
sudo apt-get install php8.2-curl php8.2-xml 8.2-gd php8.2-intl php8.2-mbstring  -y
sudo apt-get install php5.6-zip php5.6-gd php5.6-json php5.6-curl -y
sudo apt-get install php7.2-zip php7.2-gd php7.2-json php7.2-curl -y
sudo apt-get install php7.4-zip php7.4-gd php7.4-json php7.4-curl -y
sudo apt-get install php8.1-zip php8.1-gd php8.1-curl -y
sudo apt-get install php8.2-zip php8.2-gd php8.2-curl -y
sudo apt-get install php8.3-zip php8.3-gd php8.3-curl -y


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment