> For the complete documentation index, see [llms.txt](https://dika-maulidal.gitbook.io/home/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dika-maulidal.gitbook.io/home/deploy/azure-vm-laravel.md).

# Azure VM Laravel

Panduan ini berisi langkah-langkah lengkap untuk deployment awal aplikasi Laravel di Azure Virtual Machine (Ubuntu), prosedur maintenance/update kode (*git pull*), serta cara menghentikan service atau VM.

### 1. Setup Deployment Awal (Initial Deployment)

#### Langkah 1: Akses Server via SSH

Buka terminal di komputer lokal dan masuk ke Azure VM:

```bash
ssh azureuser@<IP-PUBLIC-VM-ANDA>
```

#### Langkah 2: Instalasi Server Stack (LEMP Stack)

Jalankan pembaruan sistem dan instalasi paket yang dibutuhkan:

```bash
# Update repositori paket
sudo apt update && sudo apt upgrade -y

# Install Git, Nginx, Composer, Unzip, dan Curl
sudo apt install nginx git composer unzip curl -y

# Install PHP 8.3 & ekstensi yang dibutuhkan Laravel
sudo apt install php-fpm php-mysql php-mbstring php-xml php-curl php-zip php-gd php-cli php-bcmath -y

# Install MySQL Server
sudo apt install mysql-server -y
```

#### Langkah 3: Konfigurasi Database MySQL

1. Masuk ke prompt MySQL:

   ```bash
   sudo mysql
   ```
2. Buat database dan atur autentikasi user `root`:

   ```sql
   CREATE DATABASE omah_terapi;
   ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
   FLUSH PRIVILEGES;
   EXIT;
   ```

#### Langkah 4: Clone Repositori

```bash
# Pindah ke direktori /var/www dan atur hak akses
cd /var/www
sudo chown -R $USER:$USER /var/www

# Clone repositori public
git clone https://github.com/dika-maulidal/omah-terapiku.git

# Masuk ke direktori proyek
cd /var/www/omah-terapiku
```

#### Langkah 5: Configuration & Environment Setup

```bash
# Install dependensi PHP untuk produksi
composer install --optimize-autoloader --no-dev

# Salin dan sesuaikan file .env
cp .env.example .env
nano .env
```

**Pengaturan pada file `.env`:**

Cuplikan kode

```
APP_NAME=OmahTerapi
APP_ENV=production
APP_DEBUG=false
APP_URL=http://<IP-PUBLIC-VM-ANDA>

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=omah_terapi
DB_USERNAME=root
DB_PASSWORD=root
```

#### Langkah 6: Eksekusi Perintah Artisan & Setup Hak Akses

Jalankan perintah optimasi, migrasi, dan pengisian data awal:

```bash
# Generate Application Key
php artisan key:generate

# Jalankan migrasi database
php artisan migrate --force

# Jalankan seeder (opsional untuk data awal)
php artisan db:seed --force

# Buat link symbolic storage & cache
php artisan storage:link
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Atur hak akses direktori untuk user Nginx (www-data)
sudo chown -R www-data:www-data /var/www/omah-terapiku
sudo chmod -R 775 /var/www/omah-terapiku/storage /var/www/omah-terapiku/bootstrap/cache
```

#### Langkah 7: Konfigurasi Nginx

1. Buat file konfigurasi situs:

   ```bash
   sudo nano /etc/nginx/sites-available/omah-terapiku
   ```
2. Isi dengan konfigurasi Nginx:

   Nginx

   ```bash
   server {
       listen 80;
       server_name _;

       root /var/www/omah-terapiku/public;
       index index.php index.html;

       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$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
       }

       location ~ /\.(?!well-known).* {
           deny all;
       }
   }
   ```
3. Aktifkan konfigurasi dan restart Nginx:

   Bash

   ```bash
   sudo ln -s /etc/nginx/sites-available/omah-terapiku /etc/nginx/sites-enabled/
   sudo rm -f /etc/nginx/sites-enabled/default
   sudo nginx -t
   sudo systemctl restart nginx
   ```

Aplikasi dapat diakses melalui `http://<IP-PUBLIC-VM-ANDA>`.

### 2. Alur Pembaharuan Kode (Update / Maintenance Code)

Ketika ada perubahan kode di repositori GitHub (misalnya penambahan fitur, perubahan UI, atau migrasi database baru), jalankan prosedur berikut:

```bash
# 1. Masuk ke direktori proyek
cd /var/www/omah-terapiku

# 2. Kembalikan sementara hak akses folder ke user SSH (agar git pull tidak terhalang permission)
sudo chown -R $USER:$USER /var/www/omah-terapiku

# 3. Tarik kode terbaru dari GitHub
git pull origin main

# 4. Update dependensi Composer (jika ada penambahan paket baru)
composer install --optimize-autoloader --no-dev

# 5. Jalankan migrasi database baru (jika ada file migrasi baru)
php artisan migrate --force

# 6. Perbarui cache konfigurasi, route, dan view
php artisan config:cache
php artisan route:cache
php artisan view:cache

# 7. Kembalikan hak akses ke user Nginx (www-data)
sudo chown -R www-data:www-data /var/www/omah-terapiku
sudo chmod -R 775 /var/www/omah-terapiku/storage /var/www/omah-terapiku/bootstrap/cache
```

### 3. Cara Menghentikan Service & VM (Stop Procedures)

#### Menghentikan Web Server Nginx (Temporary Stop)

Gunakan opsi ini jika hanya ingin menutup akses aplikasi web tanpa mematikan VM:

```bash
# Hentikan Nginx
sudo systemctl stop nginx

# Jalankan kembali Nginx
sudo systemctl start nginx
```

#### Menghentikan Azure VM (Untuk Menghentikan Billing/Biaya)

Menghentikan VM dari dalam terminal OS tidak secara otomatis menghentikan tagihan alokasi sumber daya komputasi Azure.

1. Buka [Azure Portal](https://www.google.com/search?q=https://portal.azure.com/\&authuser=1).
2. Navigasi ke **Virtual Machines** > pilih VM.
3. Klik tombol **Stop** pada toolbar bagian atas.
4. Pastikan status VM berubah menjadi **Stopped (deallocated)** agar penggunaan kuota/biaya komputasi berhenti sepenuhnya.
