Server

Cara Deploy Website ke VPS Linux dari Nol Lengkap 2025

Deploy website ke VPS itu skill wajib untuk web developer. Pertama kali saya coba, prosesnya membingungkan — banyak langkah yang harus urut. Di sini saya tulis step-by-step yang sudah saya coba sendiri, dari beli VPS sampai website online dengan HTTPS.

Apa itu VPS?

VPS (Virtual Private Server) adalah server virtual yang berjalan di dalam server fisik. Berbeda dengan shared hosting, kamu mendapatkan resource dedicated (CPU, RAM, storage) dan akses root penuh. VPS memberikan fleksibilitas untuk install software apapun dan konfigurasi server sesuai kebutuhan.

Prasyarat

  • VPS dengan Ubuntu 22.04 atau 24.04 (bisa beli di DigitalOcean, Vultr, Hetzner, atau IDCloudHost)
  • Domain name (bisa beli di Namecheap, Niagahoster, atau Domainesia)
  • Aplikasi terminal (Terminal di Mac/Linux, PowerShell/WSL di Windows)
  • Website files yang sudah siap di-deploy

Step 1: Koneksi ke VPS via SSH

Setelah membeli VPS, kamu akan mendapatkan IP address, username (root), dan password. Koneksi ke VPS menggunakan SSH:

# Koneksi ke VPS
ssh root@YOUR_VPS_IP

# Contoh
ssh [email protected]

# Setelah pertama kali login, buat user baru (jangan pakai root untuk sehari-hari)
adduser deploy
usermod -aG sudo deploy

# Setup SSH key (lebih aman dari password)
# Di local machine, generate SSH key:
ssh-keygen -t ed25519 -C "[email protected]"

# Copy public key ke VPS
ssh-copy-id deploy@YOUR_VPS_IP

Step 2: Setup Keamanan Dasar

# Update system
sudo apt update && sudo apt upgrade -y

# Setup firewall (UFW)
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

# Cek status firewall
sudo ufw status

# Install fail2ban (proteksi dari brute force)
sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Step 3: Install Nginx Web Server

# Install Nginx
sudo apt install nginx -y

# Start dan enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx

# Cek status
sudo systemctl status nginx

# Test: buka http://YOUR_VPS_IP di browser
# Jika muncul "Welcome to nginx!" = berhasil

Step 4: Install PHP dan Extensions

# Install PHP 8.2 dan extensions yang dibutuhkan
sudo apt install php8.2-fpm php8.2-mysql php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip php8.2-bcmath php8.2-intl php8.2-readline php8.2-opcache -y

# Cek versi PHP
php -v

# Cek PHP-FPM status
sudo systemctl status php8.2-fpm

Step 5: Install MySQL Database

# Install MySQL
sudo apt install mysql-server -y

# Secure installation
sudo mysql_secure_installation

# Jawab pertanyaan:
# - Set root password? Y (masukkan password kuat)
# - Remove anonymous users? Y
# - Disallow root login remotely? Y
# - Remove test database? Y
# - Reload privilege tables? Y

# Login ke MySQL dan buat database
sudo mysql -u root -p

# Di dalam MySQL shell:
CREATE DATABASE mywebsite;
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'PasswordKuat123!';
GRANT ALL PRIVILEGES ON mywebsite.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 6: Upload Website Files

# Di LOCAL machine, upload website files ke VPS
# Cara 1: Menggunakan scp
scp -r /path/to/your/website/* deploy@YOUR_VPS_IP:/var/www/mywebsite/

# Cara 2: Menggunakan rsync (lebih cepat, bisa resume)
rsync -avz -e ssh /path/to/your/website/ deploy@YOUR_VPS_IP:/var/www/mywebsite/

# Cara 3: Menggunakan Git (recommended untuk development)
cd /var/www/mywebsite
sudo git clone https://github.com/username/repo.git .

# Set permissions yang benar
sudo chown -R www-data:www-data /var/www/mywebsite
sudo chmod -R 755 /var/www/mywebsite
sudo chmod -R 775 /var/www/mywebsite/writable  # untuk CI4

Step 7: Konfigurasi Nginx Virtual Host

# Buat file konfigurasi Nginx
sudo nano /etc/nginx/sites-available/mywebsite
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/mywebsite/public;
    index index.php index.html;

    # Logging
    access_log /var/log/nginx/mywebsite-access.log;
    error_log /var/log/nginx/mywebsite-error.log;

    # Handle CodeIgniter 4 routing
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # PHP-FPM
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }

    # Deny access to hidden files
    location ~ /\. {
        deny all;
    }

    # Cache static files
    location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|webp|woff|woff2)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
}
# Enable site
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/

# Remove default site (opsional)
sudo rm /etc/nginx/sites-enabled/default

# Test konfigurasi
sudo nginx -t

# Restart Nginx
sudo systemctl restart nginx

Step 8: Setup Domain dan DNS

# Di dashboard domain registrar (Namecheap, Cloudflare, dll):
# Tambahkan DNS records:
# Type: A     | Name: @      | Value: YOUR_VPS_IP
# Type: A     | Name: www    | Value: YOUR_VPS_IP

# Tunggu propagasi DNS (biasanya 5-30 menit)
# Cek dengan:
dig yourdomain.com
# Atau
nslookup yourdomain.com

Step 9: Install SSL Certificate (HTTPS)

# Install Certbot
sudo apt install certbot python3-certbot-nginx -y

# Generate dan install SSL certificate
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

# Ikuti instruksi:
# - Masukkan email
# - Agree to terms: Y
# - Redirect HTTP to HTTPS: 2 (Yes)

# Cek auto-renewal
sudo certbot renew --dry-run

# SSL akan otomatis diperbarui setiap 90 hari

Step 10: Deploy dan Test

# Untuk CodeIgniter 4, jalankan migration
cd /var/www/mywebsite
php spark migrate

# Set environment ke production
# Edit .env file
nano .env
# Ubah: CI_ENVIRONMENT = production

# Test website di browser
# https://yourdomain.com

# Monitor log jika ada error
sudo tail -f /var/log/nginx/mywebsite-error.log

Troubleshooting Umum

  • 502 Bad Gateway: PHP-FPM tidak berjalan. Cek: sudo systemctl status php8.2-fpm
  • 404 Not Found: Nginx root path salah. Pastikan root mengarah ke folder public
  • 403 Forbidden: Permissions salah. Cek: ls -la /var/www/mywebsite
  • 500 Internal Server Error: Cek error log: sudo tail -f /var/log/nginx/mywebsite-error.log
  • Database connection failed: Cek credentials di file .env atau config database

Kesimpulan

Deploy website ke VPS memang butuh beberapa langkah, tapi setelah dipahami, prosesnya menjadi rutin. Keuntungan VPS: kontrol penuh, performa lebih baik, dan biaya lebih murah dibanding managed hosting untuk traffic tinggi. Setelah deploy pertama berhasil, update website selanjutnya cukup upload file baru dan restart PHP-FPM. Untuk workflow yang lebih profesional, pelajari CI/CD dengan GitHub Actions atau GitLab CI untuk auto-deploy setiap push ke repository.


You may also like


0 Comments


Leave a Reply

Comments with links or spam keywords will be rejected.
Scroll to Top