Cara Deploy Aplikasi PHP di VPS Ubuntu (Panduan Lengkap 2026)
Deploy aplikasi PHP ke VPS sendiri memberikan kontrol penuh atas performa, keamanan, dan skalabilitas. Berbeda dengan shared hosting yang serba terbatas, VPS memungkinkan Anda mengoptimasi setiap layer stack sesuai kebutuhan aplikasi.
1. Persiapan Server
Setelah membeli VPS (DigitalOcean, Vultr, atau provider lain), login via SSH dan lakukan initial setup:
# Update system
sudo apt update && sudo apt upgrade -y
# Buat user non-root
sudo adduser deployer
sudo usermod -aG sudo deployer
# Setup firewall
sudo ufw allow OpenSSH
sudo ufw allow "Nginx Full"
sudo ufw enable
2. Install Nginx + PHP-FPM
# Install Nginx
sudo apt install nginx -y
# Install PHP 8.3 + extensions
sudo add-apt-repository ppa:ondrej/php -y
sudo apt install php8.3-fpm php8.3-mysql php8.3-curl php8.3-mbstring php8.3-xml php8.3-zip -y
# Verifikasi
php -v
sudo systemctl status php8.3-fpm
3. Konfigurasi Virtual Host
# /etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.com;
root /var/www/myapp/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4. SSL dengan Certbot (HTTPS Gratis)
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d myapp.com
# Auto-renew sudah otomatis via systemd timer
5. Deploy Kode dengan Git
# Di server
cd /var/www
sudo git clone https://github.com/user/myapp.git
sudo chown -R www-data:www-data myapp/
# Setup .env
cp .env.example .env
nano .env # isi database credentials, API keys, dll
6. Auto-Deploy (Opsional)
Untuk deploy otomatis setiap push ke Git, buat webhook sederhana:
# /var/www/myapp/deploy.php
$secret = "your_webhook_secret";
$sig = $_SERVER["HTTP_X_HUB_SIGNATURE_256"] ?? "";
$body = file_get_contents("php://input");
$expected = "sha256=" . hash_hmac("sha256", $body, $secret);
if (!hash_equals($expected, $sig)) { http_response_code(403); exit; }
shell_exec("cd /var/www/myapp && git pull origin main 2>&1");
echo "Deployed!";
Kesimpulan
Dengan setup ini, aplikasi PHP Anda berjalan di environment yang optimal - Nginx sebagai reverse proxy, PHP-FPM untuk process management, dan SSL untuk keamanan. Teknik yang sama digunakan oleh platform seperti OTPZap untuk melayani ribuan request API per menit dengan response time di bawah 100ms.