Dari VPS kosong sampai aplikasi Laravel berjalan dengan HTTPS, queue worker, dan skrip deploy yang bisa diulang.
Pendahuluan
Shared hosting cukup untuk tahap awal, tetapi begitu aplikasi butuh queue, scheduler, atau kontrol penuh atas versi PHP, VPS adalah langkah berikutnya. Artikel ini membahas deployment Laravel di Ubuntu 24.04 LTS dengan susunan klasik yang stabil: Nginx sebagai web server, PHP-FPM sebagai prosesor PHP, dan MySQL sebagai database.
Asumsi dalam panduan ini:
- VPS Ubuntu 24.04 dengan akses SSH dan hak
sudo. - Domain
contoh-domain.comsudah mengarah (A record) ke IP VPS. - Aplikasi Laravel 11 atau 12 tersimpan di repository Git.
- PHP 8.3, yang tersedia di repository bawaan Ubuntu 24.04.
Ganti contoh-domain.com, nama database, dan password dengan milik Anda.
1. Persiapan Server
Login sebagai root atau user sudo, lalu perbarui paket.
ssh root@IP_VPS
apt update && apt upgrade -y
Buat user khusus deploy agar tidak bekerja sebagai root.
adduser deploy
usermod -aG sudo deploy
rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy
Aktifkan firewall dan izinkan hanya port yang dibutuhkan.
ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw enable
ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
Pastikan
OpenSSHsudah diizinkan sebelumufw enable, kalau tidak sesi SSH Anda bisa terputus.
Mulai langkah berikutnya sebagai user deploy:
su - deploy
2. Instal Nginx, PHP-FPM, dan Ekstensi
sudo apt install -y nginx git unzip curl \
php8.3-fpm php8.3-cli php8.3-mysql php8.3-mbstring php8.3-xml \
php8.3-curl php8.3-zip php8.3-bcmath php8.3-intl php8.3-gd php8.3-redis
Periksa versi dan status layanan.
php -v
systemctl status php8.3-fpm --no-pager
systemctl status nginx --no-pager
PHP 8.3.6 (cli) (built: ...) (NTS)
● php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
Active: active (running)
Instal Composer
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
rm composer-setup.php
composer --version
3. Instal dan Amankan MySQL
sudo apt install -y mysql-server
sudo mysql_secure_installation
Buat database dan user khusus aplikasi. Jangan memakai user root untuk aplikasi.
sudo mysql
CREATE DATABASE laravel_app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'GantiDenganPasswordKuat!2026';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Uji koneksi:
mysql -u laravel_user -p -e "SHOW DATABASES;"
4. Mengambil Source Code
sudo mkdir -p /var/www/laravel-app
sudo chown deploy:deploy /var/www/laravel-app
cd /var/www/laravel-app
git clone git@github.com:username/laravel-app.git .
composer install --no-dev --optimize-autoloader
Untuk repository privat, tambahkan deploy key di pengaturan repository agar server bisa melakukan git pull tanpa password.
Konfigurasi .env
cp .env.example .env
php artisan key:generate
nano .env
APP_NAME="Laravel App"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://contoh-domain.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=laravel_user
DB_PASSWORD=GantiDenganPasswordKuat!2026
SESSION_SECURE_COOKIE=true
APP_DEBUG=false wajib di production. Mode debug membocorkan isi .env dan stack trace ke pengunjung.
Jalankan migrasi dan cache konfigurasi:
php artisan migrate --force
php artisan storage:link
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
5. Hak Akses File
Nginx dan PHP-FPM berjalan sebagai user www-data. Folder storage dan bootstrap/cache harus bisa ditulis oleh user tersebut.
sudo chown -R deploy:www-data /var/www/laravel-app
sudo find /var/www/laravel-app -type d -exec chmod 755 {} \;
sudo find /var/www/laravel-app -type f -exec chmod 644 {} \;
sudo chmod -R 775 storage bootstrap/cache
sudo chmod g+s storage bootstrap/cache
Perintah chmod g+s (setgid) membuat file baru di folder itu mewarisi grup www-data, sehingga log dan cache yang dibuat oleh proses berbeda tetap bisa ditulis bersama.
Jangan pernah memakai chmod 777.
6. Konfigurasi Nginx
Buat server block baru.
sudo nano /etc/nginx/sites-available/laravel-app
server {
listen 80;
listen [::]:80;
server_name contoh-domain.com www.contoh-domain.com;
root /var/www/laravel-app/public;
index index.php;
charset utf-8;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
client_max_body_size 20M;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
# blokir file tersembunyi kecuali .well-known (dipakai Let's Encrypt)
location ~ /\.(?!well-known).* {
deny all;
}
}
Aktifkan situs, uji konfigurasi, lalu muat ulang Nginx.
sudo ln -s /etc/nginx/sites-available/laravel-app /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Perhatikan bahwa root mengarah ke folder public, bukan ke root proyek. Kalau salah, file .env bisa diunduh orang lain.
7. HTTPS dengan Let's Encrypt
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d contoh-domain.com -d www.contoh-domain.com
Certbot akan mengubah konfigurasi Nginx untuk HTTPS dan mengalihkan HTTP ke HTTPS. Uji pembaruan otomatis:
sudo certbot renew --dry-run
8. Queue Worker dengan Supervisor
Kalau aplikasi memakai queue (email, laporan, dan sebagainya), worker harus berjalan terus dan hidup kembali saat crash.
sudo apt install -y supervisor
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel-app/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/laravel-app/storage/logs/worker.log
stopwaitsecs=3600
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status
Untuk scheduler, tambahkan satu baris cron:
sudo crontab -u www-data -e
* * * * * cd /var/www/laravel-app && php artisan schedule:run >> /dev/null 2>&1
9. Skrip Deploy yang Bisa Diulang
Simpan sebagai deploy.sh di root proyek.
#!/usr/bin/env bash
set -euo pipefail
cd /var/www/laravel-app
php artisan down --retry=60
git pull origin main
composer install --no-dev --optimize-autoloader --no-interaction
php artisan migrate --force
php artisan optimize:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
# muat ulang worker agar memakai kode terbaru
php artisan queue:restart
sudo systemctl reload php8.3-fpm
php artisan up
echo "Deploy selesai: $(date)"
chmod +x deploy.sh
./deploy.sh
php artisan down menampilkan halaman maintenance selama proses berlangsung, dan reload php8.3-fpm membersihkan OPcache agar kode baru terbaca.
10. Checklist Verifikasi
- ☐
https://contoh-domain.comterbuka dengan gembok HTTPS. - ☐
https://contoh-domain.com/.envmengembalikan 404. - ☐
APP_DEBUG=falsedanAPP_ENV=production. - ☐
php artisan abouttidak menampilkan debug mode aktif. - ☐
sudo supervisorctl statusmenunjukkan workerRUNNING. - ☐
sudo ufw statushanya membuka SSH, HTTP, dan HTTPS. - ☐ Backup database terjadwal (misalnya
mysqldumpvia cron).
curl -I https://contoh-domain.com
curl -I https://contoh-domain.com/.env
Masalah Umum
| Gejala | Penyebab Umum | Solusi |
|---|---|---|
| 502 Bad Gateway | PHP-FPM mati atau path socket salah | Cek systemctl status php8.3-fpm dan path fastcgi_pass |
| 403/404 di semua halaman | root Nginx salah |
Arahkan ke folder public |
| 500 setelah deploy | Izin storage atau cache konfigurasi lama |
chmod -R 775 storage, lalu php artisan optimize:clear |
| Gambar upload tidak tampil | Symlink belum dibuat | php artisan storage:link |
| Perubahan kode tidak terbaca | OPcache | sudo systemctl reload php8.3-fpm |
Log pertama yang harus dibuka saat ada masalah:
sudo tail -f /var/log/nginx/error.log
tail -f /var/www/laravel-app/storage/logs/laravel.log
Penutup
Susunan Nginx, PHP-FPM, dan MySQL ini sudah cukup untuk sebagian besar aplikasi Laravel skala kecil sampai menengah. Langkah selanjutnya yang layak dipertimbangkan adalah deployment tanpa downtime (struktur releases/current), caching dengan Redis, dan pemantauan server. Kalau trafik mulai naik, lihat juga pembahasan load balancer untuk Laravel.