Web Server ConfigurationKonfigurasi Web Server

Apache vs Nginx: Setting Up & Troubleshooting Virtual Hosts on a VPSApache vs Nginx: Setup & Troubleshooting Virtual Host di VPS

A practical session on how Apache and Nginx actually work, how to configure a virtual host or server block, and how to diagnose the errors you'll hit running a website on a Hostinger VPS plan. Sesi praktis tentang cara kerja Apache dan Nginx, cara konfigurasi virtual host atau server block, dan cara mendiagnosis error yang bakal kamu temui saat menjalankan website di VPS Hostinger.

AudienceTarget: Mixed — new to VPS through comfortable with the CLICampuran — baru kenal VPS sampai yang sudah lancar CLI FormatFormat: 9 slides + quizslide + kuis
FoundationDasar

Two different ways to answer the same requestDua cara berbeda menjawab request yang sama

A web server's job is simple to describe: take a request from a visitor's browser, pull the right files, send a response back. Apache and Nginx do that same job with fundamentally different internal architecture.Tugas web server sebenarnya simpel: ambil request dari browser visitor, ambil file yang tepat, kirim balik responnya. Apache dan Nginx melakukan tugas yang sama tapi dengan arsitektur internal yang beda banget.

Apache
Process/thread-based — one worker is spun up per request and does everything itself (reading PHP, applying modules, building the response). Great for flexibility, but under heavy concurrent load, workers run out and new visitors get a 503. Berbasis proses/thread — satu worker dibuat per request dan mengerjakan semuanya sendiri (baca PHP, jalankan modul, susun response). Fleksibel banget, tapi kalau traffic bersamaan tinggi, worker-nya habis dan visitor baru kena 503.
Nginx
Event-driven — a small number of worker processes handle thousands of connections at once asynchronously. It hands PHP execution off to a separate process (PHP-FPM) instead of doing it itself. Event-driven — sejumlah kecil worker process menangani ribuan koneksi sekaligus secara asynchronous. Nginx melempar eksekusi PHP ke proses terpisah (PHP-FPM), bukan dikerjakan sendiri.
Restaurant analogy:Analogi restoran: Apache is a waiter who does everything from A to Z alone — takes the order, cooks, serves. Capable, but there's only one of them, so a full house means new guests wait. Nginx is more like a cashier who delegates: it hands the "cooking" (PHP) to a chef (PHP-FPM) and focuses on handling many guests' requests efficiently at once — which is why it shines for static files (images, CSS, JS) that don't need a "chef" at all. Apache itu seperti pelayan yang mengerjakan semua dari A sampai Z sendirian — terima order, masak, sajikan. Serba bisa, tapi cuma satu orang, jadi kalau restoran penuh, tamu baru harus nunggu. Nginx lebih mirip kasir yang mendelegasikan: dia lempar bagian "masak" (PHP) ke seorang chef (PHP-FPM) dan fokus melayani banyak request tamu secara efisien — makanya Nginx jago banget untuk file statis (gambar, CSS, JS) yang nggak butuh "chef" sama sekali.
Why this matters on a VPSKenapa ini penting di VPS
Apache is the easier starting point for small sites — WordPress works out of the box, and per-folder .htaccess config means no server restart needed. Nginx takes a bit more setup but scales much further on the same VPS resources, which matters for campaign or checkout traffic spikes. Apache titik awal yang lebih mudah untuk website kecil — WordPress langsung jalan, dan config per-folder lewat .htaccess berarti nggak perlu restart server. Nginx butuh sedikit lebih banyak setup tapi jauh lebih scalable dengan resource VPS yang sama, penting banget kalau ada lonjakan traffic campaign atau checkout.
Configuration StyleGaya Konfigurasi

Modules vs. server blocksModul vs. server block

Both servers extend themselves through modules — but where that config lives, and how it's applied, is very different.Kedua server memperluas fungsinya lewat modul — tapi di mana config-nya disimpan, dan bagaimana diterapkan, sangat berbeda.

Apache — modules + .htaccessmodul + .htaccess
Modules are enabled server-wide (e.g. a2enmod rewrite on Ubuntu). Commonly used ones: Modul diaktifkan di level server (mis. a2enmod rewrite di Ubuntu). Yang sering dipakai:
  • mod_rewriteURL rewriting (WordPress/Laravel pretty links)rewrite URL (link cantik WordPress/Laravel)
  • mod_sslHTTPS/TLS supportdukungan HTTPS/TLS
  • mod_headersresponse headers, CORS, caching rulesheader response, CORS, aturan caching

.htaccess lets you override config per-folder without restarting the whole server — ideal for shared hosting, but every request checks these files, adding overhead..htaccess memungkinkan override config per-folder tanpa restart seluruh server — cocok untuk shared hosting, tapi setiap request mengecek file ini, jadi ada overhead.

