Cara menjalankan Multi PHP Version di Ubuntu Server 20.04 | alvianaufan

BLOG IT SYSADMIN

Daftar akun digitalocean untuk mendapatkan free credit 100$.
DigitalOcean Referral Badge

Apa manfaat Multi PHP Version?

Penerapan Multi PHP Version sangat penting ketika anda memasang lebih dari satu website didalam satu server. Dengan Multi PHP Version, server anda dapat menjalankan lebih dari satu versi PHP yang terinstall seperti php7.0, php7.1, php7.2, php7.3, php7.4, php8.0 , semua nya bisa anda jalankan secara bersamaan dan semua website anda bisa di setting agar masing-masing nya dijalankan dengan versi PHP yang anda inginkan.



Dalam tutorial ini saya akan coba memberi contoh jika saya memiliki 3 subdomain berbeda dengan menjalankan versi php yang berbeda juga didalam satu server ubuntu 20.04.

– Subdomain 1: luffy.alvianaufan.my.id (PHP 7.3)
– Subdomain 2: zoro.alvianaufan.my.id (PHP 7.4)
– Subdomain 3: sanji.alvianaufan.my.id (PHP 8.0)

  1. Install service Apache di ubuntu anda:
    apt install apache2
  2. Setelah itu pastikan service apache nya sudah berjalan:
    systemctl status apache2
    ● apache2.service - The Apache HTTP Server
         Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
         Active: active (running) since Fri 2022-01-07 06:26:56 WIB; 22h ago
           Docs: https://httpd.apache.org/docs/2.4/
        Process: 1257404 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
       Main PID: 1211642 (apache2)
          Tasks: 56 (limit: 2344)
         Memory: 17.3M
         CGroup: /system.slice/apache2.service
                 ├─1211642 /usr/sbin/apache2 -k start
                 ├─1257412 /usr/sbin/apache2 -k start
                 ├─1257413 /usr/sbin/apache2 -k start
                 └─1257414 /usr/sbin/apache2 -k start
  3. Mulai install PHP Version 7.3, 7.4, dan 8.0 dengan PHP-FPM.
    apt-get install software-properties-common -y
    add-apt-repository ppa:ondrej/php
    apt-get update -y
    apt-get install php7.3 php7.3-fpm libapache2-mod-php7.3 php7.4 php7.4-fpm libapache2-mod-php7.4 php8.0 php8.0-fpm libapache2-mod-php8.0 libapache2-mod-fcgid -y
    systemctl start php7.3-fpm
    systemctl start php7.4-fpm
    systemctl start php8.0-fpm
    
  4. Pastikan untuk service PHP-FPM nya sudah berjalan:
    systemctl status php7.3-fpm
    systemctl status php7.4-fpm
    systemctl status php8.0-fpm
  5. Aktifkan beberapa module apache2 untuk menjalankan PHP-FPM nya.
    a2enmod actions fcgid alias proxy_fcgi
  6. Jika sudah silahkan restart apache nya.
    systemctl restart apache2
    systemctl status apache2

Selanjutnya buat virtual host untuk menjalankan semua subdomain dengan menentukan masing masing versi php yang akan dijalankan.
Sebelum itu silahkan buat dulu directory root website nya terlebih dahulu, contohnya disini saya buat seperti ini:
– Directory /var/www/html/luffy -> untuk luffy.alvianaufan.my.id
– Directory /var/www/html/zoro -> untuk zoro.alvianaufan.my.id
– Directory /var/www/html/sanji -> untuk sanji.alvianaufan.my.id

Lalu di masing-masing directory root buat satu file info.php

nano /var/www/html/luffy/info.php
<?php phpinfo(); ?>

– save dan exit

nano /var/www/html/zoro/info.php
<?php phpinfo(); ?>

– save dan exit

nano /var/www/html/sanji/info.php
<?php phpinfo(); ?>

