BAB 1: KENAPA PHP?

1-4: Setup Lingkungan Development

Objectives

  • Install dan configure PHP development environment
  • Memahami perbedaan XAMPP, Laragon, dan cara manual
  • Bisa run PHP script pertama kamu

Pilihan Development Environment

EnvironmentPlatformStackKarakteristik
XAMPPCross-platformApache + MySQLMudah, cocok pemula
LaragonWindows onlyApache/NginxAuto DNS, modern
ManualLinux-centricCustomFull control, complex
DockerCross-platformContainerizedReproducible, learning curve

Opsi 1: XAMPP (Recommended untuk Pemula)

Apa itu XAMPP?

XAMPP adalah singkatan dari:

  • X = Cross-platform (Windows, Linux, macOS)
  • A = Apache (web server)
  • M = MySQL/MariaDB (database)
  • P = PHP
  • P = Perl

Download & Install XAMPP

Langkah 1: Download

  1. Buka https://www.apachefriends.org/download.html
  2. Pilih versi PHP 8.x (terbaru)
  3. Download sesuai OS kamu (Windows/macOS/Linux)
  4. Jalankan installer

Langkah 2: Install

Windows:

  • Run installer (.exe)
  • Pilih lokasi (default: C:\xampp)
  • Uncheck "Learn more about Bitnami" (optional)
  • Next, Next, Finish

macOS:

  • Open downloaded .dmg
  • Drag XAMPP ke Applications
  • Open XAMPP Control Panel

Langkah 3: Start Services

XAMPP Control Panel:

  • Apache → Start (klik Start)
  • MySQL → Start (klik Start)
  • Ports → Default: Apache=80, MySQL=3306

Langkah 4: Verify

  1. Buka browser
  2. Ketik: http://localhost
  3. Kalau muncul halaman XAMPP → Success! 🎉

Struktur Folder XAMPP

C:\xampp\                    # Folder install XAMPP
├── htdocs\                  # Document root (letakkan file PHP di sini!)
│   ├── index.php
│   ├── project-saya.php
│   └── folder-saya\
│       └── file.php
├── apache\                   # Apache web server
├── mysql\                    # MySQL database
├── php\                      # PHP installation
│   └── php.ini              # PHP configuration
├── FileZilla\               # FTP server
└── Mercury\                 # Mail server

Testing PHP dengan XAMPP

Langkah 1: Buat file PHP

php
<?php
// File: C:\xampp\htdocs\hello.php

echo "Hello dari XAMPP!";
phpinfo();
?>

Langkah 2: Buka di Browser

Buka http://localhost/hello.php di browser.

Langkah 3: Kalau bisa lihat info PHP → All good! ✅

Opsi 2: Laragon (Windows Only, More Powerful)

Kenapa Laragon?

  • ✅ Auto Virtual Host — setiap project dapat URL sendiri (misal: myproject.test)
  • ✅ Auto SSL — HTTPS ready
  • ✅ Git integration — built-in
  • ✅ Node.js, Python, Ruby — multi-language
  • ✅ Database management — phpMyAdmin included
  • ✅ Terminal integrated — Git Bash, PowerShell
  • ✅ Portable — bisa jalan dari USB

Install Laragon

Langkah 1: Download

  1. Buka https://laragon.org/download
  2. Download "Laragon Full" (includes everything)
  3. Install dengan default settings

Langkah 2: Start Laragon

Tray Icon Laragon:

  • Klik Start All
  • Apache: Running
  • MySQL: Running
  • Web: Klik → Browser → Buka browser

Langkah 3: Auto Virtual Host

  1. Buat folder: C:\laragon\www\belajar-php
  2. Buat file index.php di dalam folder tersebut
  3. Browser otomatis buka: http://belajar-php.test
  4. Laravel auto-detect & pretty URL!

Struktur Folder Laragon

C:\laragon\
├── www\                        # Document root
│   ├── belajar-php\            # Project 1
│   │   └── index.php
│   └── myapp\                  # Project 2
│       └── public\
│           └── index.php
├── bin\                        # Tools (PHP, Apache, MySQL)
├── data\
│   └── mysql\                 # Database files
└── etc\                        # Configuration

Opsi 3: Install PHP Manual

Windows (Manual)

Langkah 1: Download PHP

  1. Buka https://windows.php.net/download
  2. Pilih PHP 8.x VS16 x64 Thread Safe
  3. Extract ke: C:\php

