Langkah-langkah Setup Web Server Nginx, PHP-FPM untuk Laravel di Ubuntu 22.04

AH
Ahmad Lukman Hakim

Dedicated Author of LUKMANLAB

# nginx# php# laravel

Tujuan

  • Melakukan instalasi dan konfigurasi Web Server (Nginx, PHP-FPM)
  • Instalasi Composer
  • Setting Server Block (Virtual Host kalau di Apache2)

Persiapan

  • Pastikan server anda sudah bisa diakses melalui SSH.
  • Pastikan jaringan di server cukup cepat agar proses download tidak butuh waktu lama.

Disini saya memberi contoh dengan kondisi Server masih clean. Artinya, belum ada tools atau aplikasi yang ada didalamnya.

Langkah-langkah

Ikuti langkah-langkah dengan seksama, jangan terburu-buru. Kalau perlu sambil ngopi santai. :D

Instalasi dan konfigurasi Web Server (Nginx, PHP-FPM)

  • Lakukan update repository dan upgrade package
sudo apt update
sudo apt dist-upgrade
  • Instalasi Nginx
sudo apt install nginx
  • Instalasi PHP-FPM 7.4 atau versi yang lain
LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/ph
sudo apt update

sudo apt install php7.4-fpm php7.4-mbstring php7.4-gd php7.4-mysql php7.4-zip php7.4-xml php7.4-json php7.4-curl php7.4-common php7.4-cli php7.4-opcache php7.4-readline
  • Pastikan service Nginx dan PHP-FPM berjalan
sudo systemctl status nginx
sudo systemctl status php7.4-fpm.service

Instalasi Composer

Instalasi composer yang akan dicontohkan membutuhkan command php. Ketika package php7.4-cli kita instal, maka sistem linux akan mempunyai command tersebut.

  • Jalankan perintah berikut baris demi baris.
> php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

> php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

> php composer-setup.php

> php -r "unlink('composer-setup.php');"

> sudo mv composer.phar /usr/local/bin/composer
  • Clone projek laravel anda, dan jalan kan composer install pada non-root user.
cd /var/www/

mkdir api.lukmanlab.com

cd api.lukmanlab.com/

git clone ${ URL_REPOSITORY_GIT_ANDA } .

chown userlinux:www-data -R ./

chmod 775 -R storage public

composer install

Konfigurasi Nginx Server Block untuk Projek Laravel

  • Buat file konfigurasi nginx
vi /etc/nginx/sites-available/api.lukmanlab.com
  • Isi file tersebut dengan script berikut:
server {
    listen 80;
    root /var/www/api.lukmanlab.com/public;
    index index.php index.html;
    server_name api.lukmanlab.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    access_log /var/log/nginx/api.lukmanlab.com.log;
    error_log /var/log/nginx/api.lukmanlab.com.error.log;

    location ~ /\.ht {
            deny all;
    }

    location ~ /\.env {
            deny all;
    }

    location ~ /\.git {
            deny all;
    }
}
  • Enable file konfigurasi tersebut
cd /etc/nginx/sites-enabled/

ln -s /etc/nginx/sites-available/api.lukmanlab.com .
  • Test file konfigurasi nginx
nginx -t

Expected Output:

  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
  • Reload nginx
systemctl reload nginx

Lanjutan

Note:

  • Sebenarnya ada beberapa langkah setelah ini, yaitu kita perlu membuat / edit file .env projek laravel.
  • Melakukan migrate data, biasanya menggunakan php artisan migrate
  • Melakukan Seeding data ke database, biasanya menggunakan php artisan db:seed