Nginx — centralized server blocksserver block terpusat
Nginx has no .htaccess equivalent. All routing and behavior lives in config files under /etc/nginx/sites-available/, which means Nginx fails to start on a config error instead of silently misbehaving — a change requires systemctl reload nginx. Nginx tidak punya padanan .htaccess. Semua routing dan behavior ada di file config di /etc/nginx/sites-available/, artinya Nginx akan gagal start kalau ada error config, bukan diam-diam salah jalan — perubahan butuh systemctl reload nginx.
MisconceptionMiskonsepsi
"Nginx supports .htaccess too, it's just optional." No — Nginx has no per-directory config file at all. Any rule you want must go in the main server block."Nginx juga support .htaccess, cuma opsional." Salah — Nginx sama sekali tidak punya file config per-direktori. Aturan apapun harus masuk ke server block utama.
Correct modelModel yang benar
On Nginx, folder-level rules (redirects, auth) are written directly into the server block's location blocks and applied on reload.Di Nginx, aturan level-folder (redirect, auth) ditulis langsung di dalam blok location pada server block dan diterapkan saat reload.
Setup — ApacheSetup — Apache

Configuring an Apache virtual hostKonfigurasi virtual host Apache

A virtual host tells Apache which folder and domain to serve. Here's the standard flow on a fresh Hostinger VPS (Ubuntu).Virtual host memberi tahu Apache folder dan domain mana yang harus dilayani. Ini alur standar di VPS Hostinger baru (Ubuntu).

1
Install Apache & PHPInstall Apache & PHP
sudo apt install apache2 -y sudo apt install php libapache2-mod-php php-mysql -y sudo ufw allow 'Apache Full'
2
Create the site config fileBuat file config situs at di /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined </VirtualHost>
3
Enable the site and reloadAktifkan situs dan reload
sudo a2ensite example.com.conf sudo a2enmod rewrite ssl headers sudo apache2ctl configtest sudo systemctl reload apache2
Tip:Tips: Always run apache2ctl configtest before reloading — Apache will still start with a broken config in some cases, and you want to catch a typo (like a missing </VirtualHost>) before it causes a 500 error for visitors. Selalu jalankan apache2ctl configtest sebelum reload — Apache kadang tetap start meski config-nya rusak, dan kamu mau menangkap typo (seperti </VirtualHost> yang hilang) sebelum bikin visitor kena error 500.
Setup — NginxSetup — Nginx

Configuring an Nginx server blockKonfigurasi server block Nginx

Nginx needs one extra piece Apache doesn't: a working connection to PHP-FPM, since Nginx never executes PHP itself.Nginx butuh satu hal tambahan yang Apache tidak perlukan: koneksi yang jalan ke PHP-FPM, karena Nginx sendiri tidak pernah mengeksekusi PHP.

1
Install Nginx & PHP-FPMInstall Nginx & PHP-FPM
sudo apt install nginx -y sudo apt install php-fpm php-mysql -y sudo ufw allow 'Nginx Full'
2
Create the server blockBuat server block at di /etc/nginx/sites-available/example.com
server { listen 80; server_name example.com www.example.com; root /var/www/example.com/public_html; index index.php index.html; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock; } }
3
Enable, test, reloadAktifkan, tes, reload
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
Common mistakeKesalahan umum
Copying the location ~ \.php$ block but forgetting to match the actual PHP-FPM socket path/version — this is the #1 cause of a 502 on a fresh Nginx setup.Copy blok location ~ \.php$ tapi lupa sesuaikan path socket/versi PHP-FPM yang sebenarnya — ini penyebab #1 error 502 di setup Nginx baru.
Check firstCek dulu
Run ls /run/php/ to confirm the actual socket filename before pasting it into the config.Jalankan ls /run/php/ untuk memastikan nama file socket yang sebenarnya sebelum ditempel ke config.
TroubleshootingTroubleshooting

Reading HTTP error codes correctlyMembaca kode error HTTP dengan benar

The same error code means something different depending on which server produced it — that's the single most useful thing to internalize for troubleshooting.Kode error yang sama bisa berarti beda tergantung server mana yang menghasilkannya — ini hal paling berguna yang perlu dipahami untuk troubleshooting.