– save dan exit



  • Lalu mulai membuat virtual host Apache nya.
  1. Buat file vhost.conf:
    nano /etc/apache2/conf-enable/vhost.conf

    – Lalu masukkan code berikut:

    <VirtualHost *:80>
    ServerAdmin halo@alvianaufan.my.id
    DocumentRoot "/var/www/html/luffy"
    ServerName luffy.alvianaufan.my.id
    ServerAlias www.luffy.alvianaufan.my.id
    ErrorLog /etc/apache2/error.log
    CustomLog /etc/apache2/access.log common
    
    <Directory "/var/www/html/luffy">
            Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted
    </Directory>
        <FilesMatch \.php$>
           # Apache 2.4.10+ can proxy to unix socket
            SetHandler "proxy:unix:/var/run/php/php7.3-fpm.sock|fcgi://localhost/"
        </FilesMatch>
    </VirtualHost>
    
    <VirtualHost *:80>
    ServerAdmin halo@alvianaufan.my.id
    DocumentRoot "/var/www/html/zoro"
    ServerName zoro.alvianaufan.my.id
    ServerAlias www.zoro.alvianaufan.my.id
    ErrorLog /etc/apache2/error.log
    CustomLog /etc/apache2/access.log common
    
    <Directory "/var/www/html/zoro">
            Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted
    </Directory>
        <FilesMatch \.php$>
           # Apache 2.4.10+ can proxy to unix socket
            SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
        </FilesMatch>
    </VirtualHost>
    
    <VirtualHost *:80>
    ServerAdmin halo@alvianaufan.my.id
    DocumentRoot "/var/www/html/sanji"
    ServerName sanji.alvianaufan.my.id
    ServerAlias www.sanji.alvianaufan.my.id
    ErrorLog /etc/apache2/error.log
    CustomLog /etc/apache2/access.log common
    
    <Directory "/var/www/html/sanji">
            Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted
    </Directory>
        <FilesMatch \.php$>
           # Apache 2.4.10+ can proxy to unix socket
            SetHandler "proxy:unix:/var/run/php/php8.0-fpm.sock|fcgi://localhost/"
        </FilesMatch>
    </VirtualHost>

    – Dapat dilihat dari code virtual host diatas ada code yang menentukan sebuah website akan berjalan menggunakan versi php yang mana, pada bagian (SetHandler “proxy:unix:/var/run/php/phpx.x-fpm.sock|fcgi://localhost/”)
    – save dan exit

  2. Silahkan restart apache nya dan coba akses semua subdomain tersebut di browser.
    systemctl restart apache2

    – Akses url http://luffy.alvianaufan.my.id/info.phpcara install phpfpm di ubuntu 20.04
    – Akses url http://zoro.alvianaufan.my.id/info.phpcara install phpfpm di ubuntu 20.04
    – Akses url http:/sanji.alvianaufan.my.id/info.phpcara install phpfpm di ubuntu 20.04

     

  3. Selanjutnya jika anda memerlukan install php module di masing masing versi php maka bisa dengan command:
    apt install php7.3-cli php7.3-curl php7.3-fpm php7.3-gd php7.3-gmp php7.3-json php7.3-mbstring php7.3-mysql php7.3-snmp php7.3-xml php7.3-zip -y
    apt install php7.4-cli php7.4-curl php7.4-fpm php7.4-gd php7.4-gmp php7.4-json php7.4-mbstring php7.4-mysql php7.4-snmp php7.4-xml php7.4-zip -y
    apt install php8.0-cli php8.0-curl php8.0-fpm php8.0-gd php8.0-gmp php8.0-json php8.0-mbstring php8.0-mysql php8.0-snmp php8.0-xml php8.0-zip -y
    systemctl restart php7.3-fpm
    systemctl restart php7.4-fpm
    systemctl restart php8.0-fpm
    systemctl restart apache2

     

Demikian cara menjalankan Multi PHP Version dengan PHP-FPM di Ubuntu Server 20.04. Silahkan jika anda memiliki pertanyaan atau menemui kendala bisa tulis komentar dibawah  ya 🙂



1 Comment

Write A Comment