Langkah 2: Add ke PATH

  1. Windows Search → "Environment Variables"
  2. Edit System Environment Variables
  3. Klik "Environment Variables"
  4. Edit "Path" → Add C:\php
  5. OK, OK, OK

Langkah 3: Verifikasi

bash
# Buka CMD baru (tutup CMD yang lama)
php -v

# Output:
PHP 8.3.x (cli) ...

Langkah 4: PHP Built-in Server

bash
# Navigate ke folder project
cd C:\xampp\htdocs\belajar

# Start server
php -S localhost:8000

# Buka browser: http://localhost:8000

macOS

Langkah 1: Install Homebrew

bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Langkah 2: Install PHP

bash
brew install php

# Verify
php -v

Langkah 3: Start Server

bash
php -S localhost:8000 -t ~/Sites/myproject

Linux (Ubuntu/Debian)

Langkah 1: Install

bash
sudo apt update
sudo apt install php php-mysql php-cli

# Verify
php -v

Langkah 2: Install Apache/MySQL

bash
sudo apt install apache2 mysql-server
sudo service apache2 start
sudo service mysql start

Opsi 4: Docker (Recommended untuk Production-like)

Apa itu Docker?

Docker lets you run services (PHP, MySQL, Redis) dalam container yang isolated dan bisa di-share antar tim.

docker-compose.yml untuk PHP

yaml
# docker-compose.yml
version: '3.8'

services:
  php:
    image: php:8.3-apache
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: myapp
    volumes:
      - db_data:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin
    ports:
      - "8080:80"
    depends_on:
      - db

volumes:
  db_data:

Run dengan Docker

bash
# Start semua services
docker-compose up -d

# Buka browser
# http://localhost      → PHP app
# http://localhost:8080 → phpMyAdmin

Troubleshooting Umum

🚫 Apache tidak mau start?

  • Port 80 sudah dipakai → cek IIS, Skype
  • Skype Options → Connection → uncheck port 80
  • XAMPP → Config → Service and Port Settings

🚫 "Connection refused" atau blank page?

  • Pastikan Apache running
  • Cek file .php ada di htdocs
  • Enable error display:
    php
    <?php phpinfo(); ini_set('display_errors', 1); ?>
    

🚫 MySQL tidak mau start?

  • Port 3306 sudah dipakai → ganti port
  • C:\xampp\mysql\data\ → check error log
  • Backup data → delete ibdata1 → restart

🚫 Permission denied di Linux?

  • sudo chmod 755 /var/www/html
  • sudo chown -R www-data:www-data /var/www/html
  • sudo service apache2 restart

Recommended: VS Code Setup

Extensions yang Perlu Diinstall

#ExtensionFungsi
1PHP IntelephenseCode completion, refactoring
2PHP DebugXdebug integration
3PHP Namespace ResolverAuto-import
4Prettier - Code formatterAuto format
5Live ServerAuto refresh browser (HTML/CSS/JS)
6Dracula OfficialDark theme (optional)

VS Code Settings untuk PHP

json
// .vscode/settings.json
{
    "php.validate.enable": true,
    "php.validate.run": "onSave",
    "php.suggest.basic": false,
    "editor.formatOnSave": true,
    "[php]": {
        "editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
    }
}

Exercise

  1. ✅ Install XAMPP atau Laragon
  2. ✅ Start Apache dan MySQL
  3. ✅ Buat file perkenalan.php di htdocs dengan konten:
    • Nama kamu
    • Tanggal hari ini
    • Pesan "Website pertama saya!"
  4. ✅ Buka di browser: http://localhost/perkenalan.php
  5. ✅ Bonus: Install VS Code + PHP Intelephense extension

Summary

EnvironmentProsConsBest For
XAMPPEasy, cross-platformBasic featuresPemula
LaragonAuto vhost, powerfulWindows onlyWindows dev
ManualFull controlComplex setupProduction-like
DockerReproducible, cleanLearning curveTeam projects

💡Tip

Recommendation: Mulai dengan XAMPP untuk simplicity. Kalau pakai Windows, upgrade ke Laragon untuk fitur lebih (auto vhost, terminal integrated). Docker bisa dipelajari kemudian saat kamu mulai kerja di tim.


💡Tip

Biasakan buka project dengan URL spesifik (misal: http://localhost/myproject) daripada langsung file (file:///C:/xampp/htdocs/project/). Ini lebih mendekati production environment dan menghindari path issues.

Klik tombol ini setelah menyelesaikan materi

📝

Quiz Bab 1

Uji pemahamanmu tentang Kenapa PHP?