Apache
Nginx
500
Almost always a typo in .htaccess — one wrong character breaks the whole file.Hampir selalu typo di .htaccess — satu karakter salah merusak seluruh file.
Rare from config (Nginx won't start with a bad one). Usually a crashed PHP script.Jarang dari config (Nginx tidak akan start kalau config-nya salah). Biasanya PHP script yang crash.
502
Rare with mod_php, since Apache runs PHP itself.Jarang terjadi dengan mod_php, karena Apache menjalankan PHP sendiri.
PHP-FPM is down or the socket path/version is wrong.PHP-FPM mati atau path/versi socket-nya salah.
503
Worker/MaxClients pool is full — all "waiters" are busy.Pool Worker/MaxClients penuh — semua "pelayan" sibuk.
Usually seen only when Nginx is a load balancer and all backends are down.Biasanya hanya muncul saat Nginx jadi load balancer dan semua backend mati.
504
Rare — Apache tends to just drop the connection instead.Jarang — Apache cenderung langsung memutus koneksi.
PHP-FPM answered but took longer than Nginx's timeout (e.g. a huge upload/long script).PHP-FPM menjawab tapi lebih lama dari timeout Nginx (mis. upload besar/script lama).
403
Deny from all in .htaccess, or wrong folder permissions.Deny from all di .htaccess, atau permission folder salah.
No index.php/index.html found, and autoindex (directory listing) is disabled by default.Tidak ada index.php/index.html, dan autoindex (daftar direktori) dinonaktifkan secara default.
Where to look firstCek dulu di mana
tail -f /var/log/apache2/error.log   oratau   tail -f /var/log/nginx/error.log
Skill — Diagnosing an OutageSkill — Mendiagnosis Outage

A repeatable troubleshooting workflowAlur troubleshooting yang bisa diulang

When a site goes down on your VPS, work through these in order rather than guessing. Tap each step as you go.Kalau situs down di VPS-mu, kerjakan langkah ini secara urut, jangan menebak-nebak. Tap tiap langkah saat kamu jalankan.

Check the exact error code the browser shows (500 / 502 / 503 / 504 / 403) — it tells you which layer to investigate.
Cek kode error persis yang muncul di browser (500 / 502 / 503 / 504 / 403) — ini menunjukkan layer mana yang harus dicek.
Tail the error log (apache2/error.log or nginx/error.log) for the exact line and file that failed.
Tail error log (apache2/error.log atau nginx/error.log) untuk melihat baris dan file persis yang gagal.
Test config syntax before touching a live service: apache2ctl configtest or nginx -t.
Tes syntax config sebelum menyentuh service yang live: apache2ctl configtest atau nginx -t.
Confirm the backing service is alive — PHP-FPM socket for Nginx (systemctl status php8.3-fpm), worker pool for Apache.
Pastikan service pendukungnya hidup — socket PHP-FPM untuk Nginx (systemctl status php8.3-fpm), worker pool untuk Apache.
Check folder/file permissions if you're seeing 403s (chmod/ownership) rather than assuming it's a config bug.
Cek permission folder/file kalau muncul 403 (chmod/ownership) daripada langsung asumsi bug config.
Reload (not restart) the service after a config fix to avoid unnecessary downtime: systemctl reload apache2 / nginx.
Reload (bukan restart) service setelah fix config supaya tidak downtime yang nggak perlu: systemctl reload apache2 / nginx.
0 / 6 steps completed0 / 6 langkah selesai
DecisionKeputusan

Which one fits your Hostinger VPS?Mana yang cocok untuk VPS Hostinger-mu?

Neither is "better" — the right choice depends on traffic pattern and how much config work you want to own.Nggak ada yang "lebih bagus" — pilihan yang tepat tergantung pola traffic dan seberapa banyak config yang mau kamu urus sendiri.

DimensionDimensiApacheNginx
Ease of setupKemudahan setup⭐⭐⭐⭐⭐ works instantlylangsung jalan⭐⭐⭐ needs PHP-FPM wiringperlu hubungkan PHP-FPM
PHPmod_php (built in)(built in)PHP-FPM (separate process)(proses terpisah)
Config locationLokasi configSpread out, can use .htaccessTersebar, bisa pakai .htaccessCentralized in /etc/nginx/Terpusat di /etc/nginx/
Best forCocok untukSmall/medium sites, WordPress, quick launchesWebsite kecil/menengah, WordPress, launching cepatHigh traffic, campaigns, checkout spikes, static-heavy sitesTraffic tinggi, campaign, lonjakan checkout, situs banyak konten statis
Restart commandPerintah restartsystemctl restart apache2systemctl restart nginx
Practical rule of thumb:Aturan praktis: Starting a new small business site or WordPress blog on a Hostinger VPS? Apache gets you live fastest. Expecting a traffic spike from a sale, registration period, or you're scaling an app? Nginx (or Apache + Nginx as a reverse proxy in front of it) handles concurrency far better on the same VPS resources. Baru mulai bisnis kecil atau blog WordPress di VPS Hostinger? Apache bikin kamu live paling cepat. Menantikan lonjakan traffic dari promo, periode pendaftaran, atau lagi scale aplikasi? Nginx (atau Apache + Nginx sebagai reverse proxy di depannya) menangani concurrency jauh lebih baik dengan resource VPS yang sama.
Check Your UnderstandingCek Pemahamanmu

Quick quizKuis singkat