Apache: Running Multiple PHP Versions

From Catatan IBM
Jump to navigation Jump to search

Sebelumnya pastikan sudah menginstall Apache dulu

Asumsi pemasangan PHP7.4 dan PHP8.1

Insstall PHP v7.4 dan v8.1 dengan PHP-FPM

Install Software-properties

sudo apt-get install software-properties-common -y

Menambahkan repo PHP

sudo add-apt-repository ppa:ondrej/php

Update

sudo apt-get update -y

Saatnya install php7.4

sudo apt-get install php7.4 php7.4-fpm php7.4-mysql libapache2-mod-php7.4 libapache2-mod-fcgid -y

dimana:

  • php7.2 is a metapackage used to run PHP applications.
  • php7.2-fpm provides the Fast Process Manager interpreter that runs as a daemon and receives Fast/CGI requests.
  • php7.2-mysql connects PHP to the MySQL database.
  • libapache2-mod-php7.2 provides the PHP module for the Apache webserver.
  • libapache2-mod-fcgid contains a mod_fcgid that starts a number of CGI program instances to handle concurrent requests.


Ulangi dengan install php8.1

sudo apt-get install php7.3 php7.3-fpm php7.3-mysql libapache2-mod-php7.3 -y

selesai. Sekarang jalankan servis di versi masing-masing

sudo systemctl start php7.4-fpm

Verifikasi apakah sudah berjalan dengan baik

sudo systemctl status php7.4-fpm

Jika tampil status running seperti ini, maka sudah berjalan dengan baik

Output
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
    Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2020-06-05 11:25:07 UTC; 1min 38s ago
      Docs: man:php-fpm7.4(8)
  Main PID: 13703 (php-fpm7.4)
    Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
     Tasks: 3 (limit: 2353)
    Memory: 6.2M
    CGroup: /system.slice/php7.4-fpm.service
            ├─13703 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
            ├─13719 php-fpm: pool www
            └─13720 php-fpm: pool www

Jun 05 11:25:07 ubuntu systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Jun 05 11:25:07 ubuntu systemd[1]: Started The PHP 7.4 FastCGI Process Manager.


Ulangi untuk versi 8.1

sudo systemctl start php8.1-fpm

Verifikasi statusnya

sudo systemctl status php8.1-fpm

Tampilan pesan bahwa sudah berjalan dengan baik

Output
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
    Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2020-06-05 11:26:33 UTC; 56s ago
      Docs: man:php-fpm8.1(8)
   Process: 23470 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 73 (code=ex>
  Main PID: 23452 (php-fpm8.1)
    Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
     Tasks: 3 (limit: 2353)
    Memory: 7.1M
    CGroup: /system.slice/php8.1-fpm.service
            ├─23452 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
            ├─23468 php-fpm: pool www
            └─23469 php-fpm: pool www

Jun 05 11:26:33 ubuntu systemd[1]: Starting The PHP 8.1 FastCGI Process Manager...
Jun 05 11:26:33 ubuntu systemd[1]: Started The PHP 8.1 FastCGI Process Manager.


Terakhir, aktifkan beberapa modul supaya apache bisa jalan di beberapa versi php

sudo a2enmod actions fcgid alias proxy_fcgi

dimana:

  • actions is used for executing CGI scripts based on media type or request method.
  • fcgid is a high performance alternative to mod_cgi that starts a sufficient number of instances of the CGI program to handle concurrent requests.
  • alias provides for the mapping of different parts of the host filesystem in the document tree, and for URL redirection.
  • proxy_fcgi allows Apache to forward requests to PHP-FPM.

Restart service

sudo systemctl restart apache2


Membuat Struktur Directory

Asumsi membuat site1.com dan site2.com

sudo mkdir /var/www/site1.com
sudo mkdir /var/www/site2.com

Secara default, apache berjalan di www-data user dan www-data grup

sudo chown -R www-data:www-data /var/www/site1.com
sudo chown -R www-data:www-data /var/www/site2.com
sudo chmod -R 755 /var/www/site1.com
sudo chmod -R 755 /var/www/site2.com

Coba membuat file info.php di masing- masing versi

sudo nano /var/www/site1.com/info.php

isi file dengan:

<?php phpinfo(); ?>

copy file site1 ke site2

sudo cp /var/www/site1.com/info.php /var/www/site2.com/info.php


Konfigurasi Apache

Domain site1.com

sudo nano /etc/apache2/sites-available/site1.com.conf

Isi Konfigurasi

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName site1.com
    DocumentRoot /var/www/site1.com
    DirectoryIndex info.php

    <Directory /var/www/site1.com>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>

   <FilesMatch \.php$>
       # From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
        SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
   </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/site1.your_domain_error.log
    CustomLog ${APACHE_LOG_DIR}/site1.your_domain_access.log combined
</VirtualHost>


Domain site2.com

sudo nano /etc/apache2/sites-available/site2.com.conf

Isi konfigurasi

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName site2.com
    DocumentRoot /var/www/site2.com
    DirectoryIndex info.php

    <Directory /var/www/site2.com>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>

   <FilesMatch \.php$>
       # 2.4.10+ can proxy to unix socket
        SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
   </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/site2.your_domain_error.log
    CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined
</VirtualHost>

Tes apakah ada syntax yang error atau tidak

sudo apachectl configtest

Aktifkan konfigurasi domain

sudo a2ensite site1.com
sudo a2ensite site2.com

Non Aktifkan konfigurasi bawaan

sudo a2dissite 000-default.conf

Restart service

sudo systemctl restart apache2

Test Website

Akses di browser dengan membukan URL http://site1.com and http://site2.com, maka akan tampil informasi PHP di masing-masing versi