Cara Jalankan Banyak Versi PHP (5.6, 7.x) dengan Apache di Ubuntu

 
Sering terjadi kasus developer ingin deploy beberapa aplikasi web PHP ke satu server yang sama dengan berbagai versi PHP yang berbeda. Sementara secara default di server yang tersedia hanya satu versi PHP saja. Nah, di artikel ini dibahas mengenai cara install dan setting untuk menjalankan banyak versi PHP (5,6 7.x) dengan Apache web server di Ubuntu .
1. Install Apache

Install Apache dan modul FastCGI.

    apt update  
    apt install apache2 libapache2-mod-fcgid -y
 
2. Install PHP Banyak Versi

Install PHP multi versi menggunakan repository dari PPA (Personal Package Archives), karena versi PHP yang tersedia di repository default Ubuntu  yaitu PHP v7.2.
Cara Jalankan Banyak Versi PHP dengan Apache di Ubuntu
Cek versi Ubuntu dan paket php dari repository default
 
    add-apt-repository ppa:ondrej/php
    apt update
    apt install php5.6 php5.6-fpm -y
    apt install php7.0 php7.0-fpm -y
    apt install php7.1 php7.1-fpm -y
    apt install php7.2 php7.2-fpm -y
    apt install php7.3 php7.3-fpm -y    


Mencari nama paket modul atau extension PHP gunakan perintah apt search atau apt-cache search.
Misalnya mencari modul untuk php7.2.

 
apt search php7.2 | more
apt-cache search php7.2 | more
 

Menguji hasil install PHP dengan menampilkan versi PHP.
 
    php5.6 -v
    php7.0 -v
    php7.1 -v
    php7.2 -v
    php7.3 -v  

Setting versi default PHP untuk php-cli, misalnya ingin memakai versi PHP 7.2 sebagai versi default.
 
    update-alternatives --set php /usr/bin/php7.2
    php -v

3. Setting Apache Banyak versi PHP

Aktif modul Apache yang dibutuhkan.

    a2enmod actions fcgid alias proxy_fcgi


Masing-masing versi PHP dibuatkan virtualhost.

    PHP v5.6 = php56.xtester.my.id
    PHP v7.0 = php70.xtester.my.id
    PHP v7.1 = php71.xtester.my.id
    PHP v7.2 = php72.xtester.my.id
    PHP v7.3 = php73.xtester.my.id

Membuat file virtualhost php56.xtester.my.id.conf untuk PHP v5.6.

    cd /etc/apache2/sites-available/
    nano php56.xtester.my.id.conf    

Isi dari file virtualhost php56.xtester.my.id.conf


    <VirtualHost *:80>
        ServerName php56.xtester.my.id
        DocumentRoot /var/www/php56
    
        <Directory /var/www/php56>
            Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted"
        </Directory>
    
        <FilesMatch \.php$>
            SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
        </FilesMatch>
    
        ErrorLog ${APACHE_LOG_DIR}/php56.xtester.my.id_error.log
        CustomLog ${APACHE_LOG_DIR}/php56.xtester.my.id_access.log combined
    </VirtualHost>

Pada file virtualhost PHP v5.6 di atas, opsi SetHandler yang merupakan versi PHP yang mengeksekusi script file .php.
Lanjutkan dengan membuat file virtualhost untuk versi PHP yang lain, sesuaikan opsi SetHandler, ServerName, DocumentRoot, ErrorLog dan CustomLog.
Setelah semua setting virtualhost selesai, lanjut membuat direktori dan file index.php yang berisi phpinfo untuk masing-masing versi PHP.
 
    cd /var/www
    mkdir php56 php70 php71 php72 php73
    echo '<?php phpinfo(); ?>' | sudo tee --append php56/index.php
    echo '<?php phpinfo(); ?>' | sudo tee --append php70/index.php
    echo '<?php phpinfo(); ?>' | sudo tee --append php71/index.php
    echo '<?php phpinfo(); ?>' | sudo tee --append php72/index.php
    echo '<?php phpinfo(); ?>' | sudo tee --append php73/index.php
 
Terakhir aktifkan virtualhost dan restart Apache.
 
    a2ensite php56.xtester.my.id.conf
    a2ensite php70.xtester.my.id.conf
    a2ensite php71.xtester.my.id.conf
    a2ensite php72.xtester.my.id.conf
    a2ensite php73.xtester.my.id.conf
    systemctl restart apache2
    systemctl status apache2

4. Pengujian
5. Beda Versi PHP di Direktori Tertentu

Virtualhost di atas berlaku untuk subdomain, sehingga file yang berada dalam direktori dan sub-direktori subdomain tersebut menjalankan versi PHP yang sama. Muncul pertanyaan bagaimana caranya jika masih dalam satu domain atau subdomain ingin menjalankan PHP dengan versi berbeda di direktori tertentu? Jawabannya dengan cara menambah opsi Directory di setting virtualhost.

Misalnya DocumentRoot /var/www/xtester.my.id dengan url akses http://xtester.my.id dan sub-direktori lain menjalankan PHP v5.6, sementara khusus untuk sub-direktori /var/www/xtester.my.id/v2 dengan url akses https://xtester.my.id/v2 menjalankan PHP v7.2.
 
 
    <VirtualHost *:80>
        ServerName www.xtester.my.id
        ServerAlias xtester.my.id
        DocumentRoot /var/www/xtester.my.id
    
        <Directory /var/www/xtester.my.id>
            Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted
        </Directory>
    
        <FilesMatch \.php$>
            SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
        </FilesMatch>
    
        <Directory /var/www/xtester.my.id/v2>
            Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted
            <FilesMatch \.php$>
              SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
            </FilesMatch>
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/xtester.my.id_error.log
        CustomLog ${APACHE_LOG_DIR}/xtester.my.id_access.log combined
    </VirtualHost>  

 

Related Articles