Halo, kalau kamu merasa tulisan saya ngebantu kamu, kamu bisa ucapkan terima kasih lewat saweria .

If you feel this website help you, you can donate at saweria .



What I Used in This Tutorial

  1. Unit 1.31.1 (unit:1.31.1-php8.2)

  2. Docker version 24.0.7, build afdd53b


How To

Di sini saya pake Unit nya menggunakan docker biar lebih ringkas

  1. Buat file docker-compose.yml

    
    version: '3.8'
    
    services:
      nginx-unit:
        container_name: nginx-unit-ctr
        image: unit:1.31.1-php8.2
        restart: always
        ports:
          - "8080:8080"
        volumes:
          - $PWD/config.json:/docker-entrypoint.d/config.json
          - $PWD/www/:/www/
    
  2. Buat file baru di folder www

    mkdir www
    echo '<?php echo "Hello pono"; ?>' > www/index.php  
    
  3. Buat file config Unit, beri nama config.json

    {
       #use port 8080
      "listeners": {
        "*:8080": {
          #proxy pass name
          "pass": "applications/php_app"
        }   
      },  
      "applications": {
        "php_app": {
         #type runtime
          "type": "php",
          "root": "/www/"
        }   
      }
    }
    
  4. Jalankan menggunakan docker-compose up -d

Menjalankan Laravel dengan Unit

  1. Buat project laravel baru dengan composer

    composer create-project laravel/laravel app 
    
  2. Then change storage directory permission

    sudo find . -type f -exec chmod 664 {} \;   
    sudo find . -type d -exec chmod 775 {} \;
    
    sudo chgrp -R www-data storage bootstrap/cache
    sudo chmod -R ug+rwx storage bootstrap/cache
    
  3. Create file docker-compose.yml

    version: '3.8'
    
    services:
      nginx-unit-laravel:
        container_name: nginx-unit-laravel-ctr
          #user: 'www-data:www-data'
        image: unit:1.31.1-php8.2
        restart: always
        ports:
          - "8081:8081"
        volumes:
          - $PWD/config.json:/docker-entrypoint.d/config.json
          - $PWD:/www/
    
  4. Create config.json

    {
    "listeners": {
        "*:8081": {
            "pass": "routes"
        }
    },
    
    "routes": [
        {
            "match": {
                "uri": "!/index.php"
            },
            "action": {
                "share": "/www/public$uri",
                "fallback": {
                    "pass": "applications/laravel"
                }
            }
        }
    ],
    
    "applications": {
        "laravel": {
            "type": "php",
            "root": "/www/public/",
            "script": "index.php"
        }
    }
    
  5. Jalankan docker-compose up

Jangan Pake -d Biar Bisa Baca Log-nya

Jangan Pake -d Biar Bisa Baca Log-nya

Deploy Laravel Pake Nginx Unit Sukses

Deploy Laravel Pake Nginx Unit Sukses


Kelebihan & Kekurangan

Kelebihan:

  1. Ga perlu install runtime untuk jalanin aplikasi. Ini di php yg paling kerasa, jadi ga usah install php-fpm sama Nginx, cuma install Unit aja

*Kekurangan:

  1. Belum bisa setup certbot kaya di nginx (bisa otomatis pake hook tapi ini ribet)

  2. Walaupun dokumentasi lengkap, tapi tutorial di “dunia nyata” masih sedikit.

Referensi:

  1. Unit Documentation - Unit in Docker

  2. Unit Documentation - Laravel