
Panduan ini menambahkan multi-PHP per folder sehingga kamu bisa mengakses project dengan versi PHP berbeda pada satu domain (misalnya http://multi.local/php74
menggunakan PHP 7.4, /php81
menggunakan PHP 8.1, dan /php82
menggunakan PHP 8.2).
Prasyarat
Pastikan Nginx, beberapa versi PHP-FPM (7.4/8.1/8.2), MariaDB, dan PhpMyAdmin sudah terpasang seperti panduan sebelumnya.
sudo apt update && sudo apt upgrade -y
1️⃣ Siapkan Direktori Project Multi-PHP
sudo mkdir -p /var/www/multi/{php74,php81,php82}
echo "<?php phpinfo(); ?>" | sudo tee /var/www/multi/php74/index.php >/dev/null
echo "<?php phpinfo(); ?>" | sudo tee /var/www/multi/php81/index.php >/dev/null
echo "<?php phpinfo(); ?>" | sudo tee /var/www/multi/php82/index.php >/dev/null
sudo chown -R www-data:www-data /var/www/multi
Struktur akhirnya: /var/www/multi/php74
, /var/www/multi/php81
, /var/www/multi/php82
. Simpan file project kamu sesuai versi PHP yang diinginkan di masing-masing folder.
2️⃣ Buat Server Block Nginx (Multi-PHP per Folder)
Buat file /etc/nginx/sites-available/multi.local
dengan isi berikut:
server {
listen 80;
server_name multi.local;
root /var/www/multi;
index index.php index.html;
# Halaman root opsional (daftar link)
location = / {
return 200 "<h3>Multi-PHP Demo</h3><ul><li><a href='/php74'>PHP 7.4</a></li><li><a href='/php81'>PHP 8.1</a></li><li><a href='/php82'>PHP 8.2</a></li></ul>";
}
# Static files caching ringan
location ~* \.(?:css|js|jpg|jpeg|png|gif|ico|svg|webp)$ {
try_files $uri =404;
expires 7d;
access_log off;
}
# ===== Folder PHP 7.4 =====
location ^~ /php74/ {
try_files $uri $uri/ /php74/index.php?$args;
}
location ~ ^/php74/.*\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
# ===== Folder PHP 8.1 =====
location ^~ /php81/ {
try_files $uri $uri/ /php81/index.php?$args;
}
location ~ ^/php81/.*\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
# ===== Folder PHP 8.2 =====
location ^~ /php82/ {
try_files $uri $uri/ /php82/index.php?$args;
}
location ~ ^/php82/.*\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
# Keamanan dasar: blok akses ke file sensitif
location ~* \.(?:ini|env|log|sql|bak)$ { deny all; }
}
Aktifkan site dan reload Nginx:
sudo ln -s /etc/nginx/sites-available/multi.local /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
3️⃣ Tambah DNS Lokal (hosts)
Agar domain multi.local
bisa diakses di mesin kamu, tambahkan entri pada hosts:
echo "127.0.0.1 multi.local" | sudo tee -a /etc/hosts
Sekarang buka di browser:
✔ http://multi.local/php74
→ berjalan di PHP 7.4
✔ http://multi.local/php81
→ berjalan di PHP 8.1
✔ http://multi.local/php82
→ berjalan di PHP 8.2
4️⃣ Tips & Catatan Penting
- Penempatan file: Simpan project sesuai versi PHP di
/var/www/multi/php74
,/php81
, atau/php82
. - Jangan pakai
alias
untuk PHP kecuali paham implikasinya. Konfigurasi di atas menggunakanroot
+$document_root$fastcgi_script_name
agar SCRIPT_FILENAME tepat. - Extensi PHP: Install modul yang dibutuhkan per versi (contoh:
php8.2-mysql
,php8.1-xml
, dll). Contoh:sudo apt install php7.4-mysql php8.1-mysql php8.2-mysql -y
- Service FPM: Pastikan semua socket aktif:
systemctl status php7.4-fpm php8.1-fpm php8.2-fpm
- PhpMyAdmin: Bisa tetap di satu versi PHP (misal 8.1). Jika domainnya
phpmyadmin.local
, pastikan di server block pakai:fastcgi_pass unix:/run/php/php8.1-fpm.sock;
5️⃣ (Opsional) Per-Domain per PHP
Kalau ingin tiap versi punya domain sendiri (mis. 74.local
, 81.local
, 82.local
), buat tiga server block terpisah dan ubah fastcgi_pass
ke socket FPM yang sesuai. Cara ini kadang lebih rapi untuk produksi.
Selesai
Dengan konfigurasi ini, kamu bisa menaruh project di folder yang sesuai versi PHP, dan Nginx otomatis meneruskan ke PHP-FPM yang tepat. Praktis untuk testing lintas versi maupun migrasi aplikasi PHP.
Posting Komentar