Laravel

Laravel

Laravel adalah framework PHP yang bersifat open-source untuk membangun aplikasi web dengan arsitektur Model-View-Controller (MVC). Laravel dikenal karena syntax yang elegan, mudah dipahami, dan fitur bawaan yang lengkap, seperti routing, autentikasi, session, dan database migration. Framework ini menyediakan alat seperti Eloquent ORM untuk mengelola database, Blade Template Engine untuk memudahkan pembuatan tampilan, dan Artisan Command Line Tool untuk mengotomatisasi berbagai tugas. Laravel sangat cocok untuk membangun aplikasi web modern karena mendukung RESTful API, middleware, dan integrasi dengan berbagai layanan pihak ketiga.

1. Persyaratan Sistem

System requirements untuk Laravel:

  • PHP 8.2+
  • PHP Extension: Ctype, cURL, DOM, Fileinfo, Filter, Hash, Mbstring, OpenSSL, PCRE, PDO, Session, Tokenizer, XML
  • Nginx, FrankenPHP, atau Apache

Sebelum memulai deploy, siapkan:

  • VPS atau server dengan OS Ubuntu 24.04
  • Akses SSH ke server
  • Domain yang sudah diarahkan ke server

2. Persiapan Server

Update sistem

sudo apt update
sudo apt upgrade -y

Install MariaDB

sudo apt install mariadb-server -y

Tambah repository PPA ondrej/php

sudo add-apt-repository ppa:ondrej/php -y
sudo apt upgrade -y

Install PHP 8.2 dan extension yang dibutuhkan

sudo apt install php8.2 php8.2-cli php8.2-common \
  php8.2-apcu php8.2-mbstring php8.2-gd php8.2-intl \
  php8.2-xml php8.2-soap php8.2-bcmath php8.2-mysql php8.2-zip \
  php8.2-curl php8.2-tidy php8.2-imagick php8.2-sqlite3 -y

Install Composer

sudo wget https://getcomposer.org/download/latest-stable/composer.phar -O /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

3. Buat Database

Login ke MariaDB

sudo mysql

Buat database dan user

CREATE DATABASE laravel;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'rahasia';
GRANT ALL PRIVILEGES ON laravel.* TO 'laravel'@'localhost';
FLUSH PRIVILEGES;
exit

4. Konfigurasi Web Server

Apache

Install Apache

sudo apt install apache2 libapache2-mod-php8.2 -y

Buat konfigurasi virtual host untuk contoh.com

sudo nano /etc/apache2/sites-available/contoh.com.conf	

Masukkan konfigurasinya

<VirtualHost *:80>
    ServerName contoh.com
    ServerAlias www.contoh.com
    DocumentRoot /var/www/contoh.com/public
    <Directory /var/www/contoh.com/public>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/contoh.com_error.log
    CustomLog /var/log/apache2/contoh.com_access.log combined
</VirtualHost>

Aktifkan modul rewrite, virtual host, dan restart apache2

sudo a2enmod rewrite
sudo a2ensite contoh.com
sudo systemctl restart apache2	

Install certbot untuk Apache

sudo apt install certbot python3-certbot-apache -y	

Request sertifikat SSL

sudo certbot --non-interactive \
    -m [email protected] \
    --agree-tos \
    --no-eff-email \
    --apache -d contoh.com -d www.contoh.com \
    --redirect	

Nginx

Install Nginx

sudo apt install nginx php8.2-fpm -y

Buat virtual host

sudo nano /etc/nginx/sites-available/contoh.com.conf

Masukkan konfigurasinya

server {
    listen 80;
    server_name www.contoh.com contoh.com;
    root /var/www/contoh.com/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 ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Aktifkan virtual host

sudo ln -s /etc/nginx/sites-available/contoh.com.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Install certbot untuk Nginx

sudo apt install certbot python3-certbot-nginx -y

Request sertifikat SSL

sudo certbot --non-interactive \
    -m [email protected] \
    --agree-tos \
    --no-eff-email \
    --nginx -d contoh.com -d www.contoh.com \
    --redirect	

5. Install Laravel

Install Laravel sebagai sample aplikasi yang akan di-deploy.

Jika sudah ada aplikasi Laravel yang siap deploy, lompat ke pengaturan konfigurasi .env.

sudo apt install unzip -y
composer create-project laravel/laravel contoh.com

Masuk ke direktori dan buka file .env

cd contoh.com
nano env .env

Atur konfigurasi URL dan database

APP_NAME="My Laravel App"
APP_ENV=production
APP_DEBUG=false
APP_TIMEZONE=Asia/Jakarta
APP_URL=https://www.contoh.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=rahasia

Lakukan migration dan seeding data

php artisan migrate --force
php artisan db:seed

atau import dari file dump .sql

mysql -u laravel -p laravel < backup-db.sql

Pindahkan direktori aplikasi Laravel ke /var/www

sudo mv contoh.com /var/www

Bersihkan cache

cd /var/www/contoh.com
php artisan optimize:clear

Ubah user-group direktori

sudo chown -R www-data:www-data /var/www/contoh.com

Deploy aplikasi Laravel telah selesai. Lakukan pengujian akses dan fungsi aplikasi.