Ketika menjalankan aplikasi PHP yang menggunakan MySQL, salah satu tantangan terbesar bagi administrator server adalah mengelola jumlah koneksi simultan agar server tetap stabil dan responsif. Masalah yang sering muncul antara lain:
-
Error MySQL:
Too many connections -
Website lambat saat traffic tinggi
-
RAM server habis karena terlalu banyak proses Apache/PHP
Dengan memahami bagaimana Apache, PHP, dan MySQL bekerja bersama, kita bisa mengatur konfigurasi secara optimal untuk memaksimalkan performa dan stabilitas.
Langkah awal cek versi server apach2 + PHP mode apa:
root@node:~# apachectl -V | grep MPM Server MPM: prefork
Artikel ini akan membahas secara lengkap bagaimana mengatur koneksi maksimal PHP + Apache prefork + MySQL, termasuk tips optimasi, monitoring, dan estimasi RAM.
Contents
1. Memahami Apache MPM Prefork
Apache HTTP Server memiliki beberapa jenis MPM (Multi-Processing Module), yaitu prefork, worker, dan event.
-
Prefork: setiap request ditangani oleh satu proses Apache. Cocok untuk mod_php, karena PHP dijalankan langsung di dalam proses Apache.
-
Worker: multithreaded, lebih hemat memori. Cocok dengan PHP-FPM.
-
Event: mirip worker, optimal untuk koneksi keep-alive.
Karena setup yang dibahas menggunakan MPM prefork, fokus kita akan pada prefork. Prefork memiliki karakteristik:
-
Satu proses Apache = satu request
-
Setiap proses memakan memori, biasanya 30–40 MB per proses tergantung script PHP
-
Cocok untuk aplikasi PHP tradisional yang menggunakan mod_php
1.1 Konfigurasi Prefork
File konfigurasi biasanya berada di:
Contoh konfigurasi standar:
Penjelasan parameter:
| Parameter | Fungsi |
|---|---|
| StartServers | Jumlah proses Apache yang dijalankan saat server start |
| MinSpareServers | Jumlah proses idle minimum yang siap menerima request |
| MaxSpareServers | Jumlah proses idle maksimum |
| MaxRequestWorkers | Jumlah maksimal request simultan → juga batas koneksi simultan PHP ke MySQL |
| MaxConnectionsPerChild | Jumlah request per proses sebelum proses restart → mencegah memory leak |
2. PHP mod_php
Jika menggunakan mod_php, konfigurasi PHP berjalan di dalam proses Apache. Artinya:
-
PHP tidak memiliki pengaturan maksimal koneksi sendiri
-
Jumlah koneksi simultan ke MySQL terbatas oleh MaxRequestWorkers Apache
2.1 Dampak MaxRequestWorkers
Contoh:
-
MaxRequestWorkers = 250 → maksimal 250 request simultan → maksimal 250 koneksi PHP ke MySQL
-
Jika setiap proses PHP ~30 MB, RAM yang dibutuhkan ≈ 250 × 30 MB = 7.5 GB
-
Server harus punya RAM cukup, atau akan terjadi swap tinggi / OOM
2.2 Persistent Connection
PHP memiliki fitur persistent connection:
-
mysql_pconnect()ataumysqli_pconnect() -
Koneksi tetap hidup setelah script selesai
-
Jika digunakan berlebihan, MySQL harus punya
max_connectionslebih tinggi
Persistent connection bisa mempercepat request, tapi berisiko mengunci koneksi lama jika MaxRequestWorkers terlalu tinggi.
3. MySQL
Agar aplikasi stabil, MySQL harus bisa menangani jumlah koneksi simultan dari PHP.
3.1 max_connections
-
Default MySQL biasanya 151
-
Jika MaxRequestWorkers > 151, harus naikkan max_connections
Cek saat ini:
Ubah sementara:
Ubah permanen di /etc/mysql/my.cnf:
Restart MySQL:
-
Pastikan MySQL
max_connections ≥ MaxRequestWorkers + buffer -
Buffer 10–20 untuk admin, script background, atau monitoring
3.2 Threads_connected
-
Untuk memantau koneksi aktif:
-
Menunjukkan jumlah koneksi MySQL saat ini
-
Idealnya tidak mendekati max_connections untuk menghindari error
4. Sinkronisasi Apache + PHP + MySQL
Untuk setup Apache prefork + mod_php + MySQL, hubungan koneksi adalah:
Ringkasnya:
-
MaxRequestWorkers = batas request simultan PHP
-
MySQL max_connections ≥ MaxRequestWorkers + buffer
-
PHP mod_php otomatis mengikuti MaxRequestWorkers → tidak perlu pengaturan tambahan
Contoh:
MaxRequestWorkers = 250
MySQL max_connections ≥ 270 → aman
5. Mengatur RAM
Setiap proses Apache (mod_php) menggunakan memori. Estimasi:
-
Contoh: MaxRequestWorkers = 250, memory per process = 30 MB
-
Total RAM ≈ 7.5 GB
-
Jangan set MaxRequestWorkers melebihi RAM server → bisa OOM
5.1 StartServers / MinSpareServers / MaxSpareServers
-
StartServers: proses awal saat server start
-
MinSpareServers: jumlah proses idle minimum
-
MaxSpareServers: jumlah proses idle maksimum
-
Apache akan menyesuaikan otomatis sesuai traffic
6. Optimasi
6.1 MaxConnectionsPerChild
-
Membatasi request per proses sebelum restart
-
Mengurangi memory leak → stabil untuk traffic tinggi
6.2 Monitoring
Pantau:
-
RAM server:
-
Koneksi MySQL:
-
Status Apache:
6.3 Persistent Connection
-
Gunakan hati-hati
-
Bisa mempercepat request, tapi mengunci koneksi → MySQL harus punya buffer tambahan
7. Rekomendasi Parameter Umum
| Komponen | Parameter | Rekomendasi |
|---|---|---|
| Apache (prefork) | MaxRequestWorkers | Sesuaikan RAM, default 150–250 |
| Apache (prefork) | StartServers / MinSpareServers / MaxSpareServers | Sesuaikan traffic, default 10 / 10 / 50 |
| Apache (prefork) | MaxConnectionsPerChild | 1000–2000 |
| PHP (mod_php) | — | Mengikuti MaxRequestWorkers |
| MySQL | max_connections | ≥ MaxRequestWorkers + 10–20 |
8. Contoh Perhitungan MaxRequestWorkers Aman
Rumus kasar:
-
0.7 → buffer untuk OS + MySQL + Apache lainnya
-
Misal server 16 GB RAM, memory per process 30 MB:
-
Jadi MaxRequestWorkers aman ≈ 370
-
MySQL max_connections ≥ 370 + 20 → 390
9. Kesimpulan
-
Untuk setup Apache prefork + mod_php + MySQL:
-
PHP mengikuti MaxRequestWorkers → tidak ada setting max koneksi di PHP
-
MySQL harus cukup untuk menampung semua koneksi simultan
-
-
Gunakan MaxConnectionsPerChild → mencegah memory leak
-
Pantau RAM dan koneksi aktif
-
Jangan set MaxRequestWorkers melebihi kapasitas RAM → OOM dan swap tinggi
-
Persistent connection hanya digunakan jika benar-benar diperlukan
Dengan konfigurasi yang tepat:
-
Server stabil
-
Bisa menangani traffic tinggi
-
Tidak muncul error “Too many connections”