# Panduan Deploy ke VPS/Server

> **Panduan lengkap** mengdeploy website ke VPS atau dedicated server.
> Untuk server seperti DigitalOcean, Linode, AWS EC2, Vultr, dll.

<div align="center">

**Mulai Digital** — *Solusi Website untuk Pertumbuhan Bisnis Anda*

</div>

---

## Daftar Isi

1. [Sebelum Memulai](#1-sebelum-memulai)
2. [Persiapan Server](#2-persiapan-server)
3. [Install Dependencies](#3-install-dependencies)
4. [Upload Project](#4-upload-project)
5. [Konfigurasi Website](#5-konfigurasi-website)
6. [Setup Database](#6-setup-database)
7. [Konfigurasi Web Server](#7-konfigurasi-web-server)
8. [SSL Certificate](#8-ssl-certificate)
9. [Finalisasi](#9-finalisasi)
10. [Troubleshooting](#10-troubleshooting)

---

## 1. Sebelum Memulai

### Yang Anda Butuhkan

| Item | Keterangan |
|------|------------|
| **VPS/Server** | Minimal 1GB RAM, 20GB storage |
| **OS** | Ubuntu 22.04/24.04 atau Debian 12 |
| **SSH Access** | Akses root atau sudo user |
| **Domain** | Sudah pointing ke IP server |

### Spesifikasi Minimum Server

| Komponen | Minimum | Recommended |
|----------|---------|-------------|
| **RAM** | 1GB | 2GB+ |
| **Storage** | 20GB | 40GB+ |
| **CPU** | 1 vCPU | 2 vCPU+ |
| **OS** | Ubuntu 22.04 | Ubuntu 24.04 |

---

## 2. Persiapan Server

### 2.1 Login ke Server

```bash
ssh root@ip-server-anda
```

### 2.2 Update Server

```bash
sudo apt update && sudo apt upgrade -y
```

### 2.3 Buat User Baru (Recommended)

```bash
# Buat user baru
adduser deployer

# Tambahkan ke sudo group
usermod -aG sudo deployer

# Switch ke user baru
su - deployer
```

### 2.4 Install packages yang dibutuhkan

```bash
sudo apt install -y git curl zip unzip
```

---

## 3. Install Dependencies

### 3.1 Install PHP 8.4

```bash
# Tambahkan repository PHP
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

# Install PHP 8.4 dan extension
sudo apt install -y php8.4 php8.4-cli php8.4-fpm php8.4-mysql \
    php8.4-mbstring php8.4-xml php8.4-curl php8.4-gd \
    php8.4-zip php8.4-bcmath php8.4-intl php8.4-redis
```

### 3.2 Install Composer

```bash
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
```

### 3.3 Install MySQL

```bash
sudo apt install -y mysql-server
sudo mysql_secure_installation
```

Ikuti prompt:
- Set root password: **Y** → Masukkan password
- Remove anonymous users: **Y**
- Disallow root login remotely: **Y**
- Remove test database: **Y**
- Reload privilege tables: **Y**

### 3.4 Install Nginx

```bash
sudo apt install -y nginx
```

---

## 4. Upload Project

### 4.1 Build Aset di Lokal

Di komputer Anda, jalankan:

```bash
# Build aset frontend
npm run build

# Cache konfigurasi
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Kompres project
cd /path/to/project
zip -r website-mulai-digital.zip . -x "node_modules/*" ".git/*" "tests/*"
```

### 4.2 Upload ke Server

```bash
# Dari komputer Anda
scp website-mulai-digital.zip deployer@ip-server:~/
```

### 4.3 Ekstrak di Server

```bash
# Login ke server
ssh deployer@ip-server

# Buat folder project
sudo mkdir -p /var/www/mulai-digital
sudo chown -R deployer:deployer /var/www/mulai-digital

# Ekstrak file
cd /var/www/mulai-digital
unzip ~/website-mulai-digital.zip
```

---

## 5. Konfigurasi Website

### 5.1 Edit File .env

```bash
nano .env
```

Ubah konfigurasi berikut:

```env
APP_NAME="Mulai Digital"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://website-anda.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mulai_digital
DB_USERNAME=mulai_user
DB_PASSWORD=password-yang-kuat

SESSION_DRIVER=database
CACHE_STORE=database
QUEUE_CONNECTION=database
```

### 5.2 Generate Application Key

```bash
php artisan key:generate
```

### 5.3 Buat Storage Link

```bash
php artisan storage:link
```

---

## 6. Setup Database

### 6.1 Buat Database & User

```bash
sudo mysql -u root -p
```

Masukkan password MySQL, lalu jalankan:

```sql
-- Buat database
CREATE DATABASE mulai_digital CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Buat user
CREATE USER 'mulai_user'@'127.0.0.1' IDENTIFIED BY 'password-yang-kuat';

-- Grant akses
GRANT ALL PRIVILEGES ON mulai_digital.* TO 'mulai_user'@'127.0.0.1';

-- Reload privilege
FLUSH PRIVILEGES;

-- Keluar
EXIT;
```

### 6.2 Jalankan Migrasi

```bash
cd /var/www/mulai-digital
php artisan migrate --force
php artisan db:seed --force
```

---

## 7. Konfigurasi Web Server

### 7.1 Buat Config Nginx

```bash
sudo nano /etc/nginx/sites-available/mulai-digital
```

Paste konfigurasi berikut:

```nginx
server {
    listen 80;
    server_name website-anda.com www.website-anda.com;
    root /var/www/mulai-digital/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    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:/var/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

### 7.2 Aktifkan Config

```bash
# Buat symlink
sudo ln -s /etc/nginx/sites-available/mulai-digital /etc/nginx/sites-enabled/

# Hapus default config (opsional)
sudo rm /etc/nginx/sites-enabled/default

# Test config
sudo nginx -t

# Restart Nginx
sudo systemctl restart nginx
```

---

## 8. SSL Certificate (HTTPS)

### 8.1 Install Certbot

```bash
sudo apt install -y certbot python3-certbot-nginx
```

### 8.2 Dapatkan SSL Certificate

```bash
sudo certbot --nginx -d website-anda.com -d www.website-anda.com
```

Ikuti prompt:
- Email: Masukkan email Anda
- Terms of Service: **A**
- Share email: **N** atau **Y** (sesuai keinginan)
- Redirect: **2** (Redirect HTTP to HTTPS)

### 8.3 Auto-Renewal

SSL certificate akan auto-renew. Untuk test:

```bash
sudo certbot renew --dry-run
```

---

## 9. Finalisasi

### 9.1 Set Permission

```bash
sudo chown -R www-data:www-data /var/www/mulai-digital
sudo chmod -R 755 /var/www/mulai-digital
sudo chmod -R 775 /var/www/mulai-digital/storage
sudo chmod -R 775 /var/www/mulai-digital/bootstrap/cache
```

### 9.2 Akses Website

Buka browser dan akses:
```
https://website-anda.com
```

### 9.3 Login Admin

```
https://website-anda.com/login
```

**Akun Default:**
| Field | Nilai |
|-------|-------|
| **Email** | admin@example.com |
| **Password** | admin@example.com |

### 9.4 Setup Queue Worker (Opsional)

Jika menggunakan queue untuk email/notifikasi:

```bash
# Jalankan queue worker
php artisan queue:work --sleep=3 --tries=3
```

Untuk menjalankan otomatis, buat service:

```bash
sudo nano /etc/systemd/system/mulai-digital-worker.service
```

```ini
[Unit]
Description=Mulai Digital Queue Worker
After=network.target

[Service]
User=www-data
Group=www-data
Restart=always
ExecStart=/usr/bin/php /var/www/mulai-digital/artisan queue:work --sleep=3 --tries=3

[Install]
WantedBy=multi-user.target
```

```bash
sudo systemctl enable mulai-digital-worker
sudo systemctl start mulai-digital-worker
```

---

## 10. Troubleshooting

### Error 500 Internal Server Error

**Solusi:**
1. Cek log Nginx:
   ```bash
   sudo tail -f /var/log/nginx/error.log
   ```
2. Cek log Laravel:
   ```bash
   tail -f /var/www/mulai-digital/storage/logs/laravel.log
   ```
3. Pastikan `APP_DEBUG=false` di `.env`
4. Cek permission:
   ```bash
   sudo chmod -R 775 /var/www/mulai-digital/storage
   ```

### Website Tidak Bisa Diakses

**Solusi:**
1. Cek Nginx running:
   ```bash
   sudo systemctl status nginx
   ```
2. Cek config Nginx:
   ```bash
   sudo nginx -t
   ```
3. Pastikan domain sudah pointing ke IP server

### Error "No application encryption key"

**Solusi:**
```bash
cd /var/www/mulai-digital
php artisan key:generate
```

### Error "SQLSTATE Connection refused"

**Solusi:**
1. Cek MySQL running:
   ```bash
   sudo systemctl status mysql
   ```
2. Cek `DB_*` di file `.env`

### CSS/JS Tidak Muncul

**Solusi:**
1. Pastikan folder `public/build` ada
2. Jika tidak ada, build ulang di lokal dan upload
3. Cek permission:
   ```bash
   sudo chmod -R 755 /var/www/mulai-digital/public/build
   ```

### SSL Certificate Error

**Solusi:**
```bash
sudo certbot renew --force-renewal
sudo systemctl restart nginx
```

---

## Butuh Bantuan?

Jika mengalami kendala, hubungi kami:

-  **Email:** [mulaidigitalofficial@gmail.com](mailto:mulaidigitalofficial@gmail.com)
-  **Website:** [mulaidigital.com](https://mulaidigital.com)

---

**Terima kasih telah membeli source code dari Mulai Digital!**
