Install Laravel Nginx Di Ubuntu
                        Kali ini saya akan berbagi mengenai instalasi Laravel 9 dengan webserver Nginx pada distro Ubuntu 22.04
Daftar Isi:
Prasyarat:
- VPS minimal 1GB RAM
 - Ubuntu 22.04 Fresh Install
 
Update sistem
Lakukan update sistem terlebih dahulu menggunakan apt update dan apt upgrade
sudo apt update && sudo apt upgrade -y
Install Nginx, PHP dan Git
Secara default, versi PHP yang digunakan adalah 8.1 dan versi PHP yang dibutuhkan oleh Laravel 9 juga 8.1, sehingga kita bisa langsung install php.
sudo apt install php php-fpm php-mbstring php-xml php-bcmath php-curl php-json php-tokenizer php-common php-cli unzip nginx git
cek masing-masing versi
nginx -v
php --version
git --version
Install Composer
Versi composer untuk Laravel 9 ini minimal adalah versi 2.x, untuk installasi jalankan perintah berikut.
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
setelah itu cek versi composer dengan perintah
composer --version
Ubah file default didalam folder /etc/nginx/sites-available/
sudo nano /etc/nginx/sites-available/default
kemudian sesuaikan seperti dibawah ini
server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    root /var/www/html/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 ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
Setelah itu lakukan restart nginx
sudo systemctl restart nginx
Composer project Laravel
Masuk ke folder root website, dalam contoh ini ada di /var/www/html/
cd /var/www/html/
kemudian lakukan composer project
composer create-project laravel/laravel .
Install certbot
Jalankan perintah berikut untuk installasi certbot
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
silahkan install ssl untuk web
sudo certbot --nginx
Jika sudah selesai, maka halaman web otomatis redirect ke https
Sharing is